index.go 26 KB

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