index.go 50 KB

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