index.go 55 KB

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