index.go 27 KB

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