index.go 54 KB

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