index.go 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  278. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  279. //check resume
  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. //break
  289. } else { //OCC queue is not empty
  290. utils.LoggerDebug.Printf("PAD goto OCC , interrupt other task !")
  291. // OCC hangup detected, hangup other running channels
  292. runningTaskName := InterruptRunningTask("PAD-OCC")
  293. if runningTaskName != "" {
  294. time.Sleep(time.Millisecond * 100) //wait endpoint release
  295. }
  296. //time.Sleep(time.Millisecond * 100) //wait io idle
  297. //lfshook.NewLogger().Infof("====Start OCC-PAD===next==%+v", res)
  298. if active.TrainDevide == 0 { //列车没有断开
  299. if (active.ActivedCab == "1" && ExtenStatus("1411") == "Idle") || (active.ActivedCab == "" && active.ActivedCabDelay == "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") || (active.ActivedCab == "" && active.ActivedCabDelay == "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. } else { //列车断开模式下
  348. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  349. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  350. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  351. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  352. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  353. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  354. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  355. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  356. }
  357. }
  358. }
  359. }
  360. if utils.IsPAIU(event["CallerIDNum"]) { // PAD hangup, check if PAD all end, send PAD end status
  361. number := strings.Split(strings.Split(event["Channel"], "-")[0], "/")[1]
  362. lfshook.NewLogger().Infof("===hangup PAD ===NUM:%s=================listAllTask:%+v===", number, priority.RegistryTask.ListAll())
  363. if utils.IsPAIU(number) { //pad and io hangup
  364. //clean confbridge & clean task info
  365. //PAD-ICP
  366. taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
  367. if ok {
  368. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  369. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  370. }
  371. // PAD-TMS
  372. taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
  373. if ok {
  374. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  375. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  376. }
  377. //PAD-OCC
  378. taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
  379. if ok {
  380. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  381. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  382. }
  383. lfshook.NewLogger().Infof("===hangup PAD ===NUM:%s========2222=========listAllTask:%+v===", number, priority.RegistryTask.ListAll())
  384. //Check PAD END
  385. res, _ := QueueStatus("0300", "") // check ICP queue ,if empty PAD end
  386. res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  387. if res == nil || res1 == nil {
  388. utils.LoggerDebug.Printf("PAD Hangup , check ICP|OCC queue return nil, exit.")
  389. return
  390. }
  391. lfshook.NewLogger().Infof("===Hangup PAD====== ICP Queue calls:%s OCC Queue calls:%s", res.Calls, res1.Calls)
  392. if res.Calls == "0" && res1.Calls == "0" {
  393. if priority.PADStart == 1 {
  394. //SetPadTimer()
  395. alstatus.PaStatus("", "PAD", "end")
  396. priority.PADStart = 0
  397. //priority.PADTMSStart = 0
  398. /*
  399. //clean confbridge
  400. //PAD-ICP
  401. taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
  402. if ok {
  403. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  404. }
  405. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  406. // PAD-TMS
  407. taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
  408. if ok {
  409. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  410. }
  411. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  412. //PAD-OCC
  413. taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
  414. if ok {
  415. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  416. }
  417. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  418. */
  419. //check resume
  420. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  421. if ok {
  422. if taskName == "EMG" {
  423. EMGConfbridgeReinvite(task.ConfbridgeID)
  424. alstatus.PaStatus("", "EMG", "start")
  425. } else if taskName == "CPA" {
  426. CPAConfbridgeReinvite(task.ConfbridgeID)
  427. alstatus.PaStatus("", "CPA", "start")
  428. }
  429. }
  430. }
  431. priority.ICPAnswer = 0
  432. priority.OCCAnswer = 0
  433. break
  434. }
  435. }
  436. }
  437. case "QueueCallerJoin":
  438. lfshook.NewLogger().Infof("=========%s", event["Event"])
  439. if priority.OCCAnswer == 1 && event["Queue"] == "0300" { //New PAD Goto the OCC queue in the first time, if OCC answered
  440. utils.LoggerDebug.Printf("Redirect PAD %s to OCC queue .", event["CallerIDNum"])
  441. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  442. go RedirectInQueue(event["CallerIDNum"], "0301", "queues-occ", event["CallerIDNum"])
  443. break
  444. }
  445. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) && event["Queue"] == "0300" { // Alarm join the queue, PAD in the queue
  446. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  447. ICPQueue, err := QueueStatus("0300", "") // check ICP queue, get entries
  448. if err != nil {
  449. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  450. return
  451. }
  452. if ICPQueue == nil {
  453. return
  454. }
  455. if priority.ICPAnswer == 0 && ICPQueue.Calls == "1" { //ICP did not answer any first call to the ICP queue ; Ready to Set Occ Queue Timer
  456. toRunpriority := priority.GetPriorityByKey("PAD-ICP")
  457. time.Sleep(time.Millisecond * 500) //wait high priority task start
  458. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  459. if ok {
  460. //lfshook.NewLogger().Infof("=1==QueueCallerJoin===runing:%d=====toRun:=%d==Status:%s", taskTmp, toRunpriority, event["ChannelStateDesc"])
  461. if taskTmp.Priority < toRunpriority { //higher priority task running ,do not set timer
  462. utils.LoggerDebug.Printf("The First PAD %s enter ICP queue , %s running do not set PAD timeout.", taskTmp.RunType, event["CallerIDNum"])
  463. break
  464. }
  465. }
  466. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  467. if active.QueueTimer != nil {
  468. if active.QueueTimer.Stop() {
  469. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  470. } else {
  471. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  472. }
  473. }
  474. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  475. utils.LoggerDebug.Printf("The first PAD %s enter ICP queue , set PAD timeout %d sec .", event["CallerIDNum"], active.PADTimeout)
  476. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  477. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  478. //if both not active , return
  479. //if active.ActivedCab == "" {
  480. // return
  481. //}
  482. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  483. if err != nil {
  484. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  485. return
  486. }
  487. if res == nil {
  488. return
  489. }
  490. if res.Calls == "0" { // OCC queue empty
  491. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  492. if err != nil {
  493. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  494. return
  495. }
  496. if resCaller.Entrys != nil {
  497. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  498. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  499. })
  500. for _, caller := range resCaller.Entrys {
  501. priority.ICPAnswer = 0
  502. //lfshook.NewLogger().Infof("====QueueCallerJoin==QueueTimer===%s", event["Event"])
  503. //lfshook.NewLogger().Infof("Redirect to 0301 extension:%s Pos:%s", caller.CallerIDNum, caller.Position)
  504. //order by pos
  505. utils.LoggerDebug.Printf("Redirect %s to OCC queue Pos:%s", caller.CallerIDNum, caller.Position)
  506. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  507. time.Sleep(time.Millisecond * 100) //200 ms delay
  508. }
  509. }
  510. }
  511. })
  512. }
  513. break
  514. }
  515. //first PAD caller goto OCC
  516. //OCC dial PACUs;
  517. //PAD Page OCC+ICPs;
  518. if utils.IsPAIU(event["CallerIDNum"]) && event["Queue"] == "0301" && priority.OCCAnswer == 0 { // The first PAD to OCC ,caller is PAD
  519. utils.LoggerDebug.Printf("The first PAD to OCC queue .")
  520. WaitTaskCreate("PAD-OCC")
  521. if priority.CheckPriority("PAD-OCC") {
  522. runningTaskName := InterruptRunningTask("PAD-OCC") //PAD-OCC interrupt other
  523. if runningTaskName != "" {
  524. time.Sleep(time.Millisecond * 100) //wait endpoint release
  525. }
  526. priority.OCCAnswer = 1
  527. time.Sleep(time.Millisecond * 300)
  528. if active.TrainDevide == 0 { //列车没有断开
  529. if (active.ActivedCab == "1" && ExtenStatus("1411") == "Idle") || (active.ActivedCab == "" && active.ActivedCabDelay == "1" && ExtenStatus("1411") == "Idle") { //check active and OCC status
  530. utils.LoggerDebug.Printf("PAD %s goto OCC1 .", event["CallerIDNum"])
  531. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  532. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  533. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  534. } else if (active.ActivedCab == "8" && ExtenStatus("1481") == "Idle") || (active.ActivedCab == "" && active.ActivedCabDelay == "8" && ExtenStatus("1481") == "Idle") {
  535. utils.LoggerDebug.Printf("PAD %s goto OCC8 .", event["CallerIDNum"])
  536. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  537. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  538. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  539. }
  540. } else { //列车断开模式下
  541. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  542. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  543. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  544. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  545. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  546. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  547. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  548. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  549. }
  550. }
  551. } else {
  552. lfshook.NewLogger().Infof("====PAD-OCC Priority false===")
  553. }
  554. time.Sleep(2 * time.Second)
  555. if priority.TaskCreating == "PAD-OCC" {
  556. priority.TaskCreating = ""
  557. }
  558. }
  559. case "ConfbridgeJoin":
  560. lfshook.NewLogger().Infof("=========%+v", event["Event"])
  561. //lfshook.NewLogger().Infof("=========%+v", event)
  562. //set priority and send PA status msg
  563. switch event["CallerIDName"] {
  564. case "EMG":
  565. if event["Exten"] == "0502" {
  566. //================================
  567. task := priority.TaskInfo{
  568. RunChannel: event["Channel"],
  569. LocalChan: event["Channel"],
  570. RunType: "EMG",
  571. Priority: priority.AllTasks.EMG.Priority,
  572. ConfbridgeID: event["Conference"],
  573. Running: true,
  574. }
  575. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  576. priority.RegistryTask.Register("EMG", task)
  577. if priority.TaskCreating == "EMG" {
  578. priority.TaskCreating = ""
  579. }
  580. alstatus.PaStatus("", "EMG", "start")
  581. //check PAD timer
  582. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.EMG.Priority {
  583. if active.QueueTimer != nil {
  584. if active.QueueTimer.Stop() {
  585. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  586. } else {
  587. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  588. }
  589. }
  590. }
  591. return
  592. }
  593. case "SPC":
  594. if event["Exten"] == "0505" {
  595. //================================
  596. task := priority.TaskInfo{
  597. RunChannel: event["Channel"],
  598. LocalChan: event["Channel"],
  599. RunType: "SPC",
  600. Priority: priority.AllTasks.SPC.Priority,
  601. ConfbridgeID: event["Conference"],
  602. Running: true,
  603. }
  604. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  605. priority.RegistryTask.Register("SPC", task)
  606. alstatus.PaStatus("", "SPC", "start")
  607. if priority.TaskCreating == "SPC" {
  608. priority.TaskCreating = ""
  609. }
  610. //check PAD timer
  611. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.SPC.Priority {
  612. if active.QueueTimer != nil {
  613. if active.QueueTimer.Stop() {
  614. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  615. } else {
  616. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  617. }
  618. }
  619. }
  620. return
  621. }
  622. case "DCS":
  623. if event["Exten"] == "0504" {
  624. //================================
  625. task := priority.TaskInfo{
  626. RunChannel: event["Channel"],
  627. LocalChan: event["Channel"],
  628. RunType: "DCS",
  629. Priority: priority.AllTasks.DCS.Priority,
  630. ConfbridgeID: event["Conference"],
  631. Running: true,
  632. }
  633. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  634. priority.RegistryTask.Register("DCS", task)
  635. alstatus.PaStatus("", "DCS", "start")
  636. if priority.TaskCreating == "DCS" {
  637. priority.TaskCreating = ""
  638. }
  639. //check PAD timer
  640. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.DCS.Priority {
  641. if active.QueueTimer != nil {
  642. if active.QueueTimer.Stop() {
  643. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  644. } else {
  645. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  646. }
  647. }
  648. }
  649. return
  650. }
  651. case "STN":
  652. if event["Exten"] == "0503" {
  653. //================================
  654. task := priority.TaskInfo{
  655. RunChannel: event["Channel"],
  656. LocalChan: event["Channel"],
  657. RunType: "STN",
  658. Priority: priority.AllTasks.STN.Priority,
  659. ConfbridgeID: event["Conference"],
  660. Running: true,
  661. }
  662. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  663. priority.RegistryTask.Register("STN", task)
  664. alstatus.PaStatus("", "STN", "start")
  665. if priority.TaskCreating == "STN" {
  666. priority.TaskCreating = ""
  667. }
  668. //check PAD timer
  669. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.STN.Priority {
  670. if active.QueueTimer != nil {
  671. if active.QueueTimer.Stop() {
  672. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  673. } else {
  674. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  675. }
  676. }
  677. }
  678. return
  679. }
  680. case "CHK":
  681. if event["Exten"] == "0510" {
  682. //================================
  683. task := priority.TaskInfo{
  684. RunChannel: event["Channel"],
  685. LocalChan: event["Channel"],
  686. RunType: "CHK",
  687. Priority: priority.AllTasks.CHK.Priority,
  688. ConfbridgeID: event["Conference"],
  689. Running: true,
  690. }
  691. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  692. priority.RegistryTask.Register("CHK", task)
  693. alstatus.PaStatus("", "CHK", "start")
  694. if priority.TaskCreating == "CHK" {
  695. priority.TaskCreating = ""
  696. }
  697. //check PAD timer
  698. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.CHK.Priority {
  699. if active.QueueTimer != nil {
  700. if active.QueueTimer.Stop() {
  701. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  702. } else {
  703. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  704. }
  705. }
  706. }
  707. return
  708. }
  709. case "VOL": // tone-test
  710. if event["Exten"] == "0513" {
  711. //================================
  712. task := priority.TaskInfo{
  713. RunChannel: event["Channel"],
  714. LocalChan: event["Channel"],
  715. RunType: "VOL",
  716. Priority: priority.AllTasks.VOL.Priority,
  717. ConfbridgeID: event["Conference"],
  718. Running: true,
  719. }
  720. utils.LoggerDebug.Printf("%s Runing , Set %s task info %+v .", event["CallerIDName"], event["CallerIDName"], task)
  721. priority.RegistryTask.Register("VOL", task)
  722. alstatus.PaStatus("", "VOL", "start")
  723. if priority.TaskCreating == "VOL" {
  724. priority.TaskCreating = ""
  725. }
  726. //check PAD timer
  727. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.VOL.Priority {
  728. if active.QueueTimer != nil {
  729. if active.QueueTimer.Stop() {
  730. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  731. } else {
  732. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  733. }
  734. }
  735. }
  736. return
  737. }
  738. }
  739. /*
  740. //get confbridge id for join back to confbridge
  741. if event["Context"] == "pad-page-occ-icp" && utils.IsPAIU(event["CallerIDNum"]) {
  742. //active.ICPCONBID = event["Conference"] //pad-occ
  743. priority.AllTasks.PADOCC.ConfbridgeID = event["Conference"]
  744. }
  745. */
  746. //Send PA start msg to STC
  747. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA start
  748. alstatus.PaStatus(event["CallerIDNum"], "PA", "start")
  749. //================================
  750. task := priority.TaskInfo{
  751. RunChannel: event["Channel"],
  752. LocalChan: event["Channel"],
  753. RunType: "PA",
  754. Priority: priority.AllTasks.PA.Priority,
  755. ConfbridgeID: event["Conference"],
  756. Running: true,
  757. }
  758. utils.LoggerDebug.Printf("PA Runing , Set PA task info %+v .", task)
  759. priority.RegistryTask.Register("PA", task)
  760. if priority.TaskCreating == "PA" {
  761. priority.TaskCreating = ""
  762. }
  763. //check PAD timer
  764. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.PA.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. break
  774. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA start
  775. alstatus.PaStatus(event["CallerIDNum"], "CPA", "start")
  776. //================================
  777. task := priority.TaskInfo{
  778. RunChannel: event["Channel"],
  779. LocalChan: event["Channel"],
  780. RunType: "CPA",
  781. Priority: priority.AllTasks.CPA.Priority,
  782. ConfbridgeID: event["Conference"],
  783. Running: true,
  784. }
  785. utils.LoggerDebug.Printf("CPA Runing , Set CPA task info %+v .", task)
  786. priority.RegistryTask.Register("CPA", task)
  787. utils.LoggerDebug.Printf("CPA Runing , Set CPA task info %+v .", priority.RegistryTask.ListAll())
  788. if priority.TaskCreating == "CPA" {
  789. priority.TaskCreating = ""
  790. }
  791. //check PAD timer
  792. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.CPA.Priority {
  793. if active.QueueTimer != nil {
  794. if active.QueueTimer.Stop() {
  795. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  796. } else {
  797. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  798. }
  799. }
  800. }
  801. return
  802. }
  803. //ICP answer PAD;PACUs connected ICP
  804. //PAD chanspy ICP1
  805. //ICP8 call PAD
  806. if event["ConnectedLineNum"] == "ani1" && event["Exten"] == "0511" { //PAD answered by ICP; PACUs connected ICP1
  807. //lfshook.NewLogger().Infof("====PAD answered by ICP1:%s=====", event["ConnectedLineName"])
  808. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  809. //================================
  810. task := priority.TaskInfo{
  811. RunChannel: event["Channel"],
  812. LocalChan: event["Channel"],
  813. RunType: "PAD-ICP",
  814. Priority: priority.AllTasks.PADICP.Priority,
  815. ConfbridgeID: event["Conference"],
  816. Running: true,
  817. }
  818. priority.RegistryTask.Register("PAD-ICP", task)
  819. if priority.TaskCreating == "PAD-ICP" {
  820. priority.TaskCreating = ""
  821. }
  822. utils.LoggerDebug.Printf("PAD %s answered by ICP1 , set task info :%+v .", event["ConnectedLineName"], task)
  823. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  824. go RedirectInQueue(event["ConnectedLineName"], "2311", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP1
  825. if ExtenStatus("2381") == "Idle" {
  826. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "8") // PAD call ICP8
  827. }
  828. }
  829. if event["ConnectedLineNum"] == "ani8" && event["Exten"] == "0511" { //PAD ansered by ICP8; PACUs connected ICP8
  830. lfshook.NewLogger().Infof("====PAD answered by ICP8:%s=====", event["ConnectedLineName"])
  831. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  832. //================================
  833. task := priority.TaskInfo{
  834. RunChannel: event["Channel"],
  835. LocalChan: event["Channel"],
  836. RunType: "PAD-ICP",
  837. Priority: priority.AllTasks.PADICP.Priority,
  838. ConfbridgeID: event["Conference"],
  839. Running: true,
  840. }
  841. priority.RegistryTask.Register("PAD-ICP", task)
  842. if priority.TaskCreating == "PAD-ICP" {
  843. priority.TaskCreating = ""
  844. }
  845. utils.LoggerDebug.Printf("PAD %s answered by ICP8 , set task info :%+v .", event["ConnectedLineName"], task)
  846. //lfshook.NewLogger().Infof("=========PAD-ICP answer==ListAll===%+v", priority.RegistryTask.ListAll())
  847. go RedirectInQueue(event["ConnectedLineName"], "2381", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP8
  848. if ExtenStatus("2311") == "Idle" {
  849. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "1") // PAD call ICP1
  850. }
  851. break
  852. }
  853. //OCC answer PAD;Set the task channel
  854. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsIO(event["Exten"]) && event["Context"] == "pad-page-occ-icp" { //PAD Page OCC1+ICPs connected
  855. lfshook.NewLogger().Infof("====PAD answered by OCC:====")
  856. //================================
  857. task := priority.TaskInfo{
  858. RunChannel: event["Channel"],
  859. LocalChan: event["Channel"],
  860. RunType: "PAD-OCC",
  861. Priority: priority.AllTasks.PADOCC.Priority,
  862. ConfbridgeID: event["Conference"],
  863. Running: true,
  864. }
  865. priority.RegistryTask.Register("PAD-OCC", task)
  866. if priority.TaskCreating == "PAD-OCC" {
  867. priority.TaskCreating = ""
  868. }
  869. utils.LoggerDebug.Printf("PAD %s answered by OCC , set task info :%+v .", event["CallerIDNum"], task)
  870. break
  871. }
  872. case "ConfbridgeLeave":
  873. lfshook.NewLogger().Infof("=========%s", event["Event"])
  874. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA end
  875. alstatus.PaStatus(event["CallerIDNum"], "PA", "end")
  876. priority.RegistryTask.StopAndUnregister("PA")
  877. SetPadTimer()
  878. // check resume
  879. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  880. if ok {
  881. if taskName == "EMG" {
  882. EMGConfbridgeReinvite(task.ConfbridgeID)
  883. alstatus.PaStatus("", "EMG", "start")
  884. } else if taskName == "CPA" {
  885. CPAConfbridgeReinvite(task.ConfbridgeID)
  886. alstatus.PaStatus("", "CPA", "start")
  887. }
  888. }
  889. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA end
  890. alstatus.PaStatus(event["CallerIDNum"], "CPA", "end")
  891. //CPA
  892. taskTmp, ok := priority.RegistryTask.Get("CPA")
  893. if ok {
  894. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  895. }
  896. priority.RegistryTask.StopAndUnregister("CPA")
  897. SetPadTimer()
  898. //check resume
  899. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  900. if ok {
  901. if taskName == "EMG" {
  902. EMGConfbridgeReinvite(task.ConfbridgeID)
  903. alstatus.PaStatus("", "EMG", "start")
  904. }
  905. }
  906. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  907. } else if event["CallerIDName"] == "EMG" && event["Exten"] == "0502" { // EMG broadcast hangup
  908. alstatus.PaStatus("", "EMG", "end")
  909. //EMG
  910. taskTmp, ok := priority.RegistryTask.Get("EMG")
  911. if ok {
  912. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  913. }
  914. priority.RegistryTask.StopAndUnregister("EMG")
  915. SetPadTimer()
  916. //check resume
  917. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  918. if ok {
  919. if taskName == "CPA" {
  920. CPAConfbridgeReinvite(task.ConfbridgeID)
  921. alstatus.PaStatus("", "CPA", "start")
  922. }
  923. }
  924. } else if event["CallerIDName"] == "STN" && event["Exten"] == "0503" {
  925. alstatus.PaStatus("", "STN", "end")
  926. //STN
  927. taskTmp, ok := priority.RegistryTask.Get("STN")
  928. if ok {
  929. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  930. }
  931. priority.RegistryTask.StopAndUnregister("STN")
  932. SetPadTimer()
  933. //check resume
  934. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  935. if ok {
  936. if taskName == "CPA" {
  937. CPAConfbridgeReinvite(task.ConfbridgeID)
  938. alstatus.PaStatus("", "CPA", "start")
  939. } else if taskName == "EMG" {
  940. EMGConfbridgeReinvite(task.ConfbridgeID)
  941. alstatus.PaStatus("", "EMG", "start")
  942. }
  943. }
  944. } else if event["CallerIDName"] == "DCS" && event["Exten"] == "0504" {
  945. alstatus.PaStatus("", "DCS", "end")
  946. //DSC
  947. taskTmp, ok := priority.RegistryTask.Get("DCS")
  948. if ok {
  949. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  950. }
  951. priority.RegistryTask.StopAndUnregister("DCS")
  952. SetPadTimer()
  953. //check resume
  954. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  955. if ok {
  956. if taskName == "CPA" {
  957. CPAConfbridgeReinvite(task.ConfbridgeID)
  958. alstatus.PaStatus("", "CPA", "start")
  959. } else if taskName == "EMG" {
  960. EMGConfbridgeReinvite(task.ConfbridgeID)
  961. alstatus.PaStatus("", "EMG", "start")
  962. //alstatus.PaStatus("", "EMG", "start")
  963. }
  964. }
  965. } else if event["CallerIDName"] == "SPC" && event["Exten"] == "0505" {
  966. alstatus.PaStatus("", "SPC", "end")
  967. //SPC
  968. taskTmp, ok := priority.RegistryTask.Get("SPC")
  969. if ok {
  970. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  971. }
  972. priority.RegistryTask.StopAndUnregister("SPC")
  973. SetPadTimer()
  974. //check resume
  975. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  976. if ok {
  977. if taskName == "CPA" {
  978. CPAConfbridgeReinvite(task.ConfbridgeID)
  979. alstatus.PaStatus("", "CPA", "start")
  980. } else if taskName == "EMG" {
  981. EMGConfbridgeReinvite(task.ConfbridgeID)
  982. alstatus.PaStatus("", "EMG", "start")
  983. }
  984. }
  985. } else if event["CallerIDName"] == "CHK" && event["Exten"] == "0510" {
  986. alstatus.PaStatus("", "CHK", "end")
  987. //CHK
  988. taskTmp, ok := priority.RegistryTask.Get("CHK")
  989. if ok {
  990. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  991. }
  992. priority.RegistryTask.StopAndUnregister("CHK")
  993. SetPadTimer()
  994. //check resume
  995. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  996. if ok {
  997. if taskName == "CPA" {
  998. CPAConfbridgeReinvite(task.ConfbridgeID)
  999. alstatus.PaStatus("", "CPA", "start")
  1000. } else if taskName == "EMG" {
  1001. EMGConfbridgeReinvite(task.ConfbridgeID)
  1002. alstatus.PaStatus("", "EMG", "start")
  1003. }
  1004. }
  1005. } else if event["CallerIDName"] == "VOL" && event["Exten"] == "0513" {
  1006. alstatus.PaStatus("", "VOL", "end")
  1007. //VOL
  1008. taskTmp, ok := priority.RegistryTask.Get("VOL")
  1009. if ok {
  1010. ConfbridgeKick(taskTmp.ConfbridgeID, "all")
  1011. }
  1012. priority.RegistryTask.StopAndUnregister("VOL")
  1013. SetPadTimer()
  1014. //check resume
  1015. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1016. if ok {
  1017. if taskName == "CPA" {
  1018. CPAConfbridgeReinvite(task.ConfbridgeID)
  1019. alstatus.PaStatus("", "CPA", "start")
  1020. } else if taskName == "EMG" {
  1021. EMGConfbridgeReinvite(task.ConfbridgeID)
  1022. alstatus.PaStatus("", "EMG", "start")
  1023. }
  1024. }
  1025. }
  1026. case "DialEnd":
  1027. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1028. //Cab Cab start
  1029. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0400" && event["DialStatus"] == "ANSWER" {
  1030. //================================
  1031. task := priority.TaskInfo{
  1032. RunChannel: event["Channel"],
  1033. LocalChan: event["Channel"],
  1034. RunType: "C2C",
  1035. Priority: priority.AllTasks.C2C.Priority,
  1036. ConfbridgeID: "",
  1037. Running: true,
  1038. }
  1039. priority.RegistryTask.Register("C2C", task)
  1040. alstatus.PaStatus(event["CallerIDNum"], "C2C", "start")
  1041. if priority.TaskCreating == "C2C" {
  1042. priority.TaskCreating = ""
  1043. }
  1044. //check PAD timer
  1045. if priority.AllTasks.PADOCC.Priority > priority.AllTasks.C2C.Priority {
  1046. if active.QueueTimer != nil {
  1047. if active.QueueTimer.Stop() {
  1048. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  1049. } else {
  1050. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  1051. }
  1052. }
  1053. }
  1054. } else if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "NOANSWER" {
  1055. //lfshook.NewLogger().Infof("===%s======%s", event["Event"], event["DialStatus"])
  1056. time.Sleep(time.Millisecond * 300)
  1057. //check resume
  1058. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1059. if ok {
  1060. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1061. }
  1062. }
  1063. //TMS Answer PAD (有时asterisk反馈的BridgeEnter事件有问题,根据dialend判断)
  1064. if utils.IsPAIU(event["CallerIDName"]) && utils.IsPACU(event["CallerIDNum"]) && event["DialStatus"] == "ANSWER" && event["Exten"] == "0403" {
  1065. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1066. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1067. //=============================
  1068. task := priority.TaskInfo{
  1069. RunChannel: event["Channel"],
  1070. LocalChan: event["Channel"],
  1071. RunType: "PAD-TMS",
  1072. Priority: priority.AllTasks.PADTMS.Priority,
  1073. ConfbridgeID: "",
  1074. Running: true,
  1075. }
  1076. priority.RegistryTask.Register("PAD-TMS", task)
  1077. if priority.TaskCreating == "PAD-TMS" {
  1078. priority.TaskCreating = ""
  1079. }
  1080. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1081. if active.ActivedCab == "1" {
  1082. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1083. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1084. } else if active.ActivedCab == "8" {
  1085. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1086. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1087. } else if active.ActivedCab == "" {
  1088. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1089. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1090. }
  1091. }
  1092. case "BridgeLeave":
  1093. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  1094. //Cab Cab end
  1095. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["Exten"] == "0400" {
  1096. alstatus.PaStatus(event["CallerIDNum"], "C2C", "end")
  1097. priority.RegistryTask.StopAndUnregister("C2C")
  1098. time.Sleep(time.Millisecond * 300)
  1099. SetPadTimer()
  1100. //check resume
  1101. taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
  1102. if ok {
  1103. ICPConfbridgeReinvite(task.ConfbridgeID, taskName)
  1104. //check resume
  1105. /*if taskName == "CPA" {
  1106. alstatus.PaStatus("", "CPA", "start")
  1107. } else if taskName == "EMG" {
  1108. alstatus.PaStatus("", "EMG", "start")
  1109. } else if taskName == "SPC" {
  1110. alstatus.PaStatus("", "SPC", "start")
  1111. } else if taskName == "DCS" {
  1112. alstatus.PaStatus("", "DCS", "start")
  1113. } else if taskName == "STN" {
  1114. alstatus.PaStatus("", "STN", "start")
  1115. } else if taskName == "CHK" {
  1116. alstatus.PaStatus("", "CHK", "start")
  1117. }*/
  1118. }
  1119. }
  1120. case "ExtensionStatus":
  1121. //lfshook.NewLogger().Infof("=========event:%s Ext:%s status:%s ", event["Event"], event["Exten"], event["StatusText"])
  1122. //update extension status
  1123. if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
  1124. if len(event["Exten"]) > 3 && utils.IsPAIU(event["Exten"]) {
  1125. alstatus.AlarmStatus(event["Exten"], event["StatusText"]) // PAD idle + unavailable
  1126. }
  1127. }
  1128. case "BridgeEnter": // TMS-ICP answer PAD; PACU connect ICP
  1129. lfshook.NewLogger().Infof("=========event:%s callerid-num:%s callerid-name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1130. //PAD-OCC start
  1131. if utils.IsIO(event["CallerIDNum"]) && utils.IsPAIU(event["ConnectedLineNum"]) {
  1132. if priority.PADOccStart == 0 {
  1133. if priority.TaskCreating == "PAD-OCC" {
  1134. priority.TaskCreating = ""
  1135. }
  1136. alstatus.OccPad("start")
  1137. priority.PADOccStart = 1
  1138. }
  1139. if priority.PADStart == 0 {
  1140. alstatus.PaStatus("", "PAD", "start")
  1141. priority.PADStart = 1
  1142. }
  1143. }
  1144. /*if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected -----TMS Answer PAD
  1145. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  1146. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  1147. //=============================
  1148. task := priority.TaskInfo{
  1149. RunChannel: event["Channel"],
  1150. LocalChan: event["Channel"],
  1151. RunType: "PAD-TMS",
  1152. Priority: priority.AllTasks.PADTMS.Priority,
  1153. ConfbridgeID: "",
  1154. Running: true,
  1155. }
  1156. priority.RegistryTask.Register("PAD-TMS", task)
  1157. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1158. if active.ActivedCab == "1" {
  1159. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1160. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1161. } else if active.ActivedCab == "8" {
  1162. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  1163. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  1164. } else if active.ActivedCab == "" {
  1165. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  1166. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  1167. }
  1168. } else*/if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0405" { // PAD connect ICP-TMS;PACU not available
  1169. //=============================
  1170. task := priority.TaskInfo{
  1171. RunChannel: event["Channel"],
  1172. LocalChan: event["Channel"],
  1173. RunType: "PAD-TMS",
  1174. Priority: priority.AllTasks.PADTMS.Priority,
  1175. ConfbridgeID: "",
  1176. Running: true,
  1177. }
  1178. priority.RegistryTask.Register("PAD-TMS", task)
  1179. if priority.TaskCreating == "PAD-TMS" {
  1180. priority.TaskCreating = ""
  1181. }
  1182. lfshook.NewLogger().Infof("=========PAD-TMS answer==ListAll===%+v", priority.RegistryTask.ListAll())
  1183. alstatus.AlarmStatus(event["CallerIDNum"], "connect") // PAD connect ICP-TMS
  1184. }
  1185. }
  1186. }
  1187. func StartAMI(connectOKCallBack func(), handleEvents []func(event map[string]string)) {
  1188. lfshook.NewLogger().Info("Start AMI")
  1189. settings := &amigo.Settings{
  1190. Host: configs.ConfigGlobal.AsteriskAMIHost,
  1191. Port: configs.ConfigGlobal.AsteriskAMIPort,
  1192. Username: configs.ConfigGlobal.AsteriskAMIUser,
  1193. Password: configs.ConfigGlobal.AsteriskAMISecret,
  1194. LogLevel: logrus.ErrorLevel}
  1195. //lfshook.NewLogger().Infof("ami setting: %+v", settings)
  1196. AminInstance = amigo.New(settings, lfshook.NewLogger())
  1197. AminInstance.EventOn(func(payload ...interface{}) {
  1198. // lfshook.NewLogger().Infof("ami event on %+v", payload[0])
  1199. event := payload[0].(map[string]string)
  1200. go HandleAMI(event)
  1201. for _, handle := range handleEvents {
  1202. go handle(event)
  1203. }
  1204. })
  1205. AminInstance.ConnectOn(func(payload ...interface{}) {
  1206. //lfshook.NewLogger().Infof("ami connect on %+v", payload[0])
  1207. if payload[0] == pkg.Connect_OK {
  1208. connectOKCallBack()
  1209. } else {
  1210. lfshook.NewLogger().Errorf("ami connect failure %+v", payload)
  1211. //active.Master = false
  1212. }
  1213. })
  1214. AminInstance.Connect()
  1215. }
  1216. func Connected() bool {
  1217. if AminInstance != nil {
  1218. return AminInstance.Connected()
  1219. }
  1220. return false
  1221. }