call.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. package action
  2. import (
  3. "errors"
  4. "fmt"
  5. "pbx-api-gin/internal/app/stc/active"
  6. "pbx-api-gin/internal/app/stc/priority"
  7. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  8. "pbx-api-gin/pkg/lfshook"
  9. "pbx-api-gin/pkg/utils"
  10. "sort"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444",
  16. "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483",
  17. "2484", "2485", "2411", "2412"}
  18. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  19. var Speakers = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181", "2311", "2381"}
  20. // Function triggered before no cab occupied signal interrupt
  21. func InActiveHangup() {
  22. if active.ActivedCab == "" {
  23. HangupTask("PA")
  24. HangupTask("CPA")
  25. HangupTask("VOL")
  26. //HangupTask("PAD-OCC")
  27. } else {
  28. HangupTask("PA")
  29. HangupTask("PAD-OCC")
  30. HangupTask("CPA")
  31. HangupTask("EMG")
  32. HangupTask("SPC")
  33. HangupTask("DCS")
  34. HangupTask("STN")
  35. HangupTask("CHK")
  36. HangupTask("VOL")
  37. Hangup("2311") //hangup cabcab
  38. }
  39. }
  40. // Hangup 挂断指定分机或通道
  41. func Hangup(channel string) {
  42. lfshook.NewLogger().Infof("Hangup extensions/channel :%s", channel)
  43. if !utils.IsChannel(channel) {
  44. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  45. }
  46. action := map[string]string{
  47. "Action": "hangup",
  48. "Channel": channel,
  49. }
  50. //lfshook.NewLogger().Infof("hangup action :%+v", action)
  51. if _, _, err := AminInstance.Send(action); err != nil {
  52. lfshook.NewLogger().Errorf("Hangup %+v", err)
  53. }
  54. }
  55. // Hangup 挂断所有分机,除指定分机和PAD
  56. func HangupAllExcept(caller string) {
  57. //all PACU
  58. for _, ret := range Pacus {
  59. Hangup(ret)
  60. }
  61. switch caller {
  62. case "2311":
  63. Hangup("1411") //IO1
  64. Hangup("1481") //IO8
  65. //Hangup("2311") //ICP1
  66. Hangup("2381") //ICP8
  67. case "2381":
  68. Hangup("1411") //IO1
  69. Hangup("1481") //IO8
  70. Hangup("2311") //ICP1
  71. //Hangup("2381") //ICP8
  72. case "1411":
  73. //Hangup("1411") //IO1
  74. Hangup("1481") //IO8
  75. Hangup("2311") //ICP1
  76. Hangup("2381") //ICP8
  77. case "1481":
  78. Hangup("1411") //IO1
  79. //Hangup("1481") //IO8
  80. Hangup("2311") //ICP1
  81. Hangup("2381") //ICP8
  82. case "":
  83. Hangup("1411") //IO1
  84. Hangup("1481") //IO8
  85. Hangup("2311") //ICP1
  86. Hangup("2381") //ICP8
  87. }
  88. }
  89. // Hangup task
  90. func HangupTask(TaskName string) {
  91. lfshook.NewLogger().Infof("HangupTask Check TaskName:%s ", TaskName)
  92. if TaskName == "PAD-OCC" {
  93. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  94. if err != nil {
  95. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  96. return
  97. }
  98. //lfshook.NewLogger().Infof("============QueueStatus ret :%+v", resCaller)
  99. if resCaller != nil {
  100. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", resCaller.Entrys)
  101. if resCaller.Entrys != nil {
  102. for _, caller := range resCaller.Entrys {
  103. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  104. time.Sleep(time.Millisecond * 50)
  105. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  106. }
  107. }
  108. }
  109. }
  110. taskInfo, ok := priority.RegistryTask.Get(TaskName)
  111. if ok {
  112. HangupAllLocalChan()
  113. ConfbridgeKick(taskInfo.ConfbridgeID, "all")
  114. Hangup(taskInfo.RunChannel)
  115. }
  116. }
  117. // interrupt the running task
  118. func InterruptRunningTask(toRunTask string) string {
  119. utils.LoggerDebug.Printf("InterruptRunningTask TorunType:%s", toRunTask)
  120. var task priority.TaskInfo
  121. var taskName string
  122. var ok bool
  123. lfshook.NewLogger().Infof("InterruptRunningTask toRuntask:%s ", toRunTask)
  124. if toRunTask != "PA" && toRunTask != "PAD-ICP" && toRunTask != "PAD-TMS" { // ignore C2C
  125. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask1()
  126. if !ok {
  127. return ""
  128. }
  129. } else { // have to check C2C
  130. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask()
  131. if !ok {
  132. return ""
  133. }
  134. }
  135. /*if toRunTask == "C2C" { //当司机对讲打断业务为PA/PAD-ICP/PAD-TMS时,检测是否有EMG和CPA存在,存在则恢复播放
  136. if taskName == "PA" || taskName == "PAD-ICP" || taskName == "PAD-TMS" {
  137. //check resume
  138. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  139. if ok {
  140. if taskName == "EMG" {
  141. EMGConfbridgeReinvite(task.ConfbridgeID)
  142. } else if taskName == "CPA" {
  143. if CPAConfbridgeReinvite(task.ConfbridgeID) {
  144. alstatus.PaStatus("", "CPA", "start")
  145. }
  146. }
  147. }
  148. }
  149. }*/
  150. utils.LoggerDebug.Printf("InterruptRunningTask RunningTask:%+v", taskName)
  151. //lfshook.NewLogger().Infof("InterruptRunningTask RunningTask:%+v ", task)
  152. //same type return
  153. if toRunTask == taskName {
  154. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" || toRunTask == "PAD-OCC" {
  155. //Auto Answer PAD-OCC one by one, clean the old confbridge and channels before answer a new PAD .
  156. if toRunTask == "PAD-OCC" {
  157. Hangup(task.RunChannel) //pad
  158. ConfbridgeKick(task.ConfbridgeID, "all")
  159. HangupIO() //io
  160. //lfshook.NewLogger().Infof("===InterruptRunningTask=ret==== ")
  161. }
  162. return taskName
  163. }
  164. }
  165. //pad all reset
  166. if toRunTask == "AlarmHoldResetAll" {
  167. HangupAllLocalChan()
  168. return taskName
  169. }
  170. switch task.RunType {
  171. case "SPC":
  172. if toRunTask == "C2C" {
  173. HangupICP()
  174. //alstatus.PaStatus("", "SPC", "end")
  175. } else {
  176. ConfbridgeKick(task.ConfbridgeID, "all")
  177. alstatus.PaStatus("", "SPC", "end")
  178. }
  179. time.Sleep(time.Millisecond * 200)
  180. case "CHK":
  181. if toRunTask == "C2C" {
  182. HangupICP()
  183. //alstatus.PaStatus("", "CHK", "end")
  184. } else {
  185. ConfbridgeKick(task.ConfbridgeID, "all")
  186. alstatus.PaStatus("", "CHK", "end")
  187. }
  188. time.Sleep(time.Millisecond * 200)
  189. case "DCS":
  190. if toRunTask == "STN" {
  191. return taskName
  192. } else if toRunTask == "C2C" {
  193. HangupICP()
  194. //alstatus.PaStatus("", "DCS", "end")
  195. } else {
  196. ConfbridgeKick(task.ConfbridgeID, "all")
  197. alstatus.PaStatus("", "DCS", "end")
  198. }
  199. time.Sleep(time.Millisecond * 200)
  200. case "STN":
  201. if toRunTask == "DCS" {
  202. return taskName
  203. } else if toRunTask == "C2C" {
  204. HangupICP()
  205. //alstatus.PaStatus("", "STN", "end")
  206. } else {
  207. ConfbridgeKick(task.ConfbridgeID, "all")
  208. alstatus.PaStatus("", "STN", "end")
  209. }
  210. time.Sleep(time.Millisecond * 200)
  211. case "CPA":
  212. //kick CPA members
  213. if toRunTask != "C2C" {
  214. CPAConfbridgeKick(task.ConfbridgeID)
  215. alstatus.PaStatus("", "CPA", "end")
  216. } else if toRunTask == "C2C" {
  217. HangupICP()
  218. //alstatus.PaStatus("", "CPA", "end")
  219. }
  220. time.Sleep(time.Millisecond * 200)
  221. case "EMG":
  222. //kick EMG members
  223. if toRunTask == "EMG" {
  224. ConfbridgeKick(task.ConfbridgeID, "all")
  225. //alstatus.PaStatus("", "EMG", "end")
  226. } else if toRunTask != "C2C" {
  227. EMGConfbridgeKick(task.ConfbridgeID)
  228. //alstatus.PaStatus("", "EMG", "end")
  229. } else if toRunTask == "C2C" {
  230. HangupICP()
  231. //alstatus.PaStatus("", "EMG", "end")
  232. }
  233. time.Sleep(time.Millisecond * 200)
  234. case "C2C": // Interrupt C2C task running,
  235. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  236. HangupICP()
  237. return taskName
  238. }
  239. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  240. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  241. break
  242. }
  243. priority.InterruptedPad = "PAD-ICP"
  244. //lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
  245. chans, err := CoreShowChannels()
  246. if err != nil {
  247. lfshook.NewLogger().Infof("InterruptRunningTask CoreShowChannels err:%+v", err)
  248. return taskName
  249. }
  250. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  251. for _, ret := range chans {
  252. // Redirect the connected PAD to 0300
  253. if utils.IsPAIU(ret.CallerIDNum) {
  254. //lfshook.NewLogger().Infof("====interrupt PAD ==== %+v ", ret)
  255. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  256. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  257. if err != nil {
  258. lfshook.NewLogger().Infof("InterruptRunningTask Redirect err:%+v", err)
  259. return taskName
  260. }
  261. //lfshook.NewLogger().Infof("====interrupt PAD =1111=== %+v ", ret)
  262. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  263. active.NotifyPaiu(number, "hold")
  264. //HangupAllLocalChan()
  265. }
  266. }
  267. }
  268. for _, ret := range chans {
  269. //hangup pad call ICP channel
  270. if utils.IsPAIU(ret.CallerIDNum) {
  271. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  272. //lfshook.NewLogger().Infof("Hangup PAD Channel :%+v", ret.Channel)
  273. Hangup(ret.Channel)
  274. }
  275. }
  276. }
  277. alstatus.PaStatus("", "PAD", "end")
  278. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  279. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  280. //2. hangup task channel (ICP + PACU)
  281. //lfshook.NewLogger().Infof("===InterruptRunningTask=2. hangup task channel === ")
  282. HangupAllLocalChan()
  283. HangupICP()
  284. case "PAD-OCC": // Interrupt PAD-OCC task running,
  285. if toRunTask == "C2C" {
  286. HangupICP()
  287. break
  288. } else {
  289. priority.InterruptedPad = "PAD-OCC"
  290. priority.OCCAnswer = 0
  291. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  292. if err != nil {
  293. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  294. return taskName
  295. }
  296. if resCaller == nil {
  297. return taskName
  298. }
  299. if resCaller.Entrys != nil {
  300. for _, caller := range resCaller.Entrys {
  301. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  302. time.Sleep(time.Millisecond * 50)
  303. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  304. }
  305. }
  306. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  307. //2. Hangup connected PAD
  308. //lfshook.NewLogger().Infof("===InterruptRunningTask=Hangup connected PAD=== ")
  309. Hangup(task.RunChannel)
  310. //3. Hangup OI & ICP
  311. HangupIO()
  312. HangupAllLocalChan()
  313. ConfbridgeKick(task.ConfbridgeID, "all")
  314. //occ pad end
  315. if priority.PADOccStart == 1 {
  316. alstatus.OccPad("end")
  317. alstatus.PaStatus("", "PAD", "end")
  318. priority.PADOccStart = 0
  319. priority.OCCAnswer = 0
  320. }
  321. }
  322. default:
  323. lfshook.NewLogger().Infof("InterruptRunningTask goto default !")
  324. //if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  325. // Hangup(priority.RunningPATaskChan)
  326. //}
  327. if toRunTask != "C2C" && task.RunType != "PA" {
  328. Hangup(task.RunChannel)
  329. ConfbridgeKick(task.ConfbridgeID, "all")
  330. }
  331. }
  332. return taskName
  333. }
  334. // Hangup all ICP
  335. func HangupICP() {
  336. Hangup("2311") //ICP1
  337. Hangup("2381") //ICP8
  338. }
  339. // Hangup all IO
  340. func HangupIO() {
  341. Hangup("1411") //IO1
  342. Hangup("1481") //IO8
  343. }
  344. // Hangup all PACU
  345. func HangupAllPACU() {
  346. //all PACU
  347. for _, ret := range Pacus {
  348. Hangup(ret)
  349. }
  350. }
  351. func HangupAllLocalChan() {
  352. chans, err := CoreShowChannels()
  353. if err != nil {
  354. lfshook.NewLogger().Infof("CoreShowChannels %+v", err)
  355. return
  356. }
  357. for _, ret := range chans {
  358. if strings.Contains(ret.Channel, "Local") && !strings.Contains(ret.Channel, "0502@default") {
  359. //lfshook.NewLogger().Infof("HangupAllLocalChan=====hangup========= %+v", ret)
  360. Hangup(ret.Channel)
  361. }
  362. }
  363. }
  364. // Hangup all PACU
  365. func HangupAllPAD() {
  366. //all PAD
  367. for _, ret := range Pads {
  368. Hangup(ret)
  369. }
  370. }
  371. // Hangup all calls
  372. func HangupAll() {
  373. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  374. }
  375. // Dial 拨打号码
  376. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  377. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  378. action := map[string]string{
  379. "Action": "Originate",
  380. "Channel": chanel,
  381. "Exten": dst,
  382. "Context": dialrule,
  383. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  384. "Priority": "1",
  385. "Variable": fmt.Sprintf("CAB=%s", callType),
  386. "async": "true",
  387. }
  388. lfshook.NewLogger().Infof("================dial action %+v", action)
  389. res, _, err := AminInstance.Send(action)
  390. if err != nil {
  391. lfshook.NewLogger().Errorf("%+v", err)
  392. }
  393. lfshook.NewLogger().Info(res)
  394. }
  395. // Dial 拨打号码
  396. func DialICP(src, dst, dialrule, callerID, callerName string) {
  397. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  398. action := map[string]string{
  399. "Action": "Originate",
  400. "Channel": chanel,
  401. "Exten": dst,
  402. "Context": dialrule,
  403. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  404. "Priority": "1",
  405. "Variable": fmt.Sprintf("CBID=%s", callerID),
  406. "async": "true",
  407. }
  408. lfshook.NewLogger().Infof("dial action %+v", action)
  409. res, _, err := AminInstance.Send(action)
  410. if err != nil {
  411. lfshook.NewLogger().Errorf("%+v", err)
  412. }
  413. lfshook.NewLogger().Info(res)
  414. }
  415. // 获取分机状态
  416. func ExtenStatus(exten string) (Status string) {
  417. action := map[string]string{
  418. "Action": "ExtensionState",
  419. "Exten": exten,
  420. "Context": "default",
  421. }
  422. res, _, err := AminInstance.Send(action)
  423. if err != nil {
  424. lfshook.NewLogger().Errorf("%+v", err)
  425. return ""
  426. }
  427. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  428. return res["StatusText"]
  429. }
  430. // PACU ChanSpy
  431. func ChanSpy(src, dst string, whisper, bargein bool) {
  432. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  433. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  434. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  435. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  436. /*
  437. if whisper {
  438. data = fmt.Sprintf("%s,w", data)
  439. }
  440. if bargein {
  441. data = fmt.Sprintf("%s,B", data)
  442. }
  443. */
  444. action := map[string]string{
  445. "Action": "Originate",
  446. "Channel": channel, // 不存在的通话
  447. "Application": "ChanSpy",
  448. "Data": data, // 存在的通话
  449. "CallerID": dst,
  450. "Async": "true",
  451. }
  452. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  453. _, _, err := AminInstance.Send(action)
  454. if err != nil {
  455. lfshook.NewLogger().Errorf("%+v", err)
  456. }
  457. }
  458. // Redirect 转接
  459. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  460. //callerID := "redirect"
  461. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  462. if !utils.IsChannel(channel) {
  463. //callerID = channel
  464. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  465. return err
  466. }
  467. }
  468. action := map[string]string{
  469. "Action": "Redirect",
  470. "Channel": channel,
  471. "Exten": dst,
  472. "Context": dialrule,
  473. "CallerID": callerID,
  474. "Priority": "1",
  475. "async": "true",
  476. }
  477. lfshook.NewLogger().Infof("RedirectInQueue: %+v", action)
  478. res, _, err := AminInstance.Send(action)
  479. if err != nil {
  480. lfshook.NewLogger().Error(err)
  481. }
  482. lfshook.NewLogger().Info(res)
  483. return err
  484. }
  485. // Redirect 转接
  486. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  487. //callerID := "redirect"
  488. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  489. if !utils.IsChannel(channel) {
  490. callerID = channel
  491. if channel, err = GetChannelByExten(channel); err != nil {
  492. return err
  493. }
  494. }
  495. action := map[string]string{
  496. "Action": "Redirect",
  497. "Channel": channel,
  498. "Exten": dst,
  499. "Context": dialrule,
  500. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  501. "Priority": "1",
  502. "async": "true",
  503. }
  504. lfshook.NewLogger().Infof("Redirect: %+v", action)
  505. res, _, err := AminInstance.Send(action)
  506. if err != nil {
  507. lfshook.NewLogger().Error(err)
  508. }
  509. lfshook.NewLogger().Info(res)
  510. return err
  511. }
  512. func SetPadTimer() {
  513. //toRunPadpriority := priority.GetPriorityByKey(priority.InterruptedPad) //Get PAD priori 获取之前打断的报警优先级
  514. toRunPadpriority := priority.GetPriorityByKey("PAD-ICP")
  515. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  516. if ok {
  517. if taskTmp.Priority < toRunPadpriority { //higher priority task running ,do not set timer
  518. utils.LoggerDebug.Printf("PAD SetPadTimer runing priority:%d toRun priority:%d , return !", taskTmp.Priority, toRunPadpriority)
  519. return
  520. }
  521. }
  522. utils.LoggerDebug.Printf("PAD SetPadTimer check 0300")
  523. res, err := QueueStatus("0300", "")
  524. if err != nil {
  525. lfshook.NewLogger().Infof("QueueStatus err%+v", err)
  526. return
  527. }
  528. if res == nil {
  529. return
  530. }
  531. if res.Calls != "0" {
  532. utils.LoggerDebug.Printf("PAD SetPadTimer Set QueueTimer timeout 30s !")
  533. //active.SetTimer = true
  534. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  535. if active.QueueTimer != nil {
  536. if active.QueueTimer.Stop() {
  537. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  538. } else {
  539. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  540. }
  541. }
  542. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  543. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  544. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  545. //if both not active , return
  546. if active.ActivedCab == "" {
  547. return
  548. }
  549. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  550. if err != nil {
  551. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  552. return
  553. }
  554. if res == nil {
  555. return
  556. }
  557. if res.Calls == "0" { // OCC queue empty
  558. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  559. if err != nil {
  560. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  561. return
  562. }
  563. if resCaller.Entrys != nil {
  564. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  565. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  566. })
  567. for _, caller := range resCaller.Entrys {
  568. priority.ICPAnswer = 0
  569. //lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=")
  570. //lfshook.NewLogger().Infof("Q300==SetPadTimer==Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  571. //order by pos
  572. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  573. time.Sleep(time.Millisecond * 100) //200 ms delay
  574. }
  575. }
  576. //==============test info =====================
  577. /*
  578. time.Sleep(2 * time.Second)
  579. occque, err1 := QueueStatus("0301", "") // check ICP queue, get entries
  580. if err1 != nil {
  581. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  582. return
  583. }
  584. for _, caller := range occque.Entrys {
  585. lfshook.NewLogger().Infof("Q301==SetPadTimer==Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  586. }*/
  587. }
  588. })
  589. }
  590. }
  591. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  592. action := map[string]string{
  593. "Action": "ConfbridgeKick",
  594. "Conference": confnum,
  595. "Channel": channel,
  596. }
  597. res, _, err = AminInstance.Send(action)
  598. if err != nil {
  599. return nil, err
  600. }
  601. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  602. if res["Response"] != "Success" {
  603. return nil, errors.New(res["Message"])
  604. }
  605. return res, nil
  606. }
  607. func CPAConfbridgeKick(confnum string) (res map[string]string, err error) {
  608. utils.LoggerDebug.Printf("CPA CPAConfbridgeKick , kick all members .")
  609. chans, err := ConfbridgeList(confnum)
  610. if err != nil {
  611. return nil, errors.New(res["Message"])
  612. }
  613. for _, confChan := range chans {
  614. if !strings.Contains(confChan, "PJSIP/1481") {
  615. ConfbridgeKick(confnum, confChan)
  616. }
  617. }
  618. return res, nil
  619. }
  620. func CPAConfbridgeReinvite(confID string) bool {
  621. time.Sleep(time.Millisecond * 100)
  622. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , resume CPA .")
  623. /*if priority.TaskCreating == "" || priority.CABInterrupt == 1 { //高优先级被cabcab打断,或者高优先级直接挂断
  624. for _, ext := range Speakers {
  625. if utils.IsICP(ext) {
  626. if priority.CABInterrupt == 0 {
  627. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  628. continue
  629. }
  630. }
  631. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  632. }
  633. } else {
  634. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Interrupt by other ,return !")
  635. return false
  636. }
  637. return true*/
  638. if priority.PAInterrupt == 1 { // PA 打断标签判断
  639. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , PA ready to go , return !")
  640. return false
  641. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  642. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  643. if utils.IsICP(ext) {
  644. if priority.CABInterrupt == 1 {
  645. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  646. continue
  647. }
  648. }
  649. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  650. }
  651. } else if priority.TaskCreating != "" {
  652. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , other PA ready to go , return !")
  653. return false
  654. } else {
  655. for _, ext := range Speakers {
  656. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  657. }
  658. }
  659. return true
  660. }
  661. func EMGConfbridgeKick(confnum string) (res map[string]string, err error) {
  662. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , kick all members .")
  663. chans, err := ConfbridgeList(confnum)
  664. if err != nil {
  665. return nil, errors.New(res["Message"])
  666. }
  667. for _, confChan := range chans {
  668. if !strings.Contains(confChan, "0502@default") {
  669. ConfbridgeKick(confnum, confChan)
  670. }
  671. }
  672. return res, nil
  673. }
  674. func EMGConfbridgeReinvite(confID string) {
  675. time.Sleep(time.Millisecond * 100)
  676. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , resume EMG .")
  677. if priority.PAInterrupt == 1 { // PA 打断标签判断
  678. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , PA ready to go , return !")
  679. return
  680. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  681. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  682. if utils.IsICP(ext) {
  683. if priority.CABInterrupt == 1 {
  684. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get CABCAB Interrupt resume EMG , ignore %s continue .", ext)
  685. continue
  686. }
  687. }
  688. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  689. }
  690. } else if priority.TaskCreating != "" {
  691. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , other PA ready to go , return !")
  692. return
  693. } else {
  694. for _, ext := range Speakers {
  695. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  696. }
  697. }
  698. }
  699. func ConfbridgeList(confnum string) (chans []string, err error) {
  700. action := map[string]string{
  701. "Action": "ConfbridgeList",
  702. "Conference": confnum,
  703. }
  704. res, events, err := AminInstance.Send(action)
  705. if err != nil {
  706. return nil, err
  707. }
  708. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  709. if res["Response"] == "Success" {
  710. for _, event := range events {
  711. if event.Data["Event"] == "ConfbridgeList" {
  712. chans = append(chans, event.Data["Channel"])
  713. }
  714. }
  715. return chans, nil
  716. } else {
  717. return nil, errors.New(res["Message"])
  718. }
  719. }
  720. func ConfbridgeReinvite(src, context, confID string) {
  721. if ExtenStatus(src) != "Idle" {
  722. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  723. return
  724. }
  725. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  726. action := map[string]string{
  727. "Action": "Originate",
  728. "Channel": chanel,
  729. "Exten": "000",
  730. "Context": "confbridge-join",
  731. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  732. "Priority": "1",
  733. "Variable": fmt.Sprintf("CBID=%s", confID),
  734. "async": "true",
  735. }
  736. lfshook.NewLogger().Infof("dial action %+v", action)
  737. res, _, err := AminInstance.Send(action)
  738. if err != nil {
  739. lfshook.NewLogger().Errorf("%+v", err)
  740. }
  741. lfshook.NewLogger().Info(res)
  742. }
  743. func ICPConfbridgeReinvite(confID, paType string) {
  744. if priority.CABInterrupt == 1 || priority.CABInterrupt == 1 {
  745. utils.LoggerDebug.Printf("ICPConfbridgeReinvite, Get PA/CABCAB Interrupt resume %s faild .", paType)
  746. return
  747. }
  748. switch paType {
  749. case "PAD-OCC":
  750. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  751. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  752. case "CPA":
  753. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  754. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  755. case "EMG":
  756. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  757. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  758. case "SPC":
  759. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  760. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  761. case "STN":
  762. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  763. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  764. case "DCS":
  765. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  766. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  767. case "CHK":
  768. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  769. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  770. case "VOL":
  771. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  772. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  773. }
  774. }
  775. var waitMutex sync.Mutex
  776. // 设置全局变量控制任务创建过程,避免被其他任务打乱任务创建过程
  777. func WaitTaskCreate(task string, args ...string) { //arg1(task chan)
  778. utils.LoggerDebug.Printf("%s check task creating ..... ,TaskCreating = %s", task, priority.TaskCreating)
  779. waitMutex.Lock()
  780. defer waitMutex.Unlock()
  781. switch priority.TaskCreating {
  782. case "C2C":
  783. if /*task == "PA" ||*/ task == "CPA" {
  784. //获取正在创建的任务的优先级
  785. priorityC2C := priority.GetPriorityByKey("C2C")
  786. //获取将要创建的任务的优先级
  787. priorityTask := priority.GetPriorityByKey(task)
  788. //比较优先级,确定是否终止正在创建的任务
  789. if priorityC2C < priorityTask {
  790. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  791. //结束task(CPA)
  792. if len(args) > 0 {
  793. Hangup(args[0])
  794. goto DELAY
  795. }
  796. } else {
  797. //结束C2C,PA 则不需要挂断ICP(由ICP自行处理优先级)
  798. utils.LoggerDebug.Printf("%s check task C2C creating , hangup C2C ICPs ", task)
  799. HangupICP()
  800. goto DELAY
  801. }
  802. }
  803. return
  804. case "CPA":
  805. if task == "PA" /*|| task == "C2C"*/ {
  806. //获取正在创建的任务的优先级
  807. priorityCPA := priority.GetPriorityByKey("CPA")
  808. //获取将要创建的任务的优先级
  809. priorityTask := priority.GetPriorityByKey(task)
  810. //比较优先级,确定是否终止正在创建的任务
  811. if priorityCPA < priorityTask {
  812. utils.LoggerDebug.Printf("%s check task CPA creating , hangup %s %s", task, task, args[0])
  813. //结束task(PA,CPA)
  814. if len(args) > 0 {
  815. Hangup(args[0])
  816. goto DELAY
  817. }
  818. } else {
  819. //结束CPA,获取CPA通道
  820. if active.ActivedCab == "1" {
  821. utils.LoggerDebug.Printf("%s check task creating , hangup CPA %s ", task, args[0])
  822. Hangup("1481")
  823. goto DELAY
  824. } else if active.ActivedCab == "8" {
  825. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  826. Hangup("1411")
  827. goto DELAY
  828. }
  829. }
  830. }
  831. return
  832. case "PA":
  833. if task == "CPA" /*|| task == "C2C"*/ {
  834. //获取正在创建的任务的优先级
  835. priorityPA := priority.GetPriorityByKey("PA")
  836. //获取将要创建的任务的优先级
  837. priorityTask := priority.GetPriorityByKey(task)
  838. //比较优先级,确定是否终止正在创建的任务
  839. if priorityPA < priorityTask {
  840. utils.LoggerDebug.Printf("%s check task PA creating , hangup CPA %s ", task, args[0])
  841. //结束task(CPA)
  842. if len(args) > 0 {
  843. Hangup(args[0])
  844. goto DELAY
  845. }
  846. } else {
  847. //结束PA,获取PA通道
  848. if active.ActivedCab == "1" {
  849. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2311")
  850. Hangup("2311")
  851. goto DELAY
  852. } else if active.ActivedCab == "8" {
  853. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2381")
  854. Hangup("2381")
  855. goto DELAY
  856. }
  857. }
  858. }
  859. return
  860. default:
  861. //utils.LoggerDebug.Printf("%s waiting trd=============previous task:%s creating ..... ", task, priority.TaskCreating)
  862. for i := 0; i < 4; i++ {
  863. if priority.TaskCreating != "" {
  864. utils.LoggerDebug.Printf("%s waiting previous task:%s creating ..... ", task, priority.TaskCreating)
  865. time.Sleep(time.Millisecond * 500)
  866. } else {
  867. utils.LoggerDebug.Printf("TaskCreating is nill, Set TaskCreating=%s", task)
  868. priority.TaskCreating = task
  869. return
  870. }
  871. }
  872. priority.TaskCreating = task
  873. utils.LoggerDebug.Printf("%s waiting previous task:%s creating timeout ! Set TaskCreating=%s", task, priority.TaskCreating, task)
  874. return
  875. }
  876. DELAY:
  877. time.Sleep(100 * time.Millisecond)
  878. }