index.go 21 KB

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