index.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. if priority.TaskCreating == "CPA" {
  776. priority.TaskCreating = ""
  777. }
  778. //check PAD timer
  779. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.CPA.Priority {
  780. if active.QueueTimer != nil {
  781. if active.QueueTimer.Stop() {
  782. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  783. } else {
  784. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  785. }
  786. }
  787. }
  788. return
  789. }
  790. //ICP answer PAD;PACUs connected ICP
  791. //PAD chanspy ICP1
  792. //ICP8 call PAD
  793. if event["ConnectedLineNum"] == "ani1" && event["Exten"] == "0511" { //PAD answered by ICP; PACUs connected ICP1
  794. //lfshook.NewLogger().Infof("====PAD answered by ICP1:%s=====", event["ConnectedLineName"])
  795. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  796. //================================
  797. task := priority.TaskInfo{
  798. RunChannel: event["Channel"],
  799. LocalChan: event["Channel"],
  800. RunType: "PAD-ICP",
  801. Priority: priority.AllTasks.PADICP.Priority,
  802. ConfbridgeID: event["Conference"],
  803. Running: true,
  804. }
  805. priority.RegistryTask.Register("PAD-ICP", task)
  806. if priority.TaskCreating == "PAD-ICP" {
  807. priority.TaskCreating = ""
  808. }
  809. utils.LoggerDebug.Printf("PAD %s answered by ICP1 , set task info :%+v .", event["ConnectedLineName"], task)
  810. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  811. go RedirectInQueue(event["ConnectedLineName"], "2311", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP1
  812. if ExtenStatus("2381") == "Idle" {
  813. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "8") // PAD call ICP8
  814. }
  815. }
  816. if event["ConnectedLineNum"] == "ani8" && event["Exten"] == "0511" { //PAD ansered by ICP8; PACUs connected ICP8
  817. lfshook.NewLogger().Infof("====PAD answered by ICP8:%s=====", event["ConnectedLineName"])
  818. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  819. //================================
  820. task := priority.TaskInfo{
  821. RunChannel: event["Channel"],
  822. LocalChan: event["Channel"],
  823. RunType: "PAD-ICP",
  824. Priority: priority.AllTasks.PADICP.Priority,
  825. ConfbridgeID: event["Conference"],
  826. Running: true,
  827. }
  828. priority.RegistryTask.Register("PAD-ICP", task)
  829. if priority.TaskCreating == "PAD-ICP" {
  830. priority.TaskCreating = ""
  831. }
  832. utils.LoggerDebug.Printf("PAD %s answered by ICP8 , set task info :%+v .", event["ConnectedLineName"], task)
  833. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  834. go RedirectInQueue(event["ConnectedLineName"], "2381", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP8
  835. if ExtenStatus("2311") == "Idle" {
  836. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "1") // PAD call ICP1
  837. }
  838. break
  839. }
  840. //OCC answer PAD;Set the task channel
  841. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsIO(event["Exten"]) && event["Context"] == "pad-page-occ-icp" { //PAD Page OCC1+ICPs connected
  842. lfshook.NewLogger().Infof("====PAD answered by OCC:====")
  843. //================================
  844. task := priority.TaskInfo{
  845. RunChannel: event["Channel"],
  846. LocalChan: event["Channel"],
  847. RunType: "PAD-OCC",
  848. Priority: priority.AllTasks.PADOCC.Priority,
  849. ConfbridgeID: event["Conference"],
  850. Running: true,
  851. }
  852. priority.RegistryTask.Register("PAD-OCC", task)
  853. if priority.TaskCreating == "PAD-OCC" {
  854. priority.TaskCreating = ""
  855. }
  856. utils.LoggerDebug.Printf("PAD %s answered by OCC , set task info :%+v .", event["CallerIDNum"], task)
  857. break
  858. }
  859. case "ConfbridgeLeave":
  860. lfshook.NewLogger().Infof("=========%s", event["Event"])
  861. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA end
  862. alstatus.PaStatus(event["CallerIDNum"], "PA", "end")
  863. priority.RegistryTask.StopAndUnregister("PA")
  864. SetPadTimer()
  865. // check resume
  866. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  867. if ok {
  868. if taskName == "EMG" {
  869. EMGConfbridgeReinvite(task.ConfbridgeID)
  870. alstatus.PaStatus("", "EMG", "start")
  871. } else if taskName == "CPA" {
  872. CPAConfbridgeReinvite(task.ConfbridgeID)
  873. alstatus.PaStatus("", "CPA", "start")
  874. }
  875. }
  876. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA end
  877. alstatus.PaStatus(event["CallerIDNum"], "CPA", "end")
  878. //CPA
  879. taskTmp, ok := priority.RegistryTask.Get("CPA")
  880. if ok {
  881. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  882. }
  883. priority.RegistryTask.StopAndUnregister("CPA")
  884. SetPadTimer()
  885. //check resume
  886. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  887. if ok {
  888. if taskName == "EMG" {
  889. EMGConfbridgeReinvite(task.ConfbridgeID)
  890. alstatus.PaStatus("", "EMG", "start")
  891. }
  892. }
  893. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  894. } else if event["CallerIDName"] == "EMG" && event["Exten"] == "0502" { // EMG broadcast hangup
  895. alstatus.PaStatus("", "EMG", "end")
  896. //EMG
  897. taskTmp, ok := priority.RegistryTask.Get("EMG")
  898. if ok {
  899. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  900. }
  901. priority.RegistryTask.StopAndUnregister("EMG")
  902. SetPadTimer()
  903. //check resume
  904. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  905. if ok {
  906. if taskName == "CPA" {
  907. CPAConfbridgeReinvite(task.ConfbridgeID)
  908. alstatus.PaStatus("", "CPA", "start")
  909. }
  910. }
  911. } else if event["CallerIDName"] == "STN" && event["Exten"] == "0503" {
  912. alstatus.PaStatus("", "STN", "end")
  913. //STN
  914. taskTmp, ok := priority.RegistryTask.Get("STN")
  915. if ok {
  916. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  917. }
  918. priority.RegistryTask.StopAndUnregister("STN")
  919. SetPadTimer()
  920. //check resume
  921. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  922. if ok {
  923. if taskName == "CPA" {
  924. CPAConfbridgeReinvite(task.ConfbridgeID)
  925. alstatus.PaStatus("", "CPA", "start")
  926. } else if taskName == "EMG" {
  927. EMGConfbridgeReinvite(task.ConfbridgeID)
  928. alstatus.PaStatus("", "EMG", "start")
  929. }
  930. }
  931. } else if event["CallerIDName"] == "DCS" && event["Exten"] == "0504" {
  932. alstatus.PaStatus("", "DCS", "end")
  933. //DSC
  934. taskTmp, ok := priority.RegistryTask.Get("DCS")
  935. if ok {
  936. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  937. }
  938. priority.RegistryTask.StopAndUnregister("DCS")
  939. SetPadTimer()
  940. //check resume
  941. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  942. if ok {
  943. if taskName == "CPA" {
  944. CPAConfbridgeReinvite(task.ConfbridgeID)
  945. alstatus.PaStatus("", "CPA", "start")
  946. } else if taskName == "EMG" {
  947. EMGConfbridgeReinvite(task.ConfbridgeID)
  948. alstatus.PaStatus("", "EMG", "start")
  949. //alstatus.PaStatus("", "EMG", "start")
  950. }
  951. }
  952. } else if event["CallerIDName"] == "SPC" && event["Exten"] == "0505" {
  953. alstatus.PaStatus("", "SPC", "end")
  954. //SPC
  955. taskTmp, ok := priority.RegistryTask.Get("SPC")
  956. if ok {
  957. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  958. }
  959. priority.RegistryTask.StopAndUnregister("SPC")
  960. SetPadTimer()
  961. //check resume
  962. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  963. if ok {
  964. if taskName == "CPA" {
  965. CPAConfbridgeReinvite(task.ConfbridgeID)
  966. alstatus.PaStatus("", "CPA", "start")
  967. } else if taskName == "EMG" {
  968. EMGConfbridgeReinvite(task.ConfbridgeID)
  969. alstatus.PaStatus("", "EMG", "start")
  970. }
  971. }
  972. } else if event["CallerIDName"] == "CHK" && event["Exten"] == "0510" {
  973. alstatus.PaStatus("", "CHK", "end")
  974. //CHK
  975. taskTmp, ok := priority.RegistryTask.Get("CHK")
  976. if ok {
  977. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  978. }
  979. priority.RegistryTask.StopAndUnregister("CHK")
  980. SetPadTimer()
  981. //check resume
  982. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  983. if ok {
  984. if taskName == "CPA" {
  985. CPAConfbridgeReinvite(task.ConfbridgeID)
  986. alstatus.PaStatus("", "CPA", "start")
  987. } else if taskName == "EMG" {
  988. EMGConfbridgeReinvite(task.ConfbridgeID)
  989. alstatus.PaStatus("", "EMG", "start")
  990. }
  991. }
  992. } else if event["CallerIDName"] == "VOL" && event["Exten"] == "0513" {
  993. alstatus.PaStatus("", "VOL", "end")
  994. //VOL
  995. taskTmp, ok := priority.RegistryTask.Get("VOL")
  996. if ok {
  997. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  998. }
  999. priority.RegistryTask.StopAndUnregister("VOL")
  1000. SetPadTimer()
  1001. //check resume
  1002. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1003. if ok {
  1004. if taskName == "CPA" {
  1005. CPAConfbridgeReinvite(task.ConfbridgeID)
  1006. alstatus.PaStatus("", "CPA", "start")
  1007. } else if taskName == "EMG" {
  1008. EMGConfbridgeReinvite(task.ConfbridgeID)
  1009. alstatus.PaStatus("", "EMG", "start")
  1010. }
  1011. }
  1012. }
  1013. case "DialEnd":
  1014. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1015. //Cab Cab start
  1016. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0400" && event["DialStatus"] == "ANSWER" {
  1017. //================================
  1018. task := priority.TaskInfo{
  1019. RunChannel: event["Channel"],
  1020. LocalChan: event["Channel"],
  1021. RunType: "C2C",
  1022. Priority: priority.AllTasks.C2C.Priority,
  1023. ConfbridgeID: "",
  1024. Running: true,
  1025. }
  1026. priority.RegistryTask.Register("C2C", task)
  1027. alstatus.PaStatus(event["CallerIDNum"], "C2C", "start")
  1028. if priority.TaskCreating == "C2C" {
  1029. priority.TaskCreating = ""
  1030. }
  1031. //check PAD timer
  1032. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.C2C.Priority {
  1033. if active.QueueTimer != nil {
  1034. if active.QueueTimer.Stop() {
  1035. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  1036. } else {
  1037. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  1038. }
  1039. }
  1040. }
  1041. } else if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "NOANSWER" {
  1042. //lfshook.NewLogger().Infof("===%s======%s", event["Event"], event["DialStatus"])
  1043. time.Sleep(time.Millisecond * 300)
  1044. //check resume
  1045. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1046. if ok {
  1047. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1048. }
  1049. }
  1050. //TMS Answer PAD (有时asterisk反馈的BridgeEnter事件有问题,根据dialend判断)
  1051. if utils.IsPAIU(event["CallerIDName"]) && utils.IsPACU(event["CallerIDNum"]) && event["DialStatus"] == "ANSWER" && event["Exten"] == "0403" {
  1052. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1053. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1054. //=============================
  1055. task := priority.TaskInfo{
  1056. RunChannel: event["Channel"],
  1057. LocalChan: event["Channel"],
  1058. RunType: "PAD-TMS",
  1059. Priority: priority.AllTasks.PADTMS.Priority,
  1060. ConfbridgeID: "",
  1061. Running: true,
  1062. }
  1063. priority.RegistryTask.Register("PAD-TMS", task)
  1064. if priority.TaskCreating == "PAD-TMS" {
  1065. priority.TaskCreating = ""
  1066. }
  1067. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1068. if active.ActivedCab == "1" {
  1069. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1070. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1071. } else if active.ActivedCab == "8" {
  1072. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1073. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1074. } else if active.ActivedCab == "" {
  1075. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1076. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1077. }
  1078. }
  1079. case "BridgeLeave":
  1080. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1081. //Cab Cab end
  1082. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["Exten"] == "0400" {
  1083. alstatus.PaStatus(event["CallerIDNum"], "C2C", "end")
  1084. priority.RegistryTask.StopAndUnregister("C2C")
  1085. time.Sleep(time.Millisecond * 300)
  1086. SetPadTimer()
  1087. //check resume
  1088. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1089. if ok {
  1090. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1091. //check resume
  1092. /*if taskName == "CPA" {
  1093. alstatus.PaStatus("", "CPA", "start")
  1094. } else if taskName == "EMG" {
  1095. alstatus.PaStatus("", "EMG", "start")
  1096. } else if taskName == "SPC" {
  1097. alstatus.PaStatus("", "SPC", "start")
  1098. } else if taskName == "DCS" {
  1099. alstatus.PaStatus("", "DCS", "start")
  1100. } else if taskName == "STN" {
  1101. alstatus.PaStatus("", "STN", "start")
  1102. } else if taskName == "CHK" {
  1103. alstatus.PaStatus("", "CHK", "start")
  1104. }*/
  1105. }
  1106. }
  1107. case "ExtensionStatus":
  1108. //lfshook.NewLogger().Infof("=========event:%s Ext:%s status:%s ", event["Event"], event["Exten"], event["StatusText"])
  1109. //update extension status
  1110. if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
  1111. if len(event["Exten"]) > 3 && utils.IsPAIU(event["Exten"]) {
  1112. alstatus.AlarmStatus(event["Exten"], event["StatusText"]) // PAD idle + unavailable
  1113. }
  1114. }
  1115. case "BridgeEnter": // TMS-ICP answer PAD; PACU connect ICP
  1116. lfshook.NewLogger().Infof("=========event:%s callerid-num:%s callerid-name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1117. //PAD-OCC start
  1118. if utils.IsIO(event["CallerIDNum"]) && utils.IsPAIU(event["ConnectedLineNum"]) {
  1119. if priority.PADOccStart == 0 {
  1120. if priority.TaskCreating == "PAD-OCC" {
  1121. priority.TaskCreating = ""
  1122. }
  1123. alstatus.OccPad("start")
  1124. priority.PADOccStart = 1
  1125. }
  1126. if priority.PADStart == 0 {
  1127. alstatus.PaStatus("", "PAD", "start")
  1128. priority.PADStart = 1
  1129. }
  1130. }
  1131. /*if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected -----TMS Answer PAD
  1132. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1133. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1134. //=============================
  1135. task := priority.TaskInfo{
  1136. RunChannel: event["Channel"],
  1137. LocalChan: event["Channel"],
  1138. RunType: "PAD-TMS",
  1139. Priority: priority.AllTasks.PADTMS.Priority,
  1140. ConfbridgeID: "",
  1141. Running: true,
  1142. }
  1143. priority.RegistryTask.Register("PAD-TMS", task)
  1144. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1145. if active.ActivedCab == "1" {
  1146. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1147. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1148. } else if active.ActivedCab == "8" {
  1149. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1150. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1151. } else if active.ActivedCab == "" {
  1152. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1153. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1154. }
  1155. } else*/if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0405" { // PAD connect ICP-TMS;PACU not available
  1156. //=============================
  1157. task := priority.TaskInfo{
  1158. RunChannel: event["Channel"],
  1159. LocalChan: event["Channel"],
  1160. RunType: "PAD-TMS",
  1161. Priority: priority.AllTasks.PADTMS.Priority,
  1162. ConfbridgeID: "",
  1163. Running: true,
  1164. }
  1165. priority.RegistryTask.Register("PAD-TMS", task)
  1166. if priority.TaskCreating == "PAD-TMS" {
  1167. priority.TaskCreating = ""
  1168. }
  1169. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1170. alstatus.AlarmStatus(event["CallerIDNum"], "connect") // PAD connect ICP-TMS
  1171. }
  1172. }
  1173. }
  1174. func StartAMI(connectOKCallBack func(), handleEvents []func(event map[string]string)) {
  1175. lfshook.NewLogger().Info("Start AMI")
  1176. settings := &amigo.Settings{
  1177. Host: configs.ConfigGlobal.AsteriskAMIHost,
  1178. Port: configs.ConfigGlobal.AsteriskAMIPort,
  1179. Username: configs.ConfigGlobal.AsteriskAMIUser,
  1180. Password: configs.ConfigGlobal.AsteriskAMISecret,
  1181. LogLevel: logrus.ErrorLevel}
  1182. //lfshook.NewLogger().Infof("ami setting: %+v", settings)
  1183. AminInstance = amigo.New(settings, lfshook.NewLogger())
  1184. AminInstance.EventOn(func(payload ...interface{}) {
  1185. // lfshook.NewLogger().Infof("ami event on %+v", payload[0])
  1186. event := payload[0].(map[string]string)
  1187. go HandleAMI(event)
  1188. for _, handle := range handleEvents {
  1189. go handle(event)
  1190. }
  1191. })
  1192. AminInstance.ConnectOn(func(payload ...interface{}) {
  1193. //lfshook.NewLogger().Infof("ami connect on %+v", payload[0])
  1194. if payload[0] == pkg.Connect_OK {
  1195. connectOKCallBack()
  1196. } else {
  1197. lfshook.NewLogger().Errorf("ami connect failure %+v", payload)
  1198. //active.Master = false
  1199. }
  1200. })
  1201. AminInstance.Connect()
  1202. }
  1203. func Connected() bool {
  1204. if AminInstance != nil {
  1205. return AminInstance.Connected()
  1206. }
  1207. return false
  1208. }