index.go 60 KB

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