index.go 26 KB

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