index.go 26 KB

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