index.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. package action
  2. import (
  3. "pbx-api-gin/internal/app/stc/active"
  4. "pbx-api-gin/internal/app/stc/priority"
  5. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  6. "pbx-api-gin/internal/pkg/configs"
  7. "pbx-api-gin/pkg/lfshook"
  8. "pbx-api-gin/pkg/utils"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/sirupsen/logrus"
  14. "github.com/tqcenglish/amigo-go"
  15. "github.com/tqcenglish/amigo-go/pkg"
  16. )
  17. var AminInstance *amigo.Amigo
  18. func HandleAMI(event map[string]string) {
  19. //lfshook.NewLogger().Infof("===start======%s", event["Event"]
  20. switch event["Event"] {
  21. case "DTMFBegin": //ICP interrupt PAD
  22. if utils.IsICP(event["CallerIDNum"]) {
  23. exten := strings.Split(strings.Split(event["Channel"], "/")[1], "-")[0] //get ICP exten number
  24. if utils.IsICP(exten) {
  25. chans, err := CoreShowChannels()
  26. if err != nil {
  27. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  28. }
  29. //1. Redirect the connected PAD to 0300
  30. for _, ret := range chans {
  31. //lfshook.NewLogger().Infof("===HangupRunningTask=PAD1====Chans-ret===%+v==== ", ret)
  32. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) { //hangup pad call ICP channel
  33. //lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  34. Hangup(ret.Channel)
  35. } else if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  36. //lfshook.NewLogger().Infof("===Redirect=Chan===%+v==== ", ret.Channel)
  37. err := Redirect(ret.Channel, "0300", "default", exten, "PAD")
  38. if err != nil {
  39. lfshook.NewLogger().Errorf("Redirect %+v", err)
  40. }
  41. }
  42. }
  43. }
  44. }
  45. case "UserEvent": // RCD filename; PA;CPA; CabCab
  46. lfshook.NewLogger().Infof("========event:%s File:%s", event["Event"], event["FILENAME"])
  47. if event["UserEvent"] == "SetRecordFile" { //get record file name
  48. //延时一下1秒;
  49. time.Sleep(time.Second)
  50. //检测录音文件是否存在;
  51. //检测录音文件是否超过3min;
  52. //如果超过则切割;切割之后推送生成的文件名;
  53. alstatus.SendRecordFile(event["FILENAME"], event["RecordType"])
  54. } else if event["UserEvent"] == "CallType" && (event["Type"] == "PA" || event["Type"] == "CPA") { //PA start; check manual PA priority
  55. //PA & CPA interrupt others
  56. if utils.IsICP(event["CallerIDNum"]) {
  57. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  58. Hangup(event["CallerIDNum"])
  59. }
  60. if priority.CheckPriority("ManuPa") {
  61. //hangup others if priority is higher
  62. HangupRunningTask("PA") //PA interrupt other
  63. } else {
  64. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  65. }
  66. } else if utils.IsIO(event["CallerIDNum"]) { // CPA
  67. if priority.CheckPriority("CPA") {
  68. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  69. Hangup(event["CallerIDNum"])
  70. }
  71. //hangup others if priority is higher
  72. if priority.RunningType != "C2C" {
  73. HangupRunningTask("CPA") //CPA interrupt other
  74. } else {
  75. //call to ICP ; can not stop calling ICP
  76. lfshook.NewLogger().Info("==Running CabCab , CPA start call ICP(ICP is on the phone)=====")
  77. }
  78. } else {
  79. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  80. }
  81. }
  82. } else if event["UserEvent"] == "CallType" && event["Type"] == "C2C" { //CabCab start; check cab cab priority
  83. if priority.CheckPriority("CabCab") {
  84. if priority.RunningType == "PA" || priority.RunningType == "PAD-ICP" || priority.RunningType == "PAD-TMS" {
  85. HangupRunningTask("C2C") //C2C interrupt other
  86. } else {
  87. //Hangup the other ICP
  88. if event["CallerIDNum"] == "2311" {
  89. Hangup("2381")
  90. } else {
  91. Hangup("2311")
  92. }
  93. }
  94. } else { // hangup caller; C2C start failed
  95. Hangup(event["CallerIDNum"])
  96. }
  97. }
  98. case "Hangup":
  99. lfshook.NewLogger().Infof("=========%s", event["Event"])
  100. //OCC answer PAD, hangup, redirect the next PAD to OCC
  101. if utils.IsIO(event["CallerIDNum"]) && (event["ConnectedLineNum"] == "ano1" || event["ConnectedLineNum"] == "ano8") && event["Context"] == "default" {
  102. lfshook.NewLogger().Infof("====Hangup OCC-PAD=====%+v", event)
  103. // OCC hangup detected, hangup other running channels
  104. HangupAllLocalChan()
  105. Hangup(priority.RunningPATaskChan)
  106. res, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  107. if res.Calls == "0" { //OCC queue is empty
  108. priority.CleanPriorityTag()
  109. alstatus.OccPad("end")
  110. priority.OCCAnswer = 0
  111. priority.PADOccStart = 0
  112. if priority.ResumeEmgPara.FileName != "" {
  113. time.Sleep(time.Second)
  114. CheckEmgResume()
  115. }
  116. break
  117. } else { //OCC queue is not empty
  118. // HangupAllLocalChan()
  119. lfshook.NewLogger().Infof("====Start OCC-PAD===next==%+v", res)
  120. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  121. time.Sleep(time.Second)
  122. PADChan := ""
  123. for _, chanEntry := range res.Entrys {
  124. lfshook.NewLogger().Infof("====PAD answered by OCC1 pos:%s===chan:%s=", chanEntry.Position, chanEntry.Channel)
  125. if chanEntry.Position == "1" {
  126. PADChan = chanEntry.Channel
  127. break
  128. }
  129. }
  130. if PADChan != "" {
  131. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  132. alstatus.AlarmStatus(Ext, "connect")
  133. go RedirectInQueue(PADChan, "1411", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  134. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  135. } else {
  136. lfshook.NewLogger().Infof("===OCC-QueueStatus==PADCchan NULL")
  137. }
  138. break
  139. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  140. time.Sleep(time.Second)
  141. PADChan := ""
  142. for _, chanEntry := range res.Entrys {
  143. lfshook.NewLogger().Infof("====PAD answered by OCC1 pos:%s===chan:%s=", chanEntry.Position, chanEntry.Channel)
  144. if chanEntry.Position == "1" {
  145. PADChan = chanEntry.Channel
  146. break
  147. }
  148. }
  149. if PADChan != "" {
  150. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  151. alstatus.AlarmStatus(Ext, "connect")
  152. go RedirectInQueue(PADChan, "1481", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  153. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC1
  154. } else {
  155. lfshook.NewLogger().Infof("===OCC-QueueStatus==PADCchan NULL")
  156. }
  157. break
  158. }
  159. }
  160. }
  161. if utils.IsPAIU(event["CallerIDNum"]) { // PAD hangup, check if PAD all end, send PAD end status
  162. res, _ := QueueStatus("0300", "") // check ICP queue ,if empty PAD end
  163. res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  164. lfshook.NewLogger().Infof("==calls:%s===calls1:%s====", res.Calls, res1.Calls)
  165. if res.Calls == "0" && res1.Calls == "0" {
  166. //priority.CleanPriorityTag()
  167. //HangupAllLocalChan()
  168. if priority.PADStart == 1 {
  169. alstatus.PaStatus(event["CallerIDNum"], "PAD", "end")
  170. priority.PADStart = 0
  171. }
  172. /*if priority.ResumeEmgPara.FileName != "" {
  173. CheckEmgResume()
  174. }*/
  175. priority.ICPAnswer = 0
  176. priority.OCCAnswer = 0
  177. break
  178. }
  179. }
  180. case "QueueCallerJoin":
  181. lfshook.NewLogger().Infof("=========%s", event["Event"])
  182. if priority.OCCAnswer == 1 && event["Queue"] == "0300" { //New PAD Goto the OCC queue in the first time, if OCC answered
  183. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  184. go RedirectInQueue(event["CallerIDNum"], "0301", "queues-occ", event["CallerIDNum"])
  185. break
  186. }
  187. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) && event["Queue"] == "0300" { // Alarm join the queue, PAD in the queue
  188. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  189. ICPQueue, err := QueueStatus("0300", "") // check ICP queue, get entries
  190. if err != nil {
  191. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  192. return
  193. }
  194. if priority.ICPAnswer == 0 && ICPQueue.Calls == "1" { //ICP did not answer any first call to the ICP queue ; Ready to Set Occ Queue Timer
  195. active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  196. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  197. if err != nil {
  198. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  199. return
  200. }
  201. if res.Calls == "0" { // OCC queue empty
  202. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  203. if err != nil {
  204. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  205. return
  206. }
  207. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  208. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  209. })
  210. for _, caller := range resCaller.Entrys {
  211. priority.ICPAnswer = 0
  212. lfshook.NewLogger().Infof("====Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  213. //order by pos
  214. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  215. time.Sleep(time.Microsecond * 500) //200 ms delay
  216. }
  217. }
  218. })
  219. }
  220. break
  221. }
  222. //first PAD caller goto OCC
  223. //OCC dial PACUs;
  224. //PAD Page OCC+ICPs;
  225. if utils.IsPAIU(event["CallerIDNum"]) && event["Queue"] == "0301" && priority.OCCAnswer == 0 { // The first PAD to OCC ,caller is PAD
  226. if priority.CheckPriority("PAD-OCC") {
  227. HangupRunningTask("PAD-OCC") //PAD-OCC interrupt other
  228. priority.OCCAnswer = 1
  229. if active.ActivedCab == "1" /* && ExtenStatus("1411") == "Idle" */ { //check active and OCC status
  230. /*if priority.PADOccStart == 0 {
  231. alstatus.OccPad("start")
  232. priority.PADOccStart = 1
  233. if priority.PADStart == 0 {
  234. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  235. priority.PADStart = 1
  236. }
  237. }*/
  238. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  239. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  240. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  241. } else if active.ActivedCab == "8" /*&& ExtenStatus("1481") == "Idle" */ {
  242. /*if priority.PADOccStart == 0 {
  243. alstatus.OccPad("start")
  244. priority.PADOccStart = 1
  245. if priority.PADStart == 0 {
  246. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  247. priority.PADStart = 1
  248. }
  249. }*/
  250. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  251. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  252. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  253. }
  254. } else {
  255. lfshook.NewLogger().Infof("====PAD-OCC Priority false===")
  256. }
  257. }
  258. case "ConfbridgeJoin":
  259. lfshook.NewLogger().Infof("=========%+v", event["Event"])
  260. //set priority and send PA status msg
  261. switch event["CallerIDName"] {
  262. case "EMG":
  263. if event["Exten"] == "0502" {
  264. priority.RunningPATaskChan = event["Channel"]
  265. priority.RunningType = "EMG"
  266. //Pa status report
  267. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.EMG)
  268. alstatus.PaStatus("", "EMG", "start")
  269. return
  270. }
  271. case "SPC":
  272. if event["Exten"] == "0505" {
  273. priority.RunningPATaskChan = event["Channel"]
  274. priority.RunningType = "SPC"
  275. //Pa status report
  276. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.SPC)
  277. alstatus.PaStatus("", "SPC", "start")
  278. return
  279. }
  280. case "DCS":
  281. if event["Exten"] == "0504" {
  282. priority.RunningPATaskChan = event["Channel"]
  283. priority.RunningType = "DCS"
  284. //Pa status report
  285. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.DCS)
  286. alstatus.PaStatus("", "DCS", "start")
  287. return
  288. }
  289. case "STN":
  290. if event["Exten"] == "0503" {
  291. priority.RunningPATaskChan = event["Channel"]
  292. priority.RunningType = "STN"
  293. //Pa status report
  294. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.STN)
  295. alstatus.PaStatus("", "STN", "start")
  296. return
  297. }
  298. case "CHK":
  299. if event["Exten"] == "0510" {
  300. priority.RunningPATaskChan = event["Channel"]
  301. priority.RunningType = "CHK"
  302. //Pa status report
  303. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CHK)
  304. alstatus.PaStatus("", "CHK", "start")
  305. return
  306. }
  307. case "VOL": // tone-test
  308. if event["Exten"] == "0513" {
  309. priority.RunningPATaskChan = event["Channel"]
  310. priority.RunningType = "VOL"
  311. //Pa status report
  312. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.VOL)
  313. alstatus.PaStatus("", "VOL", "start")
  314. return
  315. }
  316. }
  317. //Send PA start msg to STC
  318. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA start
  319. lfshook.NewLogger().Infof("====PA status:%s=====", "start")
  320. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.ManuPa)
  321. alstatus.PaStatus(event["CallerIDNum"], "PA", "start")
  322. priority.RunningPATaskChan = event["Channel"]
  323. priority.RunningType = "PA"
  324. break
  325. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA start
  326. lfshook.NewLogger().Infof("====CPA status:%s=====", "start")
  327. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CPA)
  328. alstatus.PaStatus(event["CallerIDNum"], "CPA", "start")
  329. priority.RunningPATaskChan = event["Channel"]
  330. priority.RunningType = "CPA"
  331. return
  332. }
  333. //ICP answer PAD;PACUs connected ICP
  334. //PAD chanspy ICP1
  335. //ICP8 call PAD
  336. if event["ConnectedLineNum"] == "ani1" && event["Exten"] == "0511" { //PAD answered by ICP; PACUs connected ICP1
  337. lfshook.NewLogger().Infof("====PAD answered by ICP1:%s=====", event["ConnectedLineName"])
  338. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  339. priority.RunningPATaskChan = event["Channel"]
  340. priority.RunningType = "PAD-ICP"
  341. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADICP)
  342. go RedirectInQueue(event["ConnectedLineName"], "2311", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP1
  343. if ExtenStatus("2381") == "Idle" {
  344. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "8") // PAD call ICP8
  345. }
  346. }
  347. if event["ConnectedLineNum"] == "ani8" && event["Exten"] == "0511" { //PAD ansered by ICP8; PACUs connected ICP8
  348. lfshook.NewLogger().Infof("====PAD answered by ICP8:%s=====", event["ConnectedLineName"])
  349. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  350. priority.RunningPATaskChan = event["Channel"]
  351. priority.RunningType = "PAD-ICP"
  352. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADICP)
  353. go RedirectInQueue(event["ConnectedLineName"], "2381", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP8
  354. if ExtenStatus("2311") == "Idle" {
  355. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "1") // PAD call ICP1
  356. }
  357. break
  358. }
  359. //OCC answer PAD;Set the task channel
  360. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsIO(event["Exten"]) && event["Context"] == "pad-page-occ-icp" { //PAD Page OCC1+ICPs connected
  361. lfshook.NewLogger().Infof("====PAD answered by OCC:====")
  362. priority.RunningPATaskChan = event["Channel"]
  363. priority.RunningType = "PAD-OCC"
  364. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADOCC)
  365. break
  366. }
  367. case "ConfbridgeLeave":
  368. lfshook.NewLogger().Infof("=========%s", event["Event"])
  369. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA end
  370. lfshook.NewLogger().Infof("====PA status =====%s", "end")
  371. priority.CleanPriorityTag()
  372. alstatus.PaStatus(event["CallerIDNum"], "PA", "end")
  373. if priority.ResumeEmgPara.FileName != "" {
  374. CheckEmgResume()
  375. }
  376. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA end
  377. lfshook.NewLogger().Infof("====CPA status =====%s", "end")
  378. priority.CleanPriorityTag()
  379. alstatus.PaStatus(event["CallerIDNum"], "CPA", "end")
  380. if priority.ResumeEmgPara.FileName != "" {
  381. CheckEmgResume()
  382. }
  383. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  384. } else if event["CallerIDName"] == "EMG" && event["Exten"] == "0502" { // EMG broadcast hangup
  385. priority.CleanPriorityTag()
  386. alstatus.PaStatus(event["CallerIDName"], "EMG", "end")
  387. priority.ResumeEmgPara = priority.BroadcastResumeParas{}
  388. } else if event["CallerIDName"] == "STN" && event["Exten"] == "0503" {
  389. priority.CleanPriorityTag()
  390. alstatus.PaStatus(event["CallerIDName"], "STN", "end")
  391. if priority.ResumeEmgPara.FileName != "" {
  392. CheckEmgResume()
  393. }
  394. } else if event["CallerIDName"] == "DCS" && event["Exten"] == "0504" {
  395. priority.CleanPriorityTag()
  396. alstatus.PaStatus(event["CallerIDName"], "DCS", "end")
  397. if priority.ResumeEmgPara.FileName != "" {
  398. CheckEmgResume()
  399. }
  400. } else if event["CallerIDName"] == "SPC" && event["Exten"] == "0505" {
  401. priority.CleanPriorityTag()
  402. alstatus.PaStatus(event["CallerIDName"], "SPC", "end")
  403. if priority.ResumeEmgPara.FileName != "" {
  404. CheckEmgResume()
  405. }
  406. } else if event["CallerIDName"] == "CHK" && event["Exten"] == "0510" {
  407. priority.CleanPriorityTag()
  408. alstatus.PaStatus(event["CallerIDName"], "CHK", "end")
  409. if priority.ResumeEmgPara.FileName != "" {
  410. CheckEmgResume()
  411. }
  412. } else if event["CallerIDName"] == "VOL" && event["Exten"] == "0513" {
  413. priority.CleanPriorityTag()
  414. alstatus.PaStatus(event["CallerIDName"], "VOL", "end")
  415. if priority.ResumeEmgPara.FileName != "" {
  416. CheckEmgResume()
  417. }
  418. }
  419. case "DialEnd":
  420. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  421. //Cab Cab start
  422. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "ANSWER" {
  423. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CabCab)
  424. priority.RunningType = "C2C"
  425. alstatus.PaStatus(event["CallerIDNum"], "C2C", "start")
  426. }
  427. case "BridgeLeave":
  428. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  429. //Cab Cab end
  430. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["Exten"] == "0400" {
  431. priority.RunningTypePriority = 0
  432. priority.RunningType = ""
  433. alstatus.PaStatus(event["CallerIDNum"], "C2C", "end")
  434. }
  435. case "ExtensionStatus":
  436. //lfshook.NewLogger().Infof("=========event:%s Ext:%s status:%s ", event["Event"], event["Exten"], event["StatusText"])
  437. //update extension status
  438. if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
  439. if len(event["Exten"]) > 3 && utils.IsPAIU(event["Exten"]) {
  440. alstatus.AlarmStatus(event["Exten"], event["StatusText"]) // PAD idle + unavailable
  441. }
  442. }
  443. case "BridgeEnter": // TMS-ICP answer PAD; PACU connect ICP
  444. lfshook.NewLogger().Infof("=========event:%s callerid:%s", event["Event"], event["CallerIDNum"])
  445. if utils.IsIO(event["CallerIDNum"]) && utils.IsPAIU(event["ConnectedLineNum"]) {
  446. if priority.PADOccStart == 0 {
  447. alstatus.OccPad("start")
  448. priority.PADOccStart = 1
  449. if priority.PADStart == 0 {
  450. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  451. priority.PADStart = 1
  452. }
  453. }
  454. }
  455. if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected
  456. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  457. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  458. priority.RunningPATaskChan = event["Channel"]
  459. priority.RunningType = "PAD-TMS"
  460. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADTMS)
  461. if active.ActivedCab == "1" {
  462. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  463. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  464. } else if active.ActivedCab == "8" {
  465. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  466. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  467. }
  468. } else if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0405" { // PAD connect ICP-TMS;PACU not available
  469. priority.RunningPATaskChan = event["Channel"]
  470. priority.RunningType = "PAD-TMS"
  471. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADTMS)
  472. lfshook.NewLogger().Infof("====send pad status=====")
  473. alstatus.AlarmStatus(event["CallerIDNum"], "connect") // PAD connect ICP-TMS
  474. }
  475. }
  476. }
  477. func StartAMI(connectOKCallBack func(), handleEvents []func(event map[string]string)) {
  478. lfshook.NewLogger().Info("Start AMI")
  479. settings := &amigo.Settings{
  480. Host: configs.ConfigGlobal.AsteriskAMIHost,
  481. Port: configs.ConfigGlobal.AsteriskAMIPort,
  482. Username: configs.ConfigGlobal.AsteriskAMIUser,
  483. Password: configs.ConfigGlobal.AsteriskAMISecret,
  484. LogLevel: logrus.ErrorLevel}
  485. lfshook.NewLogger().Infof("ami setting: %+v", settings)
  486. AminInstance = amigo.New(settings, lfshook.NewLogger())
  487. AminInstance.EventOn(func(payload ...interface{}) {
  488. // lfshook.NewLogger().Infof("ami event on %+v", payload[0])
  489. event := payload[0].(map[string]string)
  490. go HandleAMI(event)
  491. for _, handle := range handleEvents {
  492. go handle(event)
  493. }
  494. })
  495. AminInstance.ConnectOn(func(payload ...interface{}) {
  496. lfshook.NewLogger().Infof("ami connect on %+v", payload[0])
  497. if payload[0] == pkg.Connect_OK {
  498. connectOKCallBack()
  499. } else {
  500. lfshook.NewLogger().Errorf("ami connect failure %+v", payload)
  501. }
  502. })
  503. AminInstance.Connect()
  504. }
  505. func Connected() bool {
  506. if AminInstance != nil {
  507. return AminInstance.Connected()
  508. }
  509. return false
  510. }