index.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. package action
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "pbx-api-gin/internal/app/stc/active"
  7. "pbx-api-gin/internal/app/stc/priority"
  8. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  9. "pbx-api-gin/internal/pkg/configs"
  10. "pbx-api-gin/pkg/lfshook"
  11. "pbx-api-gin/pkg/utils"
  12. "sort"
  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. switch event["Event"] {
  23. case "DTMFBegin": //ICP interrupt PAD
  24. lfshook.NewLogger().Infof("=========%s====caller:%s=====digit:%s==", event["Event"], event["CallerIDNum"], event["Digit"])
  25. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] != "" {
  26. //PA interrupt PAD
  27. switch event["Digit"] {
  28. case "#":
  29. //lfshook.NewLogger().Infof("===PA interrupt PAD====== ")
  30. utils.LoggerDebug.Printf("Get DTMF '#', PA interrupt other !")
  31. runningTaskName := InterruptRunningTask("PA")
  32. if runningTaskName != "" {
  33. time.Sleep(time.Millisecond * 200) //wait endpoint release
  34. }
  35. case "*": //Cab cab interrupt PAD
  36. //lfshook.NewLogger().Infof("===Cab cab interrupt PAD and====== ")
  37. utils.LoggerDebug.Printf("Get DTMF '*', CabCab interrupt other !")
  38. runningTaskName := InterruptRunningTask("C2C")
  39. //Hangup("2311")
  40. //Hangup("2381")
  41. if runningTaskName != "" {
  42. time.Sleep(time.Millisecond * 100) //wait endpoint release
  43. }
  44. }
  45. }
  46. case "UserEvent": // RCD filename; PA;CPA; CabCab
  47. lfshook.NewLogger().Infof("========event:%s File:%s", event["Event"], event["FILENAME"])
  48. if event["UserEvent"] == "CONTINUE" && event["Type"] != "" {
  49. utils.LoggerDebug.Printf("Get UserEvent Type=CONTINUE , Send %s continue status !", event["Type"])
  50. alstatus.PaStatus("", event["Type"], "continue")
  51. return
  52. }
  53. if event["UserEvent"] == "CallType" && (event["Type"] == "PA" || event["Type"] == "CPA") { //PA start; check manual PA priority
  54. //PA & CPA interrupt others
  55. if utils.IsICP(event["CallerIDNum"]) { //PA
  56. utils.LoggerDebug.Printf("Get UserEvent , %s calling !", event["Type"])
  57. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  58. Hangup(event["CallerIDNum"])
  59. }
  60. WaitTaskCreate("PA", event["CallerIDNum"])
  61. if priority.CheckPriority("ManuPa") {
  62. //hangup others if priority is higher
  63. //lfshook.NewLogger().Infof("UserEvent event :PA start")
  64. utils.LoggerDebug.Printf("PA calling, check priority return true !")
  65. runningTaskName := InterruptRunningTask("PA") //PA interrupt other
  66. if runningTaskName != "" {
  67. time.Sleep(time.Millisecond * 100) //wait endpoint release
  68. }
  69. } else {
  70. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  71. }
  72. time.Sleep(2 * time.Second)
  73. if priority.TaskCreating == "PA" {
  74. priority.TaskCreating = ""
  75. }
  76. } else if utils.IsIO(event["CallerIDNum"]) { // CPA
  77. utils.LoggerDebug.Printf("Get UserEvent , %s calling !", event["Type"])
  78. WaitTaskCreate("CPA", event["CallerIDNum"])
  79. if priority.CheckPriority("CPA") {
  80. //lfshook.NewLogger().Infof("========event:%s ========devide:%d", event["Event"], active.TrainDevide)
  81. utils.LoggerDebug.Printf("CPA calling, check priority return true !")
  82. if active.TrainDevide == 1 || active.RadioFault == 1 { // 列车没有断开的情况下限制如下;列车断开之后CPA无任何限制
  83. utils.LoggerDebug.Printf("TrainDevide = %d, RadioFault = %d , CPA calling unlimit !", active.TrainDevide, active.RadioFault)
  84. //lfshook.NewLogger().Infof("===active.TrainDevide == 1 || active.RadioFault == 1====")
  85. } else {
  86. utils.LoggerDebug.Printf("CPA calling , ActivedCab = %s !", active.ActivedCab)
  87. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  88. Hangup(event["CallerIDNum"])
  89. } else if active.ActivedCab == "1" && event["CallerIDNum"] == "1411" {
  90. Hangup(event["CallerIDNum"])
  91. } else if active.ActivedCab == "8" && event["CallerIDNum"] == "1481" {
  92. Hangup(event["CallerIDNum"])
  93. }
  94. }
  95. //hangup others if priority is higher
  96. runningTaskName := InterruptRunningTask("CPA") //CPA interrupt other
  97. if runningTaskName != "" {
  98. time.Sleep(time.Millisecond * 100) //wait endpoint release
  99. }
  100. } else {
  101. utils.LoggerDebug.Printf("CPA calling failed , check priority return false !")
  102. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  103. }
  104. time.Sleep(2 * time.Second)
  105. if priority.TaskCreating == "CPA" {
  106. priority.TaskCreating = ""
  107. }
  108. }
  109. } else if event["UserEvent"] == "CallType" && event["Type"] == "C2C" { //CabCab start; check cab cab priority
  110. utils.LoggerDebug.Printf("Get UserEvent , %s calling !", event["Type"])
  111. WaitTaskCreate("C2C", event["CallerIDNum"])
  112. if priority.CheckPriority("CabCab") { // interrupt OCC-PAD
  113. //C2C start PAD interrupt
  114. taskName, _, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  115. lfshook.NewLogger().Infof("HighestPriorityRunningTask get task :%+v", taskName)
  116. utils.LoggerDebug.Printf("C2C calling, check priority return true !")
  117. if ok {
  118. if taskName == "PAD-ICP" || taskName == "PAD-TMS" || taskName == "PA" {
  119. utils.LoggerDebug.Printf("%s Start CabCab ,interrupt other !", event["CallerIDNum"])
  120. runningTaskName := InterruptRunningTask("C2C")
  121. if runningTaskName != "" {
  122. time.Sleep(time.Millisecond * 100) //wait endpoint release
  123. }
  124. } else {
  125. //lfshook.NewLogger().Infof("CabCab hangup other the one caller %s", event["CallerIDNum"])
  126. utils.LoggerDebug.Printf("%s Start CabCab ,check and hangup the other ICP first", event["CallerIDNum"])
  127. if event["CallerIDNum"] == "2311" {
  128. Hangup("2381")
  129. } else {
  130. Hangup("2311")
  131. }
  132. }
  133. }
  134. } else { // hangup caller; C2C start failed
  135. //lfshook.NewLogger().Infof("CabCab hangup caller %s", event["CallerIDNum"])
  136. utils.LoggerDebug.Printf("C2C calling failed , check priority return false !")
  137. Hangup(event["CallerIDNum"])
  138. }
  139. time.Sleep(2 * time.Second)
  140. if priority.TaskCreating == "C2C" {
  141. priority.TaskCreating = ""
  142. }
  143. break
  144. //Get record file name ,encode and upload
  145. } else if event["UserEvent"] == "SetRecordFile" {
  146. if len(event["FILENAME"]) > 0 {
  147. if configs.ConfigGlobal.ProcessRecord != "yes" {
  148. break
  149. }
  150. //检测录音文件是否存在;最多检测5次,每次间隔1秒
  151. var fileExists bool
  152. for i := 0; i < 5; i++ {
  153. time.Sleep(time.Second) // 等待1秒
  154. if _, err := os.Stat(event["FILENAME"]); err == nil {
  155. fileExists = true
  156. //lfshook.NewLogger().Infof("File found: %s", event["FILENAME"])
  157. break
  158. } else if os.IsNotExist(err) {
  159. //lfshook.NewLogger().Infof("File not found (attempt %d): %s", i+1, event["FILENAME"])
  160. } else {
  161. //lfshook.NewLogger().Infof("Error checking file: %v", err)
  162. }
  163. }
  164. if !fileExists { //5秒内没有生成录音文件
  165. //lfshook.NewLogger().Infof("File %s not found after 5 attempts", event["FILENAME"])
  166. utils.LoggerDebug.Printf("Record File %s not found after 5 attempts", event["FILENAME"])
  167. break
  168. }
  169. } else {
  170. return
  171. }
  172. //获取录音文件时长,检测录音文件是否超过3min;
  173. duration, err := utils.GetDuration(event["FILENAME"])
  174. if err != nil {
  175. utils.Logger.Printf("%s Get duration err: %+v", event["FILENAME"], err)
  176. break
  177. }
  178. //lfshook.NewLogger().Infof("==========duration===== %d", duration)
  179. //转wav文件的采样率到22kHz,并切割位180秒每段
  180. var FileNames []string
  181. if duration >= 2000 { //超过2000秒的超长文件,不处理
  182. //lfshook.NewLogger().Infof("Audio file time length over 2000 seconds, Ignored !")
  183. utils.LoggerDebug.Printf("Audio file time length over 2000 seconds, Ignored !")
  184. break
  185. } else if duration < 2000 { //小于2000秒文件进行转换和切割
  186. FileNames, err = utils.ConvertAndSegmentWAV(event["FILENAME"], strings.Replace(event["FILENAME"], ".wav", "", -1))
  187. if err != nil {
  188. // lfshook.NewLogger().Infof("%s Get duration err: %+v", event["FILENAME"], err)
  189. utils.LoggerDebug.Printf("Split and convert record file failed : %+v !", err)
  190. break
  191. }
  192. utils.LoggerDebug.Printf("Audio file split to file :%+v ", FileNames)
  193. }
  194. //执行加密操作,并将录音信息写入日志文件
  195. DstFile := ""
  196. if len(FileNames) > 0 { // 文件切割之后进入循环处理
  197. for _, filepathFull := range FileNames {
  198. file := strings.Replace(filepathFull, ".wav", "", -1)
  199. DstFile = fmt.Sprintf("%s-encrypted.wav", file)
  200. err = utils.AudioFileEncode(DstFile, filepathFull)
  201. if err != nil {
  202. //lfshook.NewLogger().Infof("Encode file: %s err: %+v", DstFile, err)
  203. utils.LoggerDebug.Printf("Encrypt file %s err: %+v", DstFile, err)
  204. continue
  205. }
  206. //切割&加密之后发送生成的文件名到STC;
  207. alstatus.SendRecordFile(DstFile, event["RecordType"])
  208. trainInfo = fmt.Sprintf("TrainNumber %s ", active.TrainNum)
  209. utils.LoggerDebug.Printf("Write record log !")
  210. if strings.Contains(event["FILENAME"], "PAD") {
  211. _, caller, callee := utils.GetPadInfo(event["FILENAME"])
  212. carNum := int(caller[2] - '0')
  213. trainInfo = fmt.Sprintf("TrainNumber %s CarNumber %s ", active.TrainNum, active.TrainInfoMap[active.TrainNum][carNum])
  214. if len(caller) == 4 && len(caller) > 0 {
  215. utils.Logger.Printf("Train Information: %s, MessageType: PAD , LocationCode: %c, Connected: %s, RecordFileName:%s", trainInfo, caller[3], callee, DstFile)
  216. } else {
  217. utils.Logger.Printf("Train Information: %s, MessageType: PAD , RecordFileName:%s . Can not get caller and callee !", trainInfo, DstFile)
  218. }
  219. } else if strings.Contains(event["FILENAME"], "C2C") {
  220. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  221. utils.Logger.Printf("Train Information: %s , MessageType: CabCab, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  222. } else if strings.Contains(event["FILENAME"], "CPA") {
  223. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  224. utils.Logger.Printf("Train Information: %s , MessageType: CPA, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  225. } else if strings.Contains(event["FILENAME"], "EMG") {
  226. utils.Logger.Printf("Train Information: %s , MessageType: EMG, RecordFileName: %s", trainInfo, DstFile)
  227. } else if strings.Contains(event["FILENAME"], "STN") {
  228. utils.Logger.Printf("Train Information: %s , MessageType: STN, RecordFileName: %s", trainInfo, DstFile)
  229. } else if strings.Contains(event["FILENAME"], "DCS") {
  230. utils.Logger.Printf("Train Information: %s , MessageType: DCS, RecordFileName: %s", trainInfo, DstFile)
  231. } else if strings.Contains(event["FILENAME"], "SPC") {
  232. utils.Logger.Printf("Train Information: %s , MessageType: SPC, RecordFileName: %s", trainInfo, DstFile)
  233. } else if strings.Contains(event["FILENAME"], "CHK") {
  234. utils.Logger.Printf("Train Information: %s , MessageType: Self Check, RecordFileName: %s", trainInfo, DstFile)
  235. } else if strings.Contains(event["FILENAME"], "TONE") {
  236. utils.Logger.Printf("Train Information: %s , MessageType: TONE Test, RecordFileName: %s", trainInfo, DstFile)
  237. } else if strings.Contains(event["FILENAME"], "PA") {
  238. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  239. utils.Logger.Printf("Train Information: %s , MessageType: PA, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  240. }
  241. }
  242. //Get path & generate recording XML file
  243. path := filepath.Dir(event["FILENAME"])
  244. rcdFileName := filepath.Base(event["FILENAME"])
  245. xmlFileName := strings.Replace(rcdFileName, "wav", "xml", 1)
  246. xmlFilePath := path + "/" + xmlFileName
  247. //lfshook.NewLogger().Infof("Train info= xml path============%s===============", xmlFilePath)
  248. utils.LoggerDebug.Printf("Write record info xml : %s !", xmlFilePath)
  249. err := active.GenerateXML(xmlFilePath)
  250. if err != nil {
  251. //lfshook.NewLogger().Infof("Generate recording XML err:%+v", err)
  252. utils.LoggerDebug.Printf("Write record info xml : %s ERR: %+v ", xmlFilePath, err)
  253. break
  254. }
  255. } else {
  256. //lfshook.NewLogger().Infof("No files to upload!!!")
  257. break
  258. }
  259. }
  260. case "Hangup":
  261. //lfshook.NewLogger().Infof("%s", event["Event"])
  262. //OCC answer PAD, hangup, redirect the next PAD to OCC
  263. if utils.IsIO(event["CallerIDNum"]) && (event["ConnectedLineNum"] == "ano1" || event["ConnectedLineNum"] == "ano8") && event["Context"] == "default" {
  264. utils.LoggerDebug.Printf("Check PAD-OCC queue if there are PADs need to be redirect to OCC !")
  265. res, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  266. if res == nil {
  267. utils.LoggerDebug.Printf("Check PAD-OCC queue , return nill . exit !")
  268. return
  269. }
  270. if res.Calls == "0" { //OCC queue is empty
  271. utils.LoggerDebug.Printf("Check PAD-OCC queue no PAD now ! Send PAD-OCC end to STC .")
  272. alstatus.OccPad("end")
  273. priority.OCCAnswer = 0
  274. priority.PADOccStart = 0
  275. /*//clean confbridge
  276. taskTmp, _ := priority.RegistryTask.Get("PAD-OCC")
  277. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  278. //check resume
  279. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  280. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  281. if ok {
  282. if taskName == "EMG" {
  283. EMGConfbridgeReinvite(task.ConfbridgeID)
  284. } else if taskName == "CPA" {
  285. CPAConfbridgeReinvite(task.ConfbridgeID)
  286. }
  287. }
  288. */
  289. //break
  290. } else { //OCC queue is not empty
  291. utils.LoggerDebug.Printf("PAD goto OCC , interrupt other task !")
  292. // OCC hangup detected, hangup other running channels
  293. runningTaskName := InterruptRunningTask("PAD-OCC")
  294. if runningTaskName != "" {
  295. time.Sleep(time.Millisecond * 100) //wait endpoint release
  296. }
  297. //time.Sleep(time.Millisecond * 100) //wait io idle
  298. //lfshook.NewLogger().Infof("====Start OCC-PAD===next==%+v", res)
  299. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  300. time.Sleep(time.Second)
  301. PADChan := ""
  302. if res.Entrys != nil {
  303. for _, chanEntry := range res.Entrys {
  304. lfshook.NewLogger().Infof("PAD answered by OCC1 pos:%s chan:%s", chanEntry.Position, chanEntry.Channel)
  305. if chanEntry.Position == "1" {
  306. PADChan = chanEntry.Channel
  307. utils.LoggerDebug.Printf("PAD-OCC , Get the first PAD(%s) in the queue !", PADChan)
  308. break
  309. }
  310. }
  311. if PADChan != "" {
  312. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  313. alstatus.AlarmStatus(Ext, "connect")
  314. utils.LoggerDebug.Printf("PAD goto OCC1 , redirect %s to OCC1 !", PADChan)
  315. go RedirectInQueue(PADChan, "1411", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  316. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  317. } else {
  318. lfshook.NewLogger().Infof("OCC-QueueStatus PADCchan NULL")
  319. utils.LoggerDebug.Printf("PAD-OCC , OCC Queue Status PADCchan return NULL .")
  320. }
  321. }
  322. break
  323. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  324. time.Sleep(time.Second)
  325. PADChan := ""
  326. if res.Entrys != nil {
  327. for _, chanEntry := range res.Entrys {
  328. lfshook.NewLogger().Infof("PAD answered by OCC1 pos:%s chan:%s", chanEntry.Position, chanEntry.Channel)
  329. if chanEntry.Position == "1" {
  330. PADChan = chanEntry.Channel
  331. utils.LoggerDebug.Printf("PAD-OCC , Get the first PAD(%s) in the queue !", PADChan)
  332. break
  333. }
  334. }
  335. if PADChan != "" {
  336. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  337. alstatus.AlarmStatus(Ext, "connect")
  338. utils.LoggerDebug.Printf("PAD goto OCC8 , redirect %s to OCC8 !", PADChan)
  339. go RedirectInQueue(PADChan, "1481", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  340. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC1
  341. } else {
  342. lfshook.NewLogger().Infof("OCC QueueStatus PADCchan NULL")
  343. }
  344. }
  345. break
  346. }
  347. }
  348. }
  349. if utils.IsPAIU(event["CallerIDNum"]) { // PAD hangup, check if PAD all end, send PAD end status
  350. number := strings.Split(strings.Split(event["Channel"], "-")[0], "/")[1]
  351. //lfshook.NewLogger().Infof("===hangup PAD =======================%s", number)
  352. if utils.IsPAIU(number) {
  353. //clean confbridge & clean task info
  354. //PAD-ICP
  355. taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
  356. if ok {
  357. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  358. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  359. }
  360. // PAD-TMS
  361. taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
  362. if ok {
  363. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  364. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  365. }
  366. //PAD-OCC
  367. taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
  368. if ok {
  369. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  370. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  371. }
  372. //Check PAD END
  373. res, _ := QueueStatus("0300", "") // check ICP queue ,if empty PAD end
  374. res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  375. if res == nil || res1 == nil {
  376. utils.LoggerDebug.Printf("PAD Hangup , check ICP|OCC queue return nil, exit.")
  377. return
  378. }
  379. lfshook.NewLogger().Infof("===Hangup PAD====== ICP Queue calls:%s OCC Queue calls:%s", res.Calls, res1.Calls)
  380. if res.Calls == "0" && res1.Calls == "0" {
  381. if priority.PADStart == 1 {
  382. //SetPadTimer()
  383. alstatus.PaStatus("", "PAD", "end")
  384. priority.PADStart = 0
  385. //priority.PADTMSStart = 0
  386. /*
  387. //clean confbridge
  388. //PAD-ICP
  389. taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
  390. if ok {
  391. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  392. }
  393. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  394. // PAD-TMS
  395. taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
  396. if ok {
  397. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  398. }
  399. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  400. //PAD-OCC
  401. taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
  402. if ok {
  403. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  404. }
  405. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  406. */
  407. //check resume
  408. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  409. if ok {
  410. if taskName == "EMG" {
  411. EMGConfbridgeReinvite(task.ConfbridgeID)
  412. alstatus.PaStatus("", "EMG", "start")
  413. } else if taskName == "CPA" {
  414. CPAConfbridgeReinvite(task.ConfbridgeID)
  415. alstatus.PaStatus("", "CPA", "start")
  416. }
  417. }
  418. }
  419. priority.ICPAnswer = 0
  420. priority.OCCAnswer = 0
  421. break
  422. }
  423. }
  424. }
  425. case "QueueCallerJoin":
  426. lfshook.NewLogger().Infof("=========%s", event["Event"])
  427. if priority.OCCAnswer == 1 && event["Queue"] == "0300" { //New PAD Goto the OCC queue in the first time, if OCC answered
  428. utils.LoggerDebug.Printf("Redirect PAD %s to OCC queue .", event["CallerIDNum"])
  429. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  430. go RedirectInQueue(event["CallerIDNum"], "0301", "queues-occ", event["CallerIDNum"])
  431. break
  432. }
  433. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) && event["Queue"] == "0300" { // Alarm join the queue, PAD in the queue
  434. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  435. ICPQueue, err := QueueStatus("0300", "") // check ICP queue, get entries
  436. if err != nil {
  437. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  438. return
  439. }
  440. if ICPQueue == nil {
  441. return
  442. }
  443. if priority.ICPAnswer == 0 && ICPQueue.Calls == "1" { //ICP did not answer any first call to the ICP queue ; Ready to Set Occ Queue Timer
  444. toRunpriority := priority.GetPriorityByKey("PAD-ICP")
  445. time.Sleep(time.Millisecond * 500) //wait high priority task start
  446. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  447. if ok {
  448. //lfshook.NewLogger().Infof("=1==QueueCallerJoin===runing:%d=====toRun:=%d==Status:%s", taskTmp, toRunpriority, event["ChannelStateDesc"])
  449. if taskTmp.Priority < toRunpriority { //higher priority task running ,do not set timer
  450. utils.LoggerDebug.Printf("The First PAD %s enter ICP queue , %s running do not set PAD timeout.", taskTmp.RunType, event["CallerIDNum"])
  451. break
  452. }
  453. }
  454. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  455. if active.QueueTimer != nil {
  456. if active.QueueTimer.Stop() {
  457. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  458. } else {
  459. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  460. }
  461. }
  462. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  463. utils.LoggerDebug.Printf("The first PAD %s enter ICP queue , set PAD timeout %d sec .", event["CallerIDNum"], active.PADTimeout)
  464. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  465. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  466. //if both not active , return
  467. if active.ActivedCab == "" {
  468. return
  469. }
  470. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  471. if err != nil {
  472. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  473. return
  474. }
  475. if res == nil {
  476. return
  477. }
  478. if res.Calls == "0" { // OCC queue empty
  479. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  480. if err != nil {
  481. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  482. return
  483. }
  484. if resCaller.Entrys != nil {
  485. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  486. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  487. })
  488. for _, caller := range resCaller.Entrys {
  489. priority.ICPAnswer = 0
  490. //lfshook.NewLogger().Infof("====QueueCallerJoin==QueueTimer===%s", event["Event"])
  491. //lfshook.NewLogger().Infof("Redirect to 0301 extension:%s Pos:%s", caller.CallerIDNum, caller.Position)
  492. //order by pos
  493. utils.LoggerDebug.Printf("Redirect %s to OCC queue Pos:%s", caller.CallerIDNum, caller.Position)
  494. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  495. time.Sleep(time.Millisecond * 100) //200 ms delay
  496. }
  497. }
  498. }
  499. })
  500. }
  501. break
  502. }
  503. //first PAD caller goto OCC
  504. //OCC dial PACUs;
  505. //PAD Page OCC+ICPs;
  506. if utils.IsPAIU(event["CallerIDNum"]) && event["Queue"] == "0301" && priority.OCCAnswer == 0 { // The first PAD to OCC ,caller is PAD
  507. utils.LoggerDebug.Printf("The first PAD to OCC queue .")
  508. WaitTaskCreate("PAD-OCC")
  509. if priority.CheckPriority("PAD-OCC") {
  510. runningTaskName := InterruptRunningTask("PAD-OCC") //PAD-OCC interrupt other
  511. if runningTaskName != "" {
  512. time.Sleep(time.Millisecond * 100) //wait endpoint release
  513. }
  514. priority.OCCAnswer = 1
  515. time.Sleep(time.Millisecond * 300)
  516. //if active.TrainDevide == 0 { //列车没有断开
  517. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  518. utils.LoggerDebug.Printf("PAD %s goto OCC1 .", event["CallerIDNum"])
  519. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  520. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  521. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  522. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  523. utils.LoggerDebug.Printf("PAD %s goto OCC8 .", event["CallerIDNum"])
  524. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  525. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  526. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  527. }
  528. /*} else { //列车断开模式下
  529. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  530. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  531. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  532. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  533. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  534. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  535. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  536. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  537. }
  538. }*/
  539. } else {
  540. lfshook.NewLogger().Infof("====PAD-OCC Priority false===")
  541. }
  542. time.Sleep(2 * time.Second)
  543. if priority.TaskCreating == "PAD-OCC" {
  544. priority.TaskCreating = ""
  545. }
  546. }
  547. case "ConfbridgeJoin":
  548. lfshook.NewLogger().Infof("=========%+v", event["Event"])
  549. //lfshook.NewLogger().Infof("=========%+v", event)
  550. //set priority and send PA status msg
  551. switch event["CallerIDName"] {
  552. case "EMG":
  553. if event["Exten"] == "0502" {
  554. //================================
  555. task := priority.TaskInfo{
  556. RunChannel: event["Channel"],
  557. LocalChan: event["Channel"],
  558. RunType: "EMG",
  559. Priority: priority.AllTasks.EMG.Priority,
  560. ConfbridgeID: event["Conference"],
  561. Running: true,
  562. }
  563. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  564. priority.RegistryTask.Register("EMG", task)
  565. if priority.TaskCreating == "EMG" {
  566. priority.TaskCreating = ""
  567. }
  568. alstatus.PaStatus("", "EMG", "start")
  569. //check PAD timer
  570. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.EMG.Priority {
  571. if active.QueueTimer != nil {
  572. if active.QueueTimer.Stop() {
  573. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  574. } else {
  575. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  576. }
  577. }
  578. }
  579. return
  580. }
  581. case "SPC":
  582. if event["Exten"] == "0505" {
  583. //================================
  584. task := priority.TaskInfo{
  585. RunChannel: event["Channel"],
  586. LocalChan: event["Channel"],
  587. RunType: "SPC",
  588. Priority: priority.AllTasks.SPC.Priority,
  589. ConfbridgeID: event["Conference"],
  590. Running: true,
  591. }
  592. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  593. priority.RegistryTask.Register("SPC", task)
  594. alstatus.PaStatus("", "SPC", "start")
  595. if priority.TaskCreating == "SPC" {
  596. priority.TaskCreating = ""
  597. }
  598. //check PAD timer
  599. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.SPC.Priority {
  600. if active.QueueTimer != nil {
  601. if active.QueueTimer.Stop() {
  602. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  603. } else {
  604. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  605. }
  606. }
  607. }
  608. return
  609. }
  610. case "DCS":
  611. if event["Exten"] == "0504" {
  612. //================================
  613. task := priority.TaskInfo{
  614. RunChannel: event["Channel"],
  615. LocalChan: event["Channel"],
  616. RunType: "DCS",
  617. Priority: priority.AllTasks.DCS.Priority,
  618. ConfbridgeID: event["Conference"],
  619. Running: true,
  620. }
  621. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  622. priority.RegistryTask.Register("DCS", task)
  623. alstatus.PaStatus("", "DCS", "start")
  624. if priority.TaskCreating == "DCS" {
  625. priority.TaskCreating = ""
  626. }
  627. //check PAD timer
  628. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.DCS.Priority {
  629. if active.QueueTimer != nil {
  630. if active.QueueTimer.Stop() {
  631. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  632. } else {
  633. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  634. }
  635. }
  636. }
  637. return
  638. }
  639. case "STN":
  640. if event["Exten"] == "0503" {
  641. //================================
  642. task := priority.TaskInfo{
  643. RunChannel: event["Channel"],
  644. LocalChan: event["Channel"],
  645. RunType: "STN",
  646. Priority: priority.AllTasks.STN.Priority,
  647. ConfbridgeID: event["Conference"],
  648. Running: true,
  649. }
  650. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  651. priority.RegistryTask.Register("STN", task)
  652. alstatus.PaStatus("", "STN", "start")
  653. if priority.TaskCreating == "STN" {
  654. priority.TaskCreating = ""
  655. }
  656. //check PAD timer
  657. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.STN.Priority {
  658. if active.QueueTimer != nil {
  659. if active.QueueTimer.Stop() {
  660. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  661. } else {
  662. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  663. }
  664. }
  665. }
  666. return
  667. }
  668. case "CHK":
  669. if event["Exten"] == "0510" {
  670. //================================
  671. task := priority.TaskInfo{
  672. RunChannel: event["Channel"],
  673. LocalChan: event["Channel"],
  674. RunType: "CHK",
  675. Priority: priority.AllTasks.CHK.Priority,
  676. ConfbridgeID: event["Conference"],
  677. Running: true,
  678. }
  679. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  680. priority.RegistryTask.Register("CHK", task)
  681. alstatus.PaStatus("", "CHK", "start")
  682. if priority.TaskCreating == "CHK" {
  683. priority.TaskCreating = ""
  684. }
  685. //check PAD timer
  686. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.CHK.Priority {
  687. if active.QueueTimer != nil {
  688. if active.QueueTimer.Stop() {
  689. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  690. } else {
  691. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  692. }
  693. }
  694. }
  695. return
  696. }
  697. case "VOL": // tone-test
  698. if event["Exten"] == "0513" {
  699. //================================
  700. task := priority.TaskInfo{
  701. RunChannel: event["Channel"],
  702. LocalChan: event["Channel"],
  703. RunType: "VOL",
  704. Priority: priority.AllTasks.VOL.Priority,
  705. ConfbridgeID: event["Conference"],
  706. Running: true,
  707. }
  708. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  709. priority.RegistryTask.Register("VOL", task)
  710. alstatus.PaStatus("", "VOL", "start")
  711. if priority.TaskCreating == "VOL" {
  712. priority.TaskCreating = ""
  713. }
  714. //check PAD timer
  715. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.VOL.Priority {
  716. if active.QueueTimer != nil {
  717. if active.QueueTimer.Stop() {
  718. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  719. } else {
  720. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  721. }
  722. }
  723. }
  724. return
  725. }
  726. }
  727. /*
  728. //get confbridge id for join back to confbridge
  729. if event["Context"] == "pad-page-occ-icp" && utils.IsPAIU(event["CallerIDNum"]) {
  730. //active.ICPCONBID = event["Conference"] //pad-occ
  731. priority.AllTasks.PADOCC.ConfbridgeID = event["Conference"]
  732. }
  733. */
  734. //Send PA start msg to STC
  735. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA start
  736. alstatus.PaStatus(event["CallerIDNum"], "PA", "start")
  737. //================================
  738. task := priority.TaskInfo{
  739. RunChannel: event["Channel"],
  740. LocalChan: event["Channel"],
  741. RunType: "PA",
  742. Priority: priority.AllTasks.PA.Priority,
  743. ConfbridgeID: event["Conference"],
  744. Running: true,
  745. }
  746. utils.LoggerDebug.Printf("PA Runing , Set PA task info %+v .", task)
  747. priority.RegistryTask.Register("PA", task)
  748. if priority.TaskCreating == "PA" {
  749. priority.TaskCreating = ""
  750. }
  751. //check PAD timer
  752. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.PA.Priority {
  753. if active.QueueTimer != nil {
  754. if active.QueueTimer.Stop() {
  755. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  756. } else {
  757. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  758. }
  759. }
  760. }
  761. break
  762. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA start
  763. alstatus.PaStatus(event["CallerIDNum"], "CPA", "start")
  764. //================================
  765. task := priority.TaskInfo{
  766. RunChannel: event["Channel"],
  767. LocalChan: event["Channel"],
  768. RunType: "CPA",
  769. Priority: priority.AllTasks.CPA.Priority,
  770. ConfbridgeID: event["Conference"],
  771. Running: true,
  772. }
  773. utils.LoggerDebug.Printf("CPA Runing , Set CPA task info %+v .", task)
  774. priority.RegistryTask.Register("CPA", task)
  775. utils.LoggerDebug.Printf("CPA Runing , Set CPA task info %+v .", priority.RegistryTask.ListAll())
  776. if priority.TaskCreating == "CPA" {
  777. priority.TaskCreating = ""
  778. }
  779. //check PAD timer
  780. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.CPA.Priority {
  781. if active.QueueTimer != nil {
  782. if active.QueueTimer.Stop() {
  783. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  784. } else {
  785. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  786. }
  787. }
  788. }
  789. return
  790. }
  791. //ICP answer PAD;PACUs connected ICP
  792. //PAD chanspy ICP1
  793. //ICP8 call PAD
  794. if event["ConnectedLineNum"] == "ani1" && event["Exten"] == "0511" { //PAD answered by ICP; PACUs connected ICP1
  795. //lfshook.NewLogger().Infof("====PAD answered by ICP1:%s=====", event["ConnectedLineName"])
  796. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  797. //================================
  798. task := priority.TaskInfo{
  799. RunChannel: event["Channel"],
  800. LocalChan: event["Channel"],
  801. RunType: "PAD-ICP",
  802. Priority: priority.AllTasks.PADICP.Priority,
  803. ConfbridgeID: event["Conference"],
  804. Running: true,
  805. }
  806. priority.RegistryTask.Register("PAD-ICP", task)
  807. if priority.TaskCreating == "PAD-ICP" {
  808. priority.TaskCreating = ""
  809. }
  810. utils.LoggerDebug.Printf("PAD %s answered by ICP1 , set task info :%+v .", event["ConnectedLineName"], task)
  811. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  812. go RedirectInQueue(event["ConnectedLineName"], "2311", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP1
  813. if ExtenStatus("2381") == "Idle" {
  814. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "8") // PAD call ICP8
  815. }
  816. }
  817. if event["ConnectedLineNum"] == "ani8" && event["Exten"] == "0511" { //PAD ansered by ICP8; PACUs connected ICP8
  818. lfshook.NewLogger().Infof("====PAD answered by ICP8:%s=====", event["ConnectedLineName"])
  819. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  820. //================================
  821. task := priority.TaskInfo{
  822. RunChannel: event["Channel"],
  823. LocalChan: event["Channel"],
  824. RunType: "PAD-ICP",
  825. Priority: priority.AllTasks.PADICP.Priority,
  826. ConfbridgeID: event["Conference"],
  827. Running: true,
  828. }
  829. priority.RegistryTask.Register("PAD-ICP", task)
  830. if priority.TaskCreating == "PAD-ICP" {
  831. priority.TaskCreating = ""
  832. }
  833. utils.LoggerDebug.Printf("PAD %s answered by ICP8 , set task info :%+v .", event["ConnectedLineName"], task)
  834. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  835. go RedirectInQueue(event["ConnectedLineName"], "2381", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP8
  836. if ExtenStatus("2311") == "Idle" {
  837. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "1") // PAD call ICP1
  838. }
  839. break
  840. }
  841. //OCC answer PAD;Set the task channel
  842. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsIO(event["Exten"]) && event["Context"] == "pad-page-occ-icp" { //PAD Page OCC1+ICPs connected
  843. lfshook.NewLogger().Infof("====PAD answered by OCC:====")
  844. //================================
  845. task := priority.TaskInfo{
  846. RunChannel: event["Channel"],
  847. LocalChan: event["Channel"],
  848. RunType: "PAD-OCC",
  849. Priority: priority.AllTasks.PADOCC.Priority,
  850. ConfbridgeID: event["Conference"],
  851. Running: true,
  852. }
  853. priority.RegistryTask.Register("PAD-OCC", task)
  854. if priority.TaskCreating == "PAD-OCC" {
  855. priority.TaskCreating = ""
  856. }
  857. utils.LoggerDebug.Printf("PAD %s answered by OCC , set task info :%+v .", event["CallerIDNum"], task)
  858. break
  859. }
  860. case "ConfbridgeLeave":
  861. lfshook.NewLogger().Infof("=========%s", event["Event"])
  862. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA end
  863. alstatus.PaStatus(event["CallerIDNum"], "PA", "end")
  864. priority.RegistryTask.StopAndUnregister("PA")
  865. SetPadTimer()
  866. // check resume
  867. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  868. if ok {
  869. if taskName == "EMG" {
  870. EMGConfbridgeReinvite(task.ConfbridgeID)
  871. alstatus.PaStatus("", "EMG", "start")
  872. } else if taskName == "CPA" {
  873. CPAConfbridgeReinvite(task.ConfbridgeID)
  874. alstatus.PaStatus("", "CPA", "start")
  875. }
  876. }
  877. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA end
  878. alstatus.PaStatus(event["CallerIDNum"], "CPA", "end")
  879. //CPA
  880. taskTmp, ok := priority.RegistryTask.Get("CPA")
  881. if ok {
  882. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  883. }
  884. priority.RegistryTask.StopAndUnregister("CPA")
  885. SetPadTimer()
  886. //check resume
  887. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  888. if ok {
  889. if taskName == "EMG" {
  890. EMGConfbridgeReinvite(task.ConfbridgeID)
  891. alstatus.PaStatus("", "EMG", "start")
  892. }
  893. }
  894. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  895. } else if event["CallerIDName"] == "EMG" && event["Exten"] == "0502" { // EMG broadcast hangup
  896. alstatus.PaStatus("", "EMG", "end")
  897. //EMG
  898. taskTmp, ok := priority.RegistryTask.Get("EMG")
  899. if ok {
  900. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  901. }
  902. priority.RegistryTask.StopAndUnregister("EMG")
  903. SetPadTimer()
  904. //check resume
  905. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  906. if ok {
  907. if taskName == "CPA" {
  908. CPAConfbridgeReinvite(task.ConfbridgeID)
  909. alstatus.PaStatus("", "CPA", "start")
  910. }
  911. }
  912. } else if event["CallerIDName"] == "STN" && event["Exten"] == "0503" {
  913. alstatus.PaStatus("", "STN", "end")
  914. //STN
  915. taskTmp, ok := priority.RegistryTask.Get("STN")
  916. if ok {
  917. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  918. }
  919. priority.RegistryTask.StopAndUnregister("STN")
  920. SetPadTimer()
  921. //check resume
  922. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  923. if ok {
  924. if taskName == "CPA" {
  925. CPAConfbridgeReinvite(task.ConfbridgeID)
  926. alstatus.PaStatus("", "CPA", "start")
  927. } else if taskName == "EMG" {
  928. EMGConfbridgeReinvite(task.ConfbridgeID)
  929. alstatus.PaStatus("", "EMG", "start")
  930. }
  931. }
  932. } else if event["CallerIDName"] == "DCS" && event["Exten"] == "0504" {
  933. alstatus.PaStatus("", "DCS", "end")
  934. //DSC
  935. taskTmp, ok := priority.RegistryTask.Get("DCS")
  936. if ok {
  937. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  938. }
  939. priority.RegistryTask.StopAndUnregister("DCS")
  940. SetPadTimer()
  941. //check resume
  942. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  943. if ok {
  944. if taskName == "CPA" {
  945. CPAConfbridgeReinvite(task.ConfbridgeID)
  946. alstatus.PaStatus("", "CPA", "start")
  947. } else if taskName == "EMG" {
  948. EMGConfbridgeReinvite(task.ConfbridgeID)
  949. alstatus.PaStatus("", "EMG", "start")
  950. //alstatus.PaStatus("", "EMG", "start")
  951. }
  952. }
  953. } else if event["CallerIDName"] == "SPC" && event["Exten"] == "0505" {
  954. alstatus.PaStatus("", "SPC", "end")
  955. //SPC
  956. taskTmp, ok := priority.RegistryTask.Get("SPC")
  957. if ok {
  958. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  959. }
  960. priority.RegistryTask.StopAndUnregister("SPC")
  961. SetPadTimer()
  962. //check resume
  963. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  964. if ok {
  965. if taskName == "CPA" {
  966. CPAConfbridgeReinvite(task.ConfbridgeID)
  967. alstatus.PaStatus("", "CPA", "start")
  968. } else if taskName == "EMG" {
  969. EMGConfbridgeReinvite(task.ConfbridgeID)
  970. alstatus.PaStatus("", "EMG", "start")
  971. }
  972. }
  973. } else if event["CallerIDName"] == "CHK" && event["Exten"] == "0510" {
  974. alstatus.PaStatus("", "CHK", "end")
  975. //CHK
  976. taskTmp, ok := priority.RegistryTask.Get("CHK")
  977. if ok {
  978. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  979. }
  980. priority.RegistryTask.StopAndUnregister("CHK")
  981. SetPadTimer()
  982. //check resume
  983. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  984. if ok {
  985. if taskName == "CPA" {
  986. CPAConfbridgeReinvite(task.ConfbridgeID)
  987. alstatus.PaStatus("", "CPA", "start")
  988. } else if taskName == "EMG" {
  989. EMGConfbridgeReinvite(task.ConfbridgeID)
  990. alstatus.PaStatus("", "EMG", "start")
  991. }
  992. }
  993. } else if event["CallerIDName"] == "VOL" && event["Exten"] == "0513" {
  994. alstatus.PaStatus("", "VOL", "end")
  995. //VOL
  996. taskTmp, ok := priority.RegistryTask.Get("VOL")
  997. if ok {
  998. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  999. }
  1000. priority.RegistryTask.StopAndUnregister("VOL")
  1001. SetPadTimer()
  1002. //check resume
  1003. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1004. if ok {
  1005. if taskName == "CPA" {
  1006. CPAConfbridgeReinvite(task.ConfbridgeID)
  1007. alstatus.PaStatus("", "CPA", "start")
  1008. } else if taskName == "EMG" {
  1009. EMGConfbridgeReinvite(task.ConfbridgeID)
  1010. alstatus.PaStatus("", "EMG", "start")
  1011. }
  1012. }
  1013. }
  1014. case "DialEnd":
  1015. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1016. //Cab Cab start
  1017. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0400" && event["DialStatus"] == "ANSWER" {
  1018. //================================
  1019. task := priority.TaskInfo{
  1020. RunChannel: event["Channel"],
  1021. LocalChan: event["Channel"],
  1022. RunType: "C2C",
  1023. Priority: priority.AllTasks.C2C.Priority,
  1024. ConfbridgeID: "",
  1025. Running: true,
  1026. }
  1027. priority.RegistryTask.Register("C2C", task)
  1028. alstatus.PaStatus(event["CallerIDNum"], "C2C", "start")
  1029. if priority.TaskCreating == "C2C" {
  1030. priority.TaskCreating = ""
  1031. }
  1032. //check PAD timer
  1033. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.C2C.Priority {
  1034. if active.QueueTimer != nil {
  1035. if active.QueueTimer.Stop() {
  1036. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  1037. } else {
  1038. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  1039. }
  1040. }
  1041. }
  1042. } else if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "NOANSWER" {
  1043. //lfshook.NewLogger().Infof("===%s======%s", event["Event"], event["DialStatus"])
  1044. time.Sleep(time.Millisecond * 300)
  1045. //check resume
  1046. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1047. if ok {
  1048. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1049. }
  1050. }
  1051. //TMS Answer PAD (有时asterisk反馈的BridgeEnter事件有问题,根据dialend判断)
  1052. if utils.IsPAIU(event["CallerIDName"]) && utils.IsPACU(event["CallerIDNum"]) && event["DialStatus"] == "ANSWER" && event["Exten"] == "0403" {
  1053. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1054. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1055. //=============================
  1056. task := priority.TaskInfo{
  1057. RunChannel: event["Channel"],
  1058. LocalChan: event["Channel"],
  1059. RunType: "PAD-TMS",
  1060. Priority: priority.AllTasks.PADTMS.Priority,
  1061. ConfbridgeID: "",
  1062. Running: true,
  1063. }
  1064. priority.RegistryTask.Register("PAD-TMS", task)
  1065. if priority.TaskCreating == "PAD-TMS" {
  1066. priority.TaskCreating = ""
  1067. }
  1068. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1069. if active.ActivedCab == "1" {
  1070. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1071. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1072. } else if active.ActivedCab == "8" {
  1073. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1074. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1075. } else if active.ActivedCab == "" {
  1076. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1077. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1078. }
  1079. }
  1080. case "BridgeLeave":
  1081. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1082. //Cab Cab end
  1083. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["Exten"] == "0400" {
  1084. alstatus.PaStatus(event["CallerIDNum"], "C2C", "end")
  1085. priority.RegistryTask.StopAndUnregister("C2C")
  1086. time.Sleep(time.Millisecond * 300)
  1087. SetPadTimer()
  1088. //check resume
  1089. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1090. if ok {
  1091. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1092. //check resume
  1093. /*if taskName == "CPA" {
  1094. alstatus.PaStatus("", "CPA", "start")
  1095. } else if taskName == "EMG" {
  1096. alstatus.PaStatus("", "EMG", "start")
  1097. } else if taskName == "SPC" {
  1098. alstatus.PaStatus("", "SPC", "start")
  1099. } else if taskName == "DCS" {
  1100. alstatus.PaStatus("", "DCS", "start")
  1101. } else if taskName == "STN" {
  1102. alstatus.PaStatus("", "STN", "start")
  1103. } else if taskName == "CHK" {
  1104. alstatus.PaStatus("", "CHK", "start")
  1105. }*/
  1106. }
  1107. }
  1108. case "ExtensionStatus":
  1109. //lfshook.NewLogger().Infof("=========event:%s Ext:%s status:%s ", event["Event"], event["Exten"], event["StatusText"])
  1110. //update extension status
  1111. if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
  1112. if len(event["Exten"]) > 3 && utils.IsPAIU(event["Exten"]) {
  1113. alstatus.AlarmStatus(event["Exten"], event["StatusText"]) // PAD idle + unavailable
  1114. }
  1115. }
  1116. case "BridgeEnter": // TMS-ICP answer PAD; PACU connect ICP
  1117. lfshook.NewLogger().Infof("=========event:%s callerid-num:%s callerid-name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1118. //PAD-OCC start
  1119. if utils.IsIO(event["CallerIDNum"]) && utils.IsPAIU(event["ConnectedLineNum"]) {
  1120. if priority.PADOccStart == 0 {
  1121. if priority.TaskCreating == "PAD-OCC" {
  1122. priority.TaskCreating = ""
  1123. }
  1124. alstatus.OccPad("start")
  1125. priority.PADOccStart = 1
  1126. }
  1127. if priority.PADStart == 0 {
  1128. alstatus.PaStatus("", "PAD", "start")
  1129. priority.PADStart = 1
  1130. }
  1131. }
  1132. /*if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected -----TMS Answer PAD
  1133. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1134. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1135. //=============================
  1136. task := priority.TaskInfo{
  1137. RunChannel: event["Channel"],
  1138. LocalChan: event["Channel"],
  1139. RunType: "PAD-TMS",
  1140. Priority: priority.AllTasks.PADTMS.Priority,
  1141. ConfbridgeID: "",
  1142. Running: true,
  1143. }
  1144. priority.RegistryTask.Register("PAD-TMS", task)
  1145. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1146. if active.ActivedCab == "1" {
  1147. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1148. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1149. } else if active.ActivedCab == "8" {
  1150. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1151. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1152. } else if active.ActivedCab == "" {
  1153. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1154. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1155. }
  1156. } else*/if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0405" { // PAD connect ICP-TMS;PACU not available
  1157. //=============================
  1158. task := priority.TaskInfo{
  1159. RunChannel: event["Channel"],
  1160. LocalChan: event["Channel"],
  1161. RunType: "PAD-TMS",
  1162. Priority: priority.AllTasks.PADTMS.Priority,
  1163. ConfbridgeID: "",
  1164. Running: true,
  1165. }
  1166. priority.RegistryTask.Register("PAD-TMS", task)
  1167. if priority.TaskCreating == "PAD-TMS" {
  1168. priority.TaskCreating = ""
  1169. }
  1170. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1171. alstatus.AlarmStatus(event["CallerIDNum"], "connect") // PAD connect ICP-TMS
  1172. }
  1173. }
  1174. }
  1175. func StartAMI(connectOKCallBack func(), handleEvents []func(event map[string]string)) {
  1176. lfshook.NewLogger().Info("Start AMI")
  1177. settings := &amigo.Settings{
  1178. Host: configs.ConfigGlobal.AsteriskAMIHost,
  1179. Port: configs.ConfigGlobal.AsteriskAMIPort,
  1180. Username: configs.ConfigGlobal.AsteriskAMIUser,
  1181. Password: configs.ConfigGlobal.AsteriskAMISecret,
  1182. LogLevel: logrus.ErrorLevel}
  1183. //lfshook.NewLogger().Infof("ami setting: %+v", settings)
  1184. AminInstance = amigo.New(settings, lfshook.NewLogger())
  1185. AminInstance.EventOn(func(payload ...interface{}) {
  1186. // lfshook.NewLogger().Infof("ami event on %+v", payload[0])
  1187. event := payload[0].(map[string]string)
  1188. go HandleAMI(event)
  1189. for _, handle := range handleEvents {
  1190. go handle(event)
  1191. }
  1192. })
  1193. AminInstance.ConnectOn(func(payload ...interface{}) {
  1194. //lfshook.NewLogger().Infof("ami connect on %+v", payload[0])
  1195. if payload[0] == pkg.Connect_OK {
  1196. connectOKCallBack()
  1197. } else {
  1198. lfshook.NewLogger().Errorf("ami connect failure %+v", payload)
  1199. //active.Master = false
  1200. }
  1201. })
  1202. AminInstance.Connect()
  1203. }
  1204. func Connected() bool {
  1205. if AminInstance != nil {
  1206. return AminInstance.Connected()
  1207. }
  1208. return false
  1209. }