index.go 52 KB

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