index.go 27 KB

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