index.go 53 KB

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