index.go 21 KB

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