index.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. package action
  2. import (
  3. "fmt"
  4. "os"
  5. "pbx-api-gin/internal/app/stc/active"
  6. "pbx-api-gin/internal/app/stc/priority"
  7. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  8. "pbx-api-gin/internal/pkg/configs"
  9. "pbx-api-gin/pkg/lfshook"
  10. "pbx-api-gin/pkg/utils"
  11. "sort"
  12. "strconv"
  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 priority.RunningType != "PAD-ICP" && priority.RunningType != "PAD-TMS" && priority.RunningType != "PAD-OCC" {
  26. break
  27. }
  28. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] != "" {
  29. chans, err := CoreShowChannels()
  30. if err != nil {
  31. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  32. }
  33. //PA interrupt PAD
  34. if event["Digit"] == "#" {
  35. //1. Redirect the connected PAD to 0300
  36. if priority.RunningType == "PAD-ICP" || priority.RunningType == "PAD-TMS" {
  37. for _, ret := range chans {
  38. //hangup pad call ICP channel
  39. if utils.IsPAIU(ret.CallerIDNum) {
  40. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  41. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  42. if err != nil {
  43. lfshook.NewLogger().Errorf("Redirect %+v", err)
  44. }
  45. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  46. active.NotifyPaiu(number, "hold")
  47. //HangupAllLocalChan()
  48. }
  49. }
  50. }
  51. for _, ret := range chans {
  52. //hangup pad call ICP channel
  53. if utils.IsPAIU(ret.CallerIDNum) {
  54. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  55. lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  56. Hangup(ret.Channel)
  57. }
  58. }
  59. }
  60. HangupRunningTask("PA")
  61. } else if priority.RunningType == "PAD-OCC" {
  62. //2. hangup connected PAD, and redirect all PADs in 0301 to 0300
  63. priority.OCCAnswer = 0
  64. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  65. if err != nil {
  66. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  67. }
  68. for _, caller := range resCaller.Entrys {
  69. lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  70. time.Sleep(time.Millisecond * 100)
  71. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  72. }
  73. /* for _, ret := range chans {
  74. //hangup pad call ICP channel
  75. if utils.IsPAIU(ret.CallerIDNum) {
  76. lfshook.NewLogger().Infof("===DTMFBegin=hangup=%+v==%s=== ", ret.Channel, ret.ChannelStateDesc)
  77. if ret.ConnectedLineNum == "<unknown>" && ret.ChannelStateDesc != "Ring" { //redirect pad chanspy channel
  78. Hangup(ret.Channel)
  79. } else if utils.IsICP(ret.ConnectedLineNum) && ret.ChannelStateDesc == "Up" {
  80. Hangup(ret.Channel)
  81. }
  82. }
  83. }*/
  84. HangupRunningTask("PA")
  85. //HangupAllLocalChan()
  86. }
  87. } else if event["Digit"] == "*" { //Cab cab interrupt PAD
  88. lfshook.NewLogger().Infof("===Cab cab interrupt PAD====== ")
  89. if priority.RunningType == "PAD-ICP" || priority.RunningType == "PAD-TMS" {
  90. for _, ret := range chans {
  91. if utils.IsPAIU(ret.CallerIDNum) {
  92. if ret.ConnectedLineNum == "<unknown>" && ret.ChannelStateDesc == "Up" { //redirect pad chanspy channel
  93. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  94. if err != nil {
  95. lfshook.NewLogger().Errorf("Redirect %+v", err)
  96. }
  97. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  98. active.NotifyPaiu(number, "hold")
  99. //HangupAllLocalChan()
  100. } else if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  101. lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  102. Hangup(ret.Channel)
  103. }
  104. }
  105. }
  106. priority.InterruptedPad = priority.RunningType
  107. HangupAllLocalChan()
  108. if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  109. Hangup(priority.RunningPATaskChan)
  110. }
  111. priority.CleanPriorityTag()
  112. //HangupRunningTask("C2C")
  113. } else if priority.RunningType == "PAD-OCC" {
  114. HangupICP()
  115. }
  116. }
  117. }
  118. case "UserEvent": // RCD filename; PA;CPA; CabCab
  119. lfshook.NewLogger().Infof("========event:%s File:%s", event["Event"], event["FILENAME"])
  120. if event["UserEvent"] == "CallType" && (event["Type"] == "PA" || event["Type"] == "CPA") { //PA start; check manual PA priority
  121. //PA & CPA interrupt others
  122. if utils.IsICP(event["CallerIDNum"]) {
  123. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  124. Hangup(event["CallerIDNum"])
  125. }
  126. if priority.CheckPriority("ManuPa") {
  127. //hangup others if priority is higher
  128. lfshook.NewLogger().Infof("=====UserEvent===event======PA start=======")
  129. HangupRunningTask("PA") //PA interrupt other
  130. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.ManuPa)
  131. } else {
  132. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  133. }
  134. } else if utils.IsIO(event["CallerIDNum"]) { // CPA
  135. if priority.CheckPriority("CPA") {
  136. if active.ActivedCab == "" { //No active Signal on both side,Hangup caller
  137. Hangup(event["CallerIDNum"])
  138. } else if active.ActivedCab == "1" && event["CallerIDNum"] == "1411" {
  139. Hangup(event["CallerIDNum"])
  140. } else if active.ActivedCab == "8" && event["CallerIDNum"] == "1481" {
  141. Hangup(event["CallerIDNum"])
  142. }
  143. //hangup others if priority is higher
  144. if priority.RunningType != "" {
  145. HangupRunningTask("CPA") //CPA interrupt other
  146. } else {
  147. //call to ICP ; can not stop calling ICP
  148. lfshook.NewLogger().Info("==Running CabCab , CPA start call ICP(ICP is on the phone)=====")
  149. }
  150. } else {
  151. Hangup(event["CallerIDNum"]) //lowwer priority ,hangup caller
  152. }
  153. }
  154. } else if event["UserEvent"] == "CallType" && event["Type"] == "C2C" { //CabCab start; check cab cab priority
  155. if priority.CheckPriority("CabCab") { // interrupt OCC-PAD
  156. //C2C start PAD interrupt
  157. if priority.RunningType == "PAD-ICP" || priority.RunningType == "PAD-TMS" || priority.RunningType == "PA" {
  158. HangupRunningTask("C2C")
  159. //Hangup the other ICP
  160. } else {
  161. lfshook.NewLogger().Infof("========CabCab hangup other one====caller %s", event["CallerIDNum"])
  162. if event["CallerIDNum"] == "2311" {
  163. Hangup("2381")
  164. } else {
  165. Hangup("2311")
  166. }
  167. }
  168. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CabCab)
  169. } else { // hangup caller; C2C start failed
  170. lfshook.NewLogger().Infof("========CabCab hangup caller====%s", event["CallerIDNum"])
  171. Hangup(event["CallerIDNum"])
  172. }
  173. //Get record file name ,encode and upload
  174. } else if event["UserEvent"] == "SetRecordFile" {
  175. if configs.ConfigGlobal.ProcessRecord != "yes" {
  176. break
  177. }
  178. //检测录音文件是否存在;最多检测5次,每次间隔1秒
  179. var fileExists bool
  180. for i := 0; i < 5; i++ {
  181. time.Sleep(time.Second) // 等待1秒
  182. if _, err := os.Stat(event["FILENAME"]); err == nil {
  183. fileExists = true
  184. lfshook.NewLogger().Infof("File found: %s", event["FILENAME"])
  185. break
  186. } else if os.IsNotExist(err) {
  187. lfshook.NewLogger().Infof("File not found (attempt %d): %s", i+1, event["FILENAME"])
  188. } else {
  189. lfshook.NewLogger().Infof("Error checking file: %v", err)
  190. }
  191. }
  192. if !fileExists { //5秒内没有生成录音文件
  193. lfshook.NewLogger().Infof("File %s not found after 5 attempts", event["FILENAME"])
  194. break
  195. }
  196. //获取录音文件时长,检测录音文件是否超过3min;
  197. duration, err := utils.GetDuration(event["FILENAME"])
  198. if err != nil {
  199. utils.Logger.Printf("Get duration err: %+v", err)
  200. break
  201. }
  202. //lfshook.NewLogger().Infof("==========duration===== %d", duration)
  203. //转wav文件的采样率到22kHz,并切割位180秒每段
  204. var FileNames []string
  205. if duration >= 600 { //超过600秒的超长文件,不处理
  206. lfshook.NewLogger().Infof("File over 600 sec, Ignored !")
  207. break
  208. } else if duration < 600 { //小于600秒文件进行转换和切割
  209. FileNames, err = utils.ConvertAndSegmentWAV(event["FILENAME"], strings.Replace(event["FILENAME"], ".wav", "", -1))
  210. if err != nil {
  211. lfshook.NewLogger().Infof("Get duration err: %+v", err)
  212. break
  213. }
  214. //lfshook.NewLogger().Infof("=============== File %+v found after convert", FileNames)
  215. }
  216. //执行加密操作,并将录音信息写入日志文件
  217. DstFile := ""
  218. if len(FileNames) > 0 { // 文件切割之后进入循环处理
  219. for _, filepath := range FileNames {
  220. file := strings.Replace(filepath, ".wav", "", -1)
  221. DstFile = fmt.Sprintf("%s-encrypted.wav", file)
  222. lfshook.NewLogger().Infof("Bin file====%s", DstFile)
  223. err = utils.AudioFileEncode(DstFile, filepath)
  224. if err != nil {
  225. lfshook.NewLogger().Infof("Encode file: %s err: %+v", DstFile, err)
  226. continue
  227. }
  228. //切割&加密之后发送生成的文件名到STC;
  229. alstatus.SendRecordFile(DstFile, event["RecordType"])
  230. trainInfo = active.ActivedCab
  231. if strings.Contains(event["FILENAME"], "PAD") {
  232. _, caller, callee := utils.GetPadInfo(event["FILENAME"])
  233. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: PAD , CabNumber: %c , LocationCode: %c, Connected: %s, RecordFileName:%s", trainInfo, caller[2], caller[3], callee, DstFile)
  234. } else if strings.Contains(event["FILENAME"], "PA") {
  235. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  236. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: PA, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  237. } else if strings.Contains(event["FILENAME"], "C2C") {
  238. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  239. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: CabCab, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  240. } else if strings.Contains(event["FILENAME"], "CPA") {
  241. _, caller, _ := utils.GetPadInfo(event["FILENAME"])
  242. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: CPA, Caller: %s, RecordFileName: %s", trainInfo, caller, DstFile)
  243. } else if strings.Contains(event["FILENAME"], "EMG") {
  244. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: EMG, RecordFileName: %s", trainInfo, DstFile)
  245. } else if strings.Contains(event["FILENAME"], "STN") {
  246. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: STN, RecordFileName: %s", trainInfo, DstFile)
  247. } else if strings.Contains(event["FILENAME"], "DCS") {
  248. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: DCS, RecordFileName: %s", trainInfo, DstFile)
  249. } else if strings.Contains(event["FILENAME"], "SPC") {
  250. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: SPC, RecordFileName: %s", trainInfo, DstFile)
  251. } else if strings.Contains(event["FILENAME"], "CHK") {
  252. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: Self Check, RecordFileName: %s", trainInfo, DstFile)
  253. } else if strings.Contains(event["FILENAME"], "TONE") {
  254. utils.Logger.Printf("Train Information: CabNumber %s, MessageType: TONE Test, RecordFileName: %s", trainInfo, DstFile)
  255. }
  256. }
  257. } else {
  258. lfshook.NewLogger().Infof("No files to upload!!!")
  259. break
  260. }
  261. }
  262. case "Hangup":
  263. lfshook.NewLogger().Infof("=========%s", event["Event"])
  264. //OCC answer PAD, hangup, redirect the next PAD to OCC
  265. if utils.IsIO(event["CallerIDNum"]) && (event["ConnectedLineNum"] == "ano1" || event["ConnectedLineNum"] == "ano8") && event["Context"] == "default" {
  266. lfshook.NewLogger().Infof("====Hangup OCC-PAD=====%+v", event)
  267. // OCC hangup detected, hangup other running channels
  268. HangupAllLocalChan()
  269. if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  270. Hangup(priority.RunningPATaskChan)
  271. }
  272. res, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  273. if res.Calls == "0" { //OCC queue is empty
  274. priority.CleanPriorityTag()
  275. alstatus.OccPad("end")
  276. priority.OCCAnswer = 0
  277. priority.PADOccStart = 0
  278. if active.EMGCONFID != "" {
  279. priority.RunningType = "EMG"
  280. priority.RunningTypePriority = active.InfoEMG.Priority
  281. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  282. time.Sleep(time.Millisecond * 300)
  283. active.ICPCONBID = active.EMGCONFID
  284. EMGConfbridgeReinvite(active.EMGCONFID)
  285. return
  286. }
  287. if active.ICPCONBID != "" {
  288. ConfbridgeKick(active.ICPCONBID, "all")
  289. active.ICPCONBID = ""
  290. }
  291. break
  292. } else { //OCC queue is not empty
  293. // HangupAllLocalChan()
  294. time.Sleep(time.Millisecond * 300)
  295. lfshook.NewLogger().Infof("====Start OCC-PAD===next==%+v", res)
  296. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  297. time.Sleep(time.Second)
  298. PADChan := ""
  299. for _, chanEntry := range res.Entrys {
  300. lfshook.NewLogger().Infof("====PAD answered by OCC1 pos:%s===chan:%s=", chanEntry.Position, chanEntry.Channel)
  301. if chanEntry.Position == "1" {
  302. PADChan = chanEntry.Channel
  303. break
  304. }
  305. }
  306. if PADChan != "" {
  307. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  308. alstatus.AlarmStatus(Ext, "connect")
  309. go RedirectInQueue(PADChan, "1411", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  310. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  311. } else {
  312. lfshook.NewLogger().Infof("===OCC-QueueStatus==PADCchan NULL")
  313. }
  314. break
  315. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  316. time.Sleep(time.Second)
  317. PADChan := ""
  318. for _, chanEntry := range res.Entrys {
  319. lfshook.NewLogger().Infof("====PAD answered by OCC1 pos:%s===chan:%s=", chanEntry.Position, chanEntry.Channel)
  320. if chanEntry.Position == "1" {
  321. PADChan = chanEntry.Channel
  322. break
  323. }
  324. }
  325. if PADChan != "" {
  326. Ext := strings.Split(strings.Split(res.Entrys[0].Channel, "/")[1], "-")[0]
  327. alstatus.AlarmStatus(Ext, "connect")
  328. go RedirectInQueue(PADChan, "1481", "pad-page-occ-icp", Ext) //PAD Page(OCC+ICPs)
  329. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC1
  330. } else {
  331. lfshook.NewLogger().Infof("===OCC-QueueStatus==PADCchan NULL")
  332. }
  333. break
  334. }
  335. }
  336. }
  337. if utils.IsPAIU(event["CallerIDNum"]) { // PAD hangup, check if PAD all end, send PAD end status
  338. number := strings.Split(strings.Split(event["Channel"], "-")[0], "/")[1]
  339. lfshook.NewLogger().Infof("===hangup PAD =======================%s", number)
  340. if utils.IsPAIU(number) {
  341. res, _ := QueueStatus("0300", "") // check ICP queue ,if empty PAD end
  342. res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
  343. lfshook.NewLogger().Infof("==calls:%s===calls1:%s====", res.Calls, res1.Calls)
  344. if res.Calls == "0" && res1.Calls == "0" {
  345. //priority.CleanPriorityTag()
  346. //HangupAllLocalChan()
  347. if priority.PADStart == 1 {
  348. SetPadTimer()
  349. alstatus.PaStatus(event["CallerIDNum"], "PAD", "end")
  350. priority.PADStart = 0
  351. priority.CleanPriorityTag()
  352. if active.EMGCONFID != "" {
  353. priority.RunningType = "EMG"
  354. priority.RunningTypePriority = active.InfoEMG.Priority
  355. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  356. time.Sleep(time.Millisecond * 300)
  357. active.ICPCONBID = active.EMGCONFID
  358. EMGConfbridgeReinvite(active.EMGCONFID)
  359. return
  360. }
  361. if active.ICPCONBID != "" {
  362. ConfbridgeKick(active.ICPCONBID, "all")
  363. active.ICPCONBID = ""
  364. }
  365. }
  366. priority.ICPAnswer = 0
  367. priority.OCCAnswer = 0
  368. break
  369. }
  370. }
  371. }
  372. case "QueueCallerJoin":
  373. lfshook.NewLogger().Infof("=========%s", event["Event"])
  374. if priority.OCCAnswer == 1 && event["Queue"] == "0300" { //New PAD Goto the OCC queue in the first time, if OCC answered
  375. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  376. go RedirectInQueue(event["CallerIDNum"], "0301", "queues-occ", event["CallerIDNum"])
  377. break
  378. }
  379. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) && event["Queue"] == "0300" { // Alarm join the queue, PAD in the queue
  380. alstatus.AlarmStatus(event["CallerIDNum"], "queue") //send status to STC
  381. ICPQueue, err := QueueStatus("0300", "") // check ICP queue, get entries
  382. if err != nil {
  383. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  384. return
  385. }
  386. if priority.ICPAnswer == 0 && ICPQueue.Calls == "1" { //ICP did not answer any first call to the ICP queue ; Ready to Set Occ Queue Timer
  387. toRunpriority := priority.GetPriorityByKey("PAD-ICP")
  388. //PAD press key
  389. lfshook.NewLogger().Infof("===QueueCallerJoin===runing:%d=====toRun:=%d==Status:%s", priority.RunningTypePriority, toRunpriority, event["ChannelStateDesc"])
  390. if (priority.RunningTypePriority > toRunpriority || priority.RunningTypePriority == 0) && event["ChannelStateDesc"] != "Up" {
  391. active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  392. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  393. if err != nil {
  394. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  395. return
  396. }
  397. if res.Calls == "0" { // OCC queue empty
  398. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  399. if err != nil {
  400. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  401. return
  402. }
  403. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  404. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  405. })
  406. for _, caller := range resCaller.Entrys {
  407. priority.ICPAnswer = 0
  408. lfshook.NewLogger().Infof("====QueueCallerJoin==QueueTimer===%s", event["Event"])
  409. lfshook.NewLogger().Infof("====Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  410. //order by pos
  411. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  412. time.Sleep(time.Millisecond * 100) //200 ms delay
  413. }
  414. }
  415. })
  416. }
  417. }
  418. break
  419. }
  420. //first PAD caller goto OCC
  421. //OCC dial PACUs;
  422. //PAD Page OCC+ICPs;
  423. if utils.IsPAIU(event["CallerIDNum"]) && event["Queue"] == "0301" && priority.OCCAnswer == 0 { // The first PAD to OCC ,caller is PAD
  424. if priority.CheckPriority("PAD-OCC") {
  425. HangupRunningTask("PAD-OCC") //PAD-OCC interrupt other
  426. priority.OCCAnswer = 1
  427. time.Sleep(time.Millisecond * 300)
  428. if active.ActivedCab == "1" && ExtenStatus("1411") == "Idle" { //check active and OCC status
  429. /*if priority.PADOccStart == 0 {
  430. alstatus.OccPad("start")
  431. priority.PADOccStart = 1
  432. if priority.PADStart == 0 {
  433. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  434. priority.PADStart = 1
  435. }
  436. }*/
  437. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  438. go RedirectInQueue(event["Channel"], "1411", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  439. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano1", "ano1", "1") // PACUs dial OCC1
  440. } else if active.ActivedCab == "8" && ExtenStatus("1481") == "Idle" {
  441. /*if priority.PADOccStart == 0 {
  442. alstatus.OccPad("start")
  443. priority.PADOccStart = 1
  444. if priority.PADStart == 0 {
  445. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  446. priority.PADStart = 1
  447. }
  448. }*/
  449. alstatus.AlarmStatus(event["CallerIDNum"], "connect")
  450. go RedirectInQueue(event["Channel"], "1481", "pad-page-occ-icp", event["CallerIDNum"]) //PAD Page(OCC+ICPs)
  451. go Dial("0401", "0512", "pad-rule-pacus-occ", "ano8", "ano8", "8") // PACUs dial OCC8
  452. }
  453. } else {
  454. lfshook.NewLogger().Infof("====PAD-OCC Priority false===")
  455. }
  456. }
  457. case "ConfbridgeJoin":
  458. lfshook.NewLogger().Infof("=========%+v", event["Event"])
  459. //set priority and send PA status msg
  460. switch event["CallerIDName"] {
  461. case "EMG":
  462. if event["Exten"] == "0502" {
  463. priority.RunningPATaskChan = event["Channel"]
  464. priority.RunningType = "EMG"
  465. //Pa status report
  466. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.EMG)
  467. //active.ICPCONBID = event["Conference"]
  468. //active.ICPCONBTYP = "EMG"
  469. active.EMGCONFID = event["Conference"]
  470. //================================
  471. active.InfoEMG.RunChannel = event["Channel"]
  472. active.InfoEMG.Priority = priority.RunningTypePriority
  473. active.InfoEMG.RunType = "EMG"
  474. alstatus.PaStatus("", "EMG", "start")
  475. return
  476. }
  477. case "SPC":
  478. if event["Exten"] == "0505" {
  479. priority.RunningPATaskChan = event["Channel"]
  480. priority.RunningType = "SPC"
  481. //Pa status report
  482. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.SPC)
  483. active.ICPCONBID = event["Conference"]
  484. active.ICPCONBTYP = "SPC"
  485. alstatus.PaStatus("", "SPC", "start")
  486. return
  487. }
  488. case "DCS":
  489. if event["Exten"] == "0504" {
  490. priority.RunningPATaskChan = event["Channel"]
  491. priority.RunningType = "DCS"
  492. //Pa status report
  493. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.DCS)
  494. active.ICPCONBID = event["Conference"]
  495. active.ICPCONBTYP = "DCS"
  496. alstatus.PaStatus("", "DCS", "start")
  497. return
  498. }
  499. case "STN":
  500. if event["Exten"] == "0503" {
  501. priority.RunningPATaskChan = event["Channel"]
  502. priority.RunningType = "STN"
  503. //Pa status report
  504. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.STN)
  505. active.ICPCONBID = event["Conference"]
  506. active.ICPCONBTYP = "STN"
  507. alstatus.PaStatus("", "STN", "start")
  508. return
  509. }
  510. case "CHK":
  511. if event["Exten"] == "0510" {
  512. priority.RunningPATaskChan = event["Channel"]
  513. priority.RunningType = "CHK"
  514. //Pa status report
  515. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CHK)
  516. active.ICPCONBID = event["Conference"]
  517. active.ICPCONBTYP = "CHK"
  518. alstatus.PaStatus("", "CHK", "start")
  519. return
  520. }
  521. case "VOL": // tone-test
  522. if event["Exten"] == "0513" {
  523. priority.RunningPATaskChan = event["Channel"]
  524. priority.RunningType = "VOL"
  525. //Pa status report
  526. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.VOL)
  527. active.ICPCONBID = event["Conference"]
  528. active.ICPCONBTYP = "VOL"
  529. alstatus.PaStatus("", "VOL", "start")
  530. return
  531. }
  532. }
  533. //get confbridge id for join back to confbridge
  534. if event["Context"] == "pad-page-occ-icp" && utils.IsPAIU(event["CallerIDNum"]) {
  535. active.ICPCONBID = event["Conference"] //pad-occ
  536. }
  537. //Send PA start msg to STC
  538. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA start
  539. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.ManuPa)
  540. alstatus.PaStatus(event["CallerIDNum"], "PA", "start")
  541. priority.RunningPATaskChan = event["Channel"]
  542. priority.RunningType = "PA"
  543. active.ICPCONBID = event["Conference"] //cpa
  544. active.ICPCONBTYP = "PA"
  545. break
  546. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA start
  547. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CPA)
  548. alstatus.PaStatus(event["CallerIDNum"], "CPA", "start")
  549. priority.RunningPATaskChan = event["Channel"]
  550. priority.RunningType = "CPA"
  551. //active.ICPCONBID = event["Conference"] //cpa icp
  552. //active.ICPCONBTYP = "CPA"
  553. active.CPACONFID = event["Conference"] //cpa
  554. //=====================================
  555. active.InfoCPA.RunChannel = event["Channel"]
  556. active.InfoCPA.Priority = priority.RunningTypePriority
  557. active.InfoCPA.RunType = "CPA"
  558. return
  559. }
  560. //ICP answer PAD;PACUs connected ICP
  561. //PAD chanspy ICP1
  562. //ICP8 call PAD
  563. if event["ConnectedLineNum"] == "ani1" && event["Exten"] == "0511" { //PAD answered by ICP; PACUs connected ICP1
  564. lfshook.NewLogger().Infof("====PAD answered by ICP1:%s=====", event["ConnectedLineName"])
  565. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  566. priority.RunningPATaskChan = event["Channel"]
  567. priority.RunningType = "PAD-ICP"
  568. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADICP)
  569. go RedirectInQueue(event["ConnectedLineName"], "2311", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP1
  570. if ExtenStatus("2381") == "Idle" {
  571. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "8") // PAD call ICP8
  572. }
  573. }
  574. if event["ConnectedLineNum"] == "ani8" && event["Exten"] == "0511" { //PAD ansered by ICP8; PACUs connected ICP8
  575. lfshook.NewLogger().Infof("====PAD answered by ICP8:%s=====", event["ConnectedLineName"])
  576. alstatus.AlarmStatus(event["ConnectedLineName"], "connect")
  577. priority.RunningPATaskChan = event["Channel"]
  578. priority.RunningType = "PAD-ICP"
  579. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADICP)
  580. go RedirectInQueue(event["ConnectedLineName"], "2381", "chanspy-rule-whisper", event["ConnectedLineName"]) //PAD chanspy(EqW) ICP8
  581. if ExtenStatus("2311") == "Idle" {
  582. go Dial("0402", event["ConnectedLineName"], "call-pad-rule", event["ConnectedLineName"], event["ConnectedLineName"], "1") // PAD call ICP1
  583. }
  584. break
  585. }
  586. //OCC answer PAD;Set the task channel
  587. if utils.IsPAIU(event["CallerIDNum"]) && utils.IsIO(event["Exten"]) && event["Context"] == "pad-page-occ-icp" { //PAD Page OCC1+ICPs connected
  588. lfshook.NewLogger().Infof("====PAD answered by OCC:====")
  589. priority.RunningPATaskChan = event["Channel"]
  590. priority.RunningType = "PAD-OCC"
  591. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADOCC)
  592. break
  593. }
  594. case "ConfbridgeLeave":
  595. lfshook.NewLogger().Infof("=========%s", event["Event"])
  596. if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0500" { // PA end
  597. SetPadTimer()
  598. priority.CleanPriorityTag()
  599. alstatus.PaStatus(event["CallerIDNum"], "PA", "end")
  600. lfshook.NewLogger().Infof("====active.EMGCONFID:%s ====active.ICPCONBID:=%s", active.EMGCONFID, active.ICPCONBID)
  601. if active.EMGCONFID != "" {
  602. priority.RunningType = "EMG"
  603. priority.RunningTypePriority = active.InfoEMG.Priority
  604. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  605. time.Sleep(time.Millisecond * 300)
  606. active.ICPCONBID = active.EMGCONFID
  607. EMGConfbridgeReinvite(active.EMGCONFID)
  608. return
  609. }
  610. if active.ICPCONBID != "" {
  611. ConfbridgeKick(active.ICPCONBID, "all")
  612. active.ICPCONBID = ""
  613. }
  614. } else if utils.IsIO(event["CallerIDNum"]) && event["Exten"] == "0501" { //CPA end
  615. SetPadTimer()
  616. priority.CleanPriorityTag()
  617. alstatus.PaStatus(event["CallerIDNum"], "CPA", "end")
  618. if active.EMGCONFID != "" {
  619. priority.RunningType = "EMG"
  620. priority.RunningTypePriority = active.InfoEMG.Priority
  621. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  622. time.Sleep(time.Millisecond * 300)
  623. active.ICPCONBID = active.EMGCONFID
  624. EMGConfbridgeReinvite(active.EMGCONFID)
  625. return
  626. }
  627. if active.ICPCONBID != "" {
  628. ConfbridgeKick(active.ICPCONBID, "all")
  629. active.ICPCONBID = ""
  630. }
  631. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  632. } else if event["CallerIDName"] == "EMG" && event["Exten"] == "0502" { // EMG broadcast hangup
  633. SetPadTimer()
  634. priority.CleanPriorityTag()
  635. alstatus.PaStatus(event["CallerIDName"], "EMG", "end")
  636. } else if event["CallerIDName"] == "STN" && event["Exten"] == "0503" {
  637. SetPadTimer()
  638. priority.CleanPriorityTag()
  639. alstatus.PaStatus(event["CallerIDName"], "STN", "end")
  640. if active.EMGCONFID != "" {
  641. priority.RunningType = "EMG"
  642. priority.RunningTypePriority = active.InfoEMG.Priority
  643. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  644. time.Sleep(time.Millisecond * 300)
  645. active.ICPCONBID = active.EMGCONFID
  646. EMGConfbridgeReinvite(active.EMGCONFID)
  647. return
  648. }
  649. if active.ICPCONBID != "" {
  650. ConfbridgeKick(active.ICPCONBID, "all")
  651. active.ICPCONBID = ""
  652. }
  653. } else if event["CallerIDName"] == "DCS" && event["Exten"] == "0504" {
  654. SetPadTimer()
  655. priority.CleanPriorityTag()
  656. alstatus.PaStatus(event["CallerIDName"], "DCS", "end")
  657. if active.EMGCONFID != "" {
  658. priority.RunningType = "EMG"
  659. priority.RunningTypePriority = active.InfoEMG.Priority
  660. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  661. time.Sleep(time.Millisecond * 300)
  662. active.ICPCONBID = active.EMGCONFID
  663. EMGConfbridgeReinvite(active.EMGCONFID)
  664. return
  665. }
  666. if active.ICPCONBID != "" {
  667. ConfbridgeKick(active.ICPCONBID, "all")
  668. active.ICPCONBID = ""
  669. }
  670. } else if event["CallerIDName"] == "SPC" && event["Exten"] == "0505" {
  671. SetPadTimer()
  672. priority.CleanPriorityTag()
  673. alstatus.PaStatus(event["CallerIDName"], "SPC", "end")
  674. if active.EMGCONFID != "" {
  675. priority.RunningType = "EMG"
  676. priority.RunningTypePriority = active.InfoEMG.Priority
  677. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  678. time.Sleep(time.Millisecond * 300)
  679. active.ICPCONBID = active.EMGCONFID
  680. EMGConfbridgeReinvite(active.EMGCONFID)
  681. return
  682. }
  683. if active.ICPCONBID != "" {
  684. ConfbridgeKick(active.ICPCONBID, "all")
  685. active.ICPCONBID = ""
  686. }
  687. } else if event["CallerIDName"] == "CHK" && event["Exten"] == "0510" {
  688. SetPadTimer()
  689. priority.CleanPriorityTag()
  690. alstatus.PaStatus(event["CallerIDName"], "CHK", "end")
  691. if active.EMGCONFID != "" {
  692. priority.RunningType = "EMG"
  693. priority.RunningTypePriority = active.InfoEMG.Priority
  694. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  695. time.Sleep(time.Millisecond * 300)
  696. active.ICPCONBID = active.EMGCONFID
  697. EMGConfbridgeReinvite(active.EMGCONFID)
  698. return
  699. }
  700. if active.ICPCONBID != "" {
  701. ConfbridgeKick(active.ICPCONBID, "all")
  702. active.ICPCONBID = ""
  703. }
  704. } else if event["CallerIDName"] == "VOL" && event["Exten"] == "0513" {
  705. SetPadTimer()
  706. priority.CleanPriorityTag()
  707. alstatus.PaStatus(event["CallerIDName"], "VOL", "end")
  708. if active.EMGCONFID != "" {
  709. priority.RunningType = "EMG"
  710. priority.RunningTypePriority = active.InfoEMG.Priority
  711. priority.RunningPATaskChan = active.InfoEMG.RunChannel
  712. time.Sleep(time.Millisecond * 300)
  713. active.ICPCONBID = active.EMGCONFID
  714. EMGConfbridgeReinvite(active.EMGCONFID)
  715. return
  716. }
  717. if active.ICPCONBID != "" {
  718. ConfbridgeKick(active.ICPCONBID, "all")
  719. active.ICPCONBID = ""
  720. }
  721. }
  722. case "DialEnd":
  723. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  724. //Cab Cab start
  725. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "ANSWER" {
  726. priority.TmpRunningTypePriority = priority.RunningTypePriority
  727. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.CabCab)
  728. priority.TmpRunningType = priority.RunningType
  729. priority.RunningType = "C2C"
  730. alstatus.PaStatus(event["CallerIDNum"], "C2C", "start")
  731. } else if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["DialStatus"] == "NOANSWER" {
  732. lfshook.NewLogger().Infof("===%s======%s", event["Event"], priority.RunningType)
  733. //ICPs go back to confbridge
  734. //ICPConfbridgeReinvite(active.ICPCONBID, active.ICPCONBTYP)
  735. time.Sleep(time.Millisecond * 300)
  736. //check cpa & emg & opa confbridge
  737. if active.CPACONFID != "" && active.EMGCONFID != "" && priority.RunningTypePriority != 0 {
  738. if priority.GetPriorityByKey("CPA") < priority.GetPriorityByKey("EMG") && priority.GetPriorityByKey("CPA") < priority.RunningTypePriority {
  739. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  740. active.CPACONFID = ""
  741. } else if priority.GetPriorityByKey("EMG") < priority.GetPriorityByKey("CPA") && priority.GetPriorityByKey("EMG") < priority.RunningTypePriority {
  742. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  743. active.EMGCONFID = ""
  744. } else if priority.RunningTypePriority < priority.GetPriorityByKey("EMG") && priority.GetPriorityByKey("CPA") > priority.RunningTypePriority {
  745. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  746. active.ICPCONBID = ""
  747. active.ICPCONBTYP = ""
  748. }
  749. //check cpa & emg confbridge
  750. } else if active.CPACONFID != "" && active.EMGCONFID != "" {
  751. if priority.GetPriorityByKey("CPA") < priority.GetPriorityByKey("EMG") {
  752. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  753. active.CPACONFID = ""
  754. } else {
  755. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  756. active.EMGCONFID = ""
  757. }
  758. //check emg & opa confbridge
  759. } else if active.EMGCONFID != "" && priority.RunningTypePriority != 0 {
  760. if priority.GetPriorityByKey("EMG") < priority.RunningTypePriority {
  761. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  762. active.EMGCONFID = ""
  763. } else {
  764. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  765. active.ICPCONBID = ""
  766. active.ICPCONBTYP = ""
  767. }
  768. //check cpa & opa confbridge
  769. } else if active.CPACONFID != "" && priority.RunningTypePriority != 0 {
  770. if priority.GetPriorityByKey("CPA") < priority.RunningTypePriority {
  771. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  772. active.CPACONFID = ""
  773. } else {
  774. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  775. active.ICPCONBID = ""
  776. active.ICPCONBTYP = ""
  777. }
  778. } else if active.CPACONFID != "" {
  779. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  780. active.CPACONFID = ""
  781. } else if active.EMGCONFID != "" {
  782. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  783. active.EMGCONFID = ""
  784. } else if priority.RunningTypePriority != 0 {
  785. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  786. active.ICPCONBID = ""
  787. active.ICPCONBTYP = ""
  788. }
  789. }
  790. case "BridgeLeave":
  791. //lfshook.NewLogger().Infof("=========%s", event["Event"])
  792. //Cab Cab end
  793. if utils.IsICP(event["CallerIDNum"]) && utils.IsICP(event["ConnectedLineNum"]) && event["Exten"] == "0400" {
  794. SetPadTimer()
  795. alstatus.PaStatus(event["CallerIDNum"], "C2C", "end")
  796. priority.RunningTypePriority = priority.TmpRunningTypePriority
  797. priority.RunningType = priority.TmpRunningType
  798. time.Sleep(time.Millisecond * 300)
  799. //check cpa & emg & opa confbridge
  800. if active.CPACONFID != "" && active.EMGCONFID != "" && priority.RunningTypePriority != 0 {
  801. if priority.GetPriorityByKey("CPA") < priority.GetPriorityByKey("EMG") && priority.GetPriorityByKey("CPA") < priority.RunningTypePriority {
  802. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  803. active.CPACONFID = ""
  804. } else if priority.GetPriorityByKey("EMG") < priority.GetPriorityByKey("CPA") && priority.GetPriorityByKey("EMG") < priority.RunningTypePriority {
  805. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  806. active.EMGCONFID = ""
  807. } else if priority.RunningTypePriority < priority.GetPriorityByKey("EMG") && priority.GetPriorityByKey("CPA") > priority.RunningTypePriority {
  808. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  809. active.ICPCONBID = ""
  810. active.ICPCONBTYP = ""
  811. }
  812. //check cpa & emg confbridge
  813. } else if active.CPACONFID != "" && active.EMGCONFID != "" {
  814. if priority.GetPriorityByKey("CPA") < priority.GetPriorityByKey("EMG") {
  815. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  816. active.CPACONFID = ""
  817. } else {
  818. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  819. active.EMGCONFID = ""
  820. }
  821. //check emg & opa confbridge
  822. } else if active.EMGCONFID != "" && priority.RunningTypePriority != 0 {
  823. if priority.GetPriorityByKey("EMG") < priority.RunningTypePriority {
  824. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  825. active.EMGCONFID = ""
  826. } else {
  827. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  828. active.ICPCONBID = ""
  829. active.ICPCONBTYP = ""
  830. }
  831. //check cpa & opa confbridge
  832. } else if active.CPACONFID != "" && priority.RunningTypePriority != 0 {
  833. if priority.GetPriorityByKey("CPA") < priority.RunningTypePriority {
  834. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  835. active.CPACONFID = ""
  836. } else {
  837. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  838. active.ICPCONBID = ""
  839. active.ICPCONBTYP = ""
  840. }
  841. } else if active.CPACONFID != "" {
  842. ICPConfbridgeReinvite(active.CPACONFID, "CPA")
  843. active.CPACONFID = ""
  844. } else if active.EMGCONFID != "" {
  845. ICPConfbridgeReinvite(active.EMGCONFID, "EMG")
  846. active.EMGCONFID = ""
  847. } else if priority.RunningTypePriority != 0 {
  848. ICPConfbridgeReinvite(active.ICPCONBID, priority.RunningType)
  849. active.ICPCONBID = ""
  850. active.ICPCONBTYP = ""
  851. }
  852. //check other broadcast confbridge
  853. //ICPs go back to confbridge
  854. priority.TmpRunningType = ""
  855. priority.TmpRunningTypePriority = 0
  856. }
  857. case "ExtensionStatus":
  858. //lfshook.NewLogger().Infof("=========event:%s Ext:%s status:%s ", event["Event"], event["Exten"], event["StatusText"])
  859. //update extension status
  860. if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
  861. if len(event["Exten"]) > 3 && utils.IsPAIU(event["Exten"]) {
  862. alstatus.AlarmStatus(event["Exten"], event["StatusText"]) // PAD idle + unavailable
  863. }
  864. }
  865. case "BridgeEnter": // TMS-ICP answer PAD; PACU connect ICP
  866. lfshook.NewLogger().Infof("=========event:%s callerid:%s", event["Event"], event["CallerIDNum"])
  867. if utils.IsIO(event["CallerIDNum"]) && utils.IsPAIU(event["ConnectedLineNum"]) {
  868. if priority.PADOccStart == 0 {
  869. alstatus.OccPad("start")
  870. priority.PADOccStart = 1
  871. if priority.PADStart == 0 {
  872. alstatus.PaStatus(event["CallerIDNum"], "PAD", "start")
  873. priority.PADStart = 1
  874. }
  875. }
  876. }
  877. if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected
  878. lfshook.NewLogger().Infof("====BridgeEnter==IN action===%s===ID:%s Name:%s", event["Event"], event["CallerIDNum"], event["CallerIDName"])
  879. alstatus.AlarmStatus(event["CallerIDName"], "connect") // Alarm connected
  880. priority.RunningPATaskChan = event["Channel"]
  881. priority.RunningType = "PAD-TMS"
  882. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADTMS)
  883. if active.ActivedCab == "1" {
  884. go RedirectInQueue(event["CallerIDName"], "2311", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP1
  885. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2381", "2381", "8") //ICP8---call----PAD
  886. } else if active.ActivedCab == "8" {
  887. go RedirectInQueue(event["CallerIDName"], "2381", "chanspy-rule-whisper", "") //PAD chanspy(EqW) ICP8
  888. go Dial("0403", event["CallerIDName"], "call-pad-rule", "2311", "2311", "1") //ICP1---call----PAD
  889. }
  890. } else if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0405" { // PAD connect ICP-TMS;PACU not available
  891. priority.RunningPATaskChan = event["Channel"]
  892. priority.RunningType = "PAD-TMS"
  893. priority.RunningTypePriority, _ = strconv.Atoi(priority.Priority.PADTMS)
  894. lfshook.NewLogger().Infof("====send pad status=====")
  895. alstatus.AlarmStatus(event["CallerIDNum"], "connect") // PAD connect ICP-TMS
  896. }
  897. }
  898. }
  899. func StartAMI(connectOKCallBack func(), handleEvents []func(event map[string]string)) {
  900. lfshook.NewLogger().Info("Start AMI")
  901. settings := &amigo.Settings{
  902. Host: configs.ConfigGlobal.AsteriskAMIHost,
  903. Port: configs.ConfigGlobal.AsteriskAMIPort,
  904. Username: configs.ConfigGlobal.AsteriskAMIUser,
  905. Password: configs.ConfigGlobal.AsteriskAMISecret,
  906. LogLevel: logrus.ErrorLevel}
  907. lfshook.NewLogger().Infof("ami setting: %+v", settings)
  908. AminInstance = amigo.New(settings, lfshook.NewLogger())
  909. AminInstance.EventOn(func(payload ...interface{}) {
  910. // lfshook.NewLogger().Infof("ami event on %+v", payload[0])
  911. event := payload[0].(map[string]string)
  912. go HandleAMI(event)
  913. for _, handle := range handleEvents {
  914. go handle(event)
  915. }
  916. })
  917. AminInstance.ConnectOn(func(payload ...interface{}) {
  918. lfshook.NewLogger().Infof("ami connect on %+v", payload[0])
  919. if payload[0] == pkg.Connect_OK {
  920. connectOKCallBack()
  921. } else {
  922. lfshook.NewLogger().Errorf("ami connect failure %+v", payload)
  923. }
  924. })
  925. AminInstance.Connect()
  926. }
  927. func Connected() bool {
  928. if AminInstance != nil {
  929. return AminInstance.Connected()
  930. }
  931. return false
  932. }