call.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. package action
  2. import (
  3. "errors"
  4. "fmt"
  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/pkg/lfshook"
  9. "pbx-api-gin/pkg/utils"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444",
  15. "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483",
  16. "2484", "2485", "2411", "2412"}
  17. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  18. var Speakers = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181", "2311", "2381"}
  19. // Function triggered before no cab occupied signal interrupt
  20. func InActiveHangup() {
  21. if active.ActivedCab == "" {
  22. HangupTask("PA")
  23. HangupTask("CPA")
  24. HangupTask("VOL")
  25. //HangupTask("PAD-OCC")
  26. } else {
  27. HangupTask("PA")
  28. HangupTask("PAD-OCC")
  29. HangupTask("CPA")
  30. HangupTask("EMG")
  31. HangupTask("SPC")
  32. HangupTask("DCS")
  33. HangupTask("STN")
  34. HangupTask("CHK")
  35. HangupTask("VOL")
  36. Hangup("2311") //hangup cabcab
  37. }
  38. }
  39. // Hangup 挂断指定分机或通道
  40. func Hangup(channel string) {
  41. lfshook.NewLogger().Infof("Hangup extensions/channel :%s", channel)
  42. if !utils.IsChannel(channel) {
  43. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  44. }
  45. action := map[string]string{
  46. "Action": "hangup",
  47. "Channel": channel,
  48. }
  49. //lfshook.NewLogger().Infof("hangup action :%+v", action)
  50. if _, _, err := AminInstance.Send(action); err != nil {
  51. lfshook.NewLogger().Errorf("Hangup %+v", err)
  52. }
  53. }
  54. // Hangup 挂断所有分机,除指定分机和PAD
  55. func HangupAllExcept(caller string) {
  56. //all PACU
  57. for _, ret := range Pacus {
  58. Hangup(ret)
  59. }
  60. switch caller {
  61. case "2311":
  62. Hangup("1411") //IO1
  63. Hangup("1481") //IO8
  64. //Hangup("2311") //ICP1
  65. Hangup("2381") //ICP8
  66. case "2381":
  67. Hangup("1411") //IO1
  68. Hangup("1481") //IO8
  69. Hangup("2311") //ICP1
  70. //Hangup("2381") //ICP8
  71. case "1411":
  72. //Hangup("1411") //IO1
  73. Hangup("1481") //IO8
  74. Hangup("2311") //ICP1
  75. Hangup("2381") //ICP8
  76. case "1481":
  77. Hangup("1411") //IO1
  78. //Hangup("1481") //IO8
  79. Hangup("2311") //ICP1
  80. Hangup("2381") //ICP8
  81. case "":
  82. Hangup("1411") //IO1
  83. Hangup("1481") //IO8
  84. Hangup("2311") //ICP1
  85. Hangup("2381") //ICP8
  86. }
  87. }
  88. // Hangup task
  89. func HangupTask(TaskName string) {
  90. lfshook.NewLogger().Infof("HangupTask Check TaskName:%s ", TaskName)
  91. if TaskName == "PAD-OCC" {
  92. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  93. if err != nil {
  94. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  95. return
  96. }
  97. //lfshook.NewLogger().Infof("============QueueStatus ret :%+v", resCaller)
  98. if resCaller != nil {
  99. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", resCaller.Entrys)
  100. if resCaller.Entrys != nil {
  101. for _, caller := range resCaller.Entrys {
  102. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  103. time.Sleep(time.Millisecond * 50)
  104. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  105. }
  106. }
  107. }
  108. }
  109. taskInfo, ok := priority.RegistryTask.Get(TaskName)
  110. if ok {
  111. HangupAllLocalChan()
  112. ConfbridgeKick(taskInfo.ConfbridgeID, "all")
  113. Hangup(taskInfo.RunChannel)
  114. }
  115. }
  116. // interrupt the running task
  117. func InterruptRunningTask(toRunTask string) string {
  118. utils.LoggerDebug.Printf("InterruptRunningTask TorunType:%s", toRunTask)
  119. var task priority.TaskInfo
  120. var taskName string
  121. var ok bool
  122. lfshook.NewLogger().Infof("InterruptRunningTask toRuntask:%s ", toRunTask)
  123. if toRunTask != "PA" && toRunTask != "PAD-ICP" && toRunTask != "PAD-TMS" { // ignore C2C
  124. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask1()
  125. if !ok {
  126. return ""
  127. }
  128. } else { // have to check C2C
  129. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask()
  130. if !ok {
  131. return ""
  132. }
  133. }
  134. utils.LoggerDebug.Printf("InterruptRunningTask RunningTask:%+v", taskName)
  135. //lfshook.NewLogger().Infof("InterruptRunningTask RunningTask:%+v ", task)
  136. //same type return
  137. if toRunTask == taskName {
  138. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" || toRunTask == "PAD-OCC" {
  139. //Auto Answer PAD-OCC one by one, clean the old confbridge and channels before answer a new PAD .
  140. if toRunTask == "PAD-OCC" {
  141. Hangup(task.RunChannel) //pad
  142. ConfbridgeKick(task.ConfbridgeID, "all")
  143. HangupIO() //io
  144. //lfshook.NewLogger().Infof("===InterruptRunningTask=ret==== ")
  145. }
  146. return taskName
  147. }
  148. }
  149. //pad all reset
  150. if toRunTask == "AlarmHoldResetAll" {
  151. HangupAllLocalChan()
  152. return taskName
  153. }
  154. switch task.RunType {
  155. case "SPC":
  156. if toRunTask == "C2C" {
  157. HangupICP()
  158. } else {
  159. ConfbridgeKick(task.ConfbridgeID, "all")
  160. }
  161. time.Sleep(time.Millisecond * 200)
  162. case "CHK":
  163. if toRunTask == "C2C" {
  164. HangupICP()
  165. } else {
  166. ConfbridgeKick(task.ConfbridgeID, "all")
  167. }
  168. time.Sleep(time.Millisecond * 200)
  169. case "DCS":
  170. if toRunTask == "STN" {
  171. return taskName
  172. } else if toRunTask == "C2C" {
  173. HangupICP()
  174. } else {
  175. ConfbridgeKick(task.ConfbridgeID, "all")
  176. }
  177. time.Sleep(time.Millisecond * 200)
  178. case "STN":
  179. if toRunTask == "DCS" {
  180. return taskName
  181. } else if toRunTask == "C2C" {
  182. HangupICP()
  183. } else {
  184. ConfbridgeKick(task.ConfbridgeID, "all")
  185. }
  186. time.Sleep(time.Millisecond * 200)
  187. case "CPA":
  188. //kick CPA members
  189. if toRunTask != "C2C" {
  190. CPAConfbridgeKick(task)
  191. //alstatus.PaStatus("", "CPA", "end")
  192. } else if toRunTask == "C2C" {
  193. HangupICP()
  194. }
  195. time.Sleep(time.Millisecond * 200)
  196. case "EMG":
  197. //kick EMG members
  198. if toRunTask == "EMG" {
  199. ConfbridgeKick(task.ConfbridgeID, "all")
  200. } else if toRunTask != "C2C" {
  201. EMGConfbridgeKick(task)
  202. //alstatus.PaStatus("", "EMG", "end")
  203. } else if toRunTask == "C2C" {
  204. HangupICP()
  205. }
  206. time.Sleep(time.Millisecond * 200)
  207. case "C2C": // Interrupt C2C task running,
  208. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  209. HangupICP()
  210. return taskName
  211. }
  212. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  213. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  214. break
  215. }
  216. priority.InterruptedPad = "PAD-ICP"
  217. //lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
  218. chans, err := CoreShowChannels()
  219. if err != nil {
  220. lfshook.NewLogger().Infof("InterruptRunningTask CoreShowChannels err:%+v", err)
  221. return taskName
  222. }
  223. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  224. for _, ret := range chans {
  225. // Redirect the connected PAD to 0300
  226. if utils.IsPAIU(ret.CallerIDNum) {
  227. //lfshook.NewLogger().Infof("====interrupt PAD ==== %+v ", ret)
  228. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  229. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  230. if err != nil {
  231. lfshook.NewLogger().Infof("InterruptRunningTask Redirect err:%+v", err)
  232. return taskName
  233. }
  234. //lfshook.NewLogger().Infof("====interrupt PAD =1111=== %+v ", ret)
  235. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  236. active.NotifyPaiu(number, "hold")
  237. //HangupAllLocalChan()
  238. }
  239. }
  240. }
  241. for _, ret := range chans {
  242. //hangup pad call ICP channel
  243. if utils.IsPAIU(ret.CallerIDNum) {
  244. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  245. //lfshook.NewLogger().Infof("Hangup PAD Channel :%+v", ret.Channel)
  246. Hangup(ret.Channel)
  247. }
  248. }
  249. }
  250. alstatus.PaStatus("", "PAD", "end")
  251. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  252. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  253. //2. hangup task channel (ICP + PACU)
  254. //lfshook.NewLogger().Infof("===InterruptRunningTask=2. hangup task channel === ")
  255. HangupAllLocalChan()
  256. HangupICP()
  257. case "PAD-OCC": // Interrupt PAD-OCC task running,
  258. if toRunTask == "C2C" {
  259. HangupICP()
  260. break
  261. } else {
  262. priority.InterruptedPad = "PAD-OCC"
  263. priority.OCCAnswer = 0
  264. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  265. if err != nil {
  266. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  267. return taskName
  268. }
  269. if resCaller == nil {
  270. return taskName
  271. }
  272. if resCaller.Entrys != nil {
  273. for _, caller := range resCaller.Entrys {
  274. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  275. time.Sleep(time.Millisecond * 50)
  276. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  277. }
  278. }
  279. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  280. //2. Hangup connected PAD
  281. //lfshook.NewLogger().Infof("===InterruptRunningTask=Hangup connected PAD=== ")
  282. Hangup(task.RunChannel)
  283. //3. interrupt OCC-PAD Hangup OI
  284. if active.ActivedCab == "1" {
  285. Hangup("1411")
  286. } else if active.ActivedCab == "8" {
  287. Hangup("1481")
  288. } else {
  289. if active.ActivedCabDelay == "1" {
  290. Hangup("1411")
  291. } else if active.ActivedCabDelay == "8" {
  292. Hangup("1481")
  293. } else {
  294. Hangup("1411")
  295. }
  296. }
  297. HangupAllLocalChan()
  298. ConfbridgeKick(task.ConfbridgeID, "all")
  299. //occ pad end
  300. if priority.PADOccStart == 1 {
  301. alstatus.OccPad("end")
  302. alstatus.PaStatus("", "PAD", "end")
  303. priority.PADOccStart = 0
  304. priority.OCCAnswer = 0
  305. }
  306. }
  307. default:
  308. lfshook.NewLogger().Infof("InterruptRunningTask goto default !")
  309. //if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  310. // Hangup(priority.RunningPATaskChan)
  311. //}
  312. if toRunTask != "C2C" && task.RunType != "PA" {
  313. Hangup(task.RunChannel)
  314. ConfbridgeKick(task.ConfbridgeID, "all")
  315. }
  316. }
  317. return taskName
  318. }
  319. // Hangup all ICP
  320. func HangupICP() {
  321. Hangup("2311") //ICP1
  322. Hangup("2381") //ICP8
  323. }
  324. // Hangup all IO
  325. func HangupIO() {
  326. Hangup("1411") //IO1
  327. Hangup("1481") //IO8
  328. }
  329. // Hangup all PACU
  330. func HangupAllPACU() {
  331. //all PACU
  332. for _, ret := range Pacus {
  333. Hangup(ret)
  334. }
  335. }
  336. func HangupAllLocalChan() {
  337. chans, err := CoreShowChannels()
  338. if err != nil {
  339. lfshook.NewLogger().Infof("CoreShowChannels %+v", err)
  340. return
  341. }
  342. for _, ret := range chans {
  343. if strings.Contains(ret.Channel, "Local") && !strings.Contains(ret.Channel, "0502@default") {
  344. //lfshook.NewLogger().Infof("HangupAllLocalChan=====hangup========= %+v", ret)
  345. Hangup(ret.Channel)
  346. }
  347. }
  348. }
  349. // Hangup all PACU
  350. func HangupAllPAD() {
  351. //all PAD
  352. for _, ret := range Pads {
  353. Hangup(ret)
  354. }
  355. }
  356. // Hangup all calls
  357. func HangupAll() {
  358. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  359. }
  360. // Dial 拨打号码
  361. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  362. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  363. action := map[string]string{
  364. "Action": "Originate",
  365. "Channel": chanel,
  366. "Exten": dst,
  367. "Context": dialrule,
  368. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  369. "Priority": "1",
  370. "Variable": fmt.Sprintf("CAB=%s", callType),
  371. "async": "true",
  372. }
  373. //lfshook.NewLogger().Infof("================dial action %+v", action)
  374. res, _, err := AminInstance.Send(action)
  375. if err != nil {
  376. lfshook.NewLogger().Errorf("%+v", err)
  377. }
  378. lfshook.NewLogger().Info(res)
  379. }
  380. // Dial 拨打号码
  381. func DialICP(src, dst, dialrule, callerID, callerName string) {
  382. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  383. action := map[string]string{
  384. "Action": "Originate",
  385. "Channel": chanel,
  386. "Exten": dst,
  387. "Context": dialrule,
  388. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  389. "Priority": "1",
  390. "Variable": fmt.Sprintf("CBID=%s", callerID),
  391. "async": "true",
  392. }
  393. lfshook.NewLogger().Infof("dial action %+v", action)
  394. res, _, err := AminInstance.Send(action)
  395. if err != nil {
  396. lfshook.NewLogger().Errorf("%+v", err)
  397. }
  398. lfshook.NewLogger().Info(res)
  399. }
  400. // 获取分机状态
  401. func ExtenStatus(exten string) (Status string) {
  402. action := map[string]string{
  403. "Action": "ExtensionState",
  404. "Exten": exten,
  405. "Context": "default",
  406. }
  407. res, _, err := AminInstance.Send(action)
  408. if err != nil {
  409. lfshook.NewLogger().Errorf("%+v", err)
  410. return ""
  411. }
  412. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  413. return res["StatusText"]
  414. }
  415. // PACU ChanSpy
  416. func ChanSpy(src, dst string, whisper, bargein bool) {
  417. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  418. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  419. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  420. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  421. /*
  422. if whisper {
  423. data = fmt.Sprintf("%s,w", data)
  424. }
  425. if bargein {
  426. data = fmt.Sprintf("%s,B", data)
  427. }
  428. */
  429. action := map[string]string{
  430. "Action": "Originate",
  431. "Channel": channel, // 不存在的通话
  432. "Application": "ChanSpy",
  433. "Data": data, // 存在的通话
  434. "CallerID": dst,
  435. "Async": "true",
  436. }
  437. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  438. _, _, err := AminInstance.Send(action)
  439. if err != nil {
  440. lfshook.NewLogger().Errorf("%+v", err)
  441. }
  442. }
  443. // Redirect 转接
  444. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  445. //callerID := "redirect"
  446. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  447. if !utils.IsChannel(channel) {
  448. //callerID = channel
  449. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  450. return err
  451. }
  452. }
  453. action := map[string]string{
  454. "Action": "Redirect",
  455. "Channel": channel,
  456. "Exten": dst,
  457. "Context": dialrule,
  458. "CallerID": callerID,
  459. "Priority": "1",
  460. "async": "true",
  461. }
  462. lfshook.NewLogger().Infof("RedirectInQueue: %+v", action)
  463. res, _, err := AminInstance.Send(action)
  464. if err != nil {
  465. lfshook.NewLogger().Error(err)
  466. }
  467. lfshook.NewLogger().Info(res)
  468. return err
  469. }
  470. // Redirect 转接
  471. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  472. //callerID := "redirect"
  473. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  474. if !utils.IsChannel(channel) {
  475. callerID = channel
  476. if channel, err = GetChannelByExten(channel); err != nil {
  477. return err
  478. }
  479. }
  480. action := map[string]string{
  481. "Action": "Redirect",
  482. "Channel": channel,
  483. "Exten": dst,
  484. "Context": dialrule,
  485. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  486. "Priority": "1",
  487. "async": "true",
  488. }
  489. lfshook.NewLogger().Infof("Redirect: %+v", action)
  490. res, _, err := AminInstance.Send(action)
  491. if err != nil {
  492. lfshook.NewLogger().Error(err)
  493. }
  494. lfshook.NewLogger().Info(res)
  495. return err
  496. }
  497. func SetPadTimer() {
  498. //toRunPadpriority := priority.GetPriorityByKey(priority.InterruptedPad) //Get PAD priori 获取之前打断的报警优先级
  499. toRunPadpriority := priority.GetPriorityByKey("PAD-ICP")
  500. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  501. if ok {
  502. if taskTmp.Priority < toRunPadpriority { //higher priority task running ,do not set timer
  503. utils.LoggerDebug.Printf("PAD SetPadTimer runing priority:%d toRun priority:%d , return !", taskTmp.Priority, toRunPadpriority)
  504. return
  505. }
  506. }
  507. utils.LoggerDebug.Printf("PAD SetPadTimer check 0300")
  508. res, err := QueueStatus("0300", "")
  509. if err != nil {
  510. lfshook.NewLogger().Infof("QueueStatus err%+v", err)
  511. return
  512. }
  513. if res == nil {
  514. return
  515. }
  516. if res.Calls != "0" {
  517. utils.LoggerDebug.Printf("PAD SetPadTimer Set QueueTimer timeout %ds !", active.PADTimeout)
  518. //active.SetTimer = true
  519. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  520. if active.QueueTimer != nil {
  521. if active.QueueTimer.Stop() {
  522. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  523. } else {
  524. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  525. }
  526. }
  527. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  528. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  529. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  530. //if both not active , return
  531. if active.ActivedCab == "" {
  532. return
  533. }
  534. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  535. if err != nil {
  536. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  537. return
  538. }
  539. if res == nil {
  540. return
  541. }
  542. if res.Calls == "0" { // OCC queue empty
  543. /*resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  544. if err != nil {
  545. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  546. return
  547. }
  548. if resCaller.Entrys != nil {
  549. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  550. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  551. })
  552. for _, caller := range resCaller.Entrys {
  553. priority.ICPAnswer = 0
  554. //order by pos
  555. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  556. time.Sleep(time.Millisecond * 100) //200 ms delay
  557. }
  558. }*/
  559. for _, ret := range alstatus.PadQueues {
  560. //utils.LoggerDebug.Printf("PAD to OCC exten:%s", ret.Exten)
  561. priority.ICPAnswer = 0
  562. //order by pos
  563. RedirectInQueue(ret.Exten, "0301", "queues-occ", ret.Exten) // redirect All ICP-PAD redirect to OCC queue
  564. time.Sleep(time.Millisecond * 100)
  565. }
  566. }
  567. })
  568. }
  569. }
  570. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  571. action := map[string]string{
  572. "Action": "ConfbridgeKick",
  573. "Conference": confnum,
  574. "Channel": channel,
  575. }
  576. res, _, err = AminInstance.Send(action)
  577. if err != nil {
  578. return nil, err
  579. }
  580. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  581. if res["Response"] != "Success" {
  582. return nil, errors.New(res["Message"])
  583. }
  584. return res, nil
  585. }
  586. func CPAConfbridgeKick(confInfo priority.TaskInfo) (res map[string]string, err error) {
  587. if !confInfo.Running {
  588. utils.LoggerDebug.Printf("CPA CPAConfbridgeKick , not running, return .")
  589. return
  590. }
  591. utils.LoggerDebug.Printf("CPA CPAConfbridgeKick , kick all members .")
  592. chans, err := ConfbridgeList(confInfo.ConfbridgeID)
  593. if err != nil {
  594. return nil, errors.New(res["Message"])
  595. }
  596. for _, confChan := range chans {
  597. if active.TrainDevide == 1 {
  598. if active.CabNum == "1" { //在1车车厢内
  599. if !strings.Contains(confChan, "PJSIP/1411") {
  600. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  601. }
  602. } else { //在8车车厢内
  603. if !strings.Contains(confChan, "PJSIP/1481") {
  604. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  605. }
  606. }
  607. } else {
  608. if active.ActivedCab != "1" {
  609. if !strings.Contains(confChan, "PJSIP/1411") {
  610. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  611. }
  612. } else {
  613. if !strings.Contains(confChan, "PJSIP/1481") {
  614. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  615. }
  616. }
  617. }
  618. }
  619. if confInfo.Running {
  620. alstatus.PaStatus("", "CPA", "end")
  621. }
  622. priority.RegistryTask.UpdateStatus("CPA", false)
  623. return res, nil
  624. }
  625. func CPAConfbridgeReinvite(task priority.TaskInfo) bool {
  626. if task.Running {
  627. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , already running, return .")
  628. return false
  629. }
  630. time.Sleep(time.Millisecond * 100)
  631. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , resume CPA .")
  632. chans, err := ConfbridgeList(task.ConfbridgeID)
  633. if len(chans) > 1 || err != nil {
  634. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , return . ConfbridgeList chans > 1 or err !")
  635. return false
  636. }
  637. if priority.PAInterrupt == 1 { // PA 打断标签判断
  638. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , PA ready to go , return !")
  639. return false
  640. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  641. priority.TaskCreating = "CPA"
  642. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  643. if utils.IsICP(ext) {
  644. if priority.CABInterrupt == 1 {
  645. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  646. continue
  647. }
  648. }
  649. ConfbridgeReinvite(ext, "call-speakers-cpa", task.ConfbridgeID)
  650. }
  651. //task.Running = true
  652. priority.RegistryTask.UpdateStatus("CPA", true)
  653. time.Sleep(time.Millisecond * 200) //等待通道建立
  654. priority.TaskCreating = ""
  655. alstatus.PaStatus("", "CPA", "start")
  656. priority.CPAActived = 0
  657. } else if priority.TaskCreating != "" {
  658. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , other PA ready to go , return !")
  659. return false
  660. } else {
  661. priority.TaskCreating = "CPA"
  662. for _, ext := range Speakers {
  663. ConfbridgeReinvite(ext, "call-speakers-cpa", task.ConfbridgeID)
  664. }
  665. //task.Running = true
  666. priority.RegistryTask.UpdateStatus("CPA", true)
  667. time.Sleep(time.Millisecond * 200) //等待通道建立
  668. priority.TaskCreating = ""
  669. alstatus.PaStatus("", "CPA", "start")
  670. priority.CPAActived = 0
  671. }
  672. return true
  673. }
  674. func EMGConfbridgeKick(confInfo priority.TaskInfo) (res map[string]string, err error) {
  675. if !confInfo.Running {
  676. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , not running, return .")
  677. return
  678. }
  679. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , kick all members .")
  680. chans, err := ConfbridgeList(confInfo.ConfbridgeID)
  681. if err != nil {
  682. return nil, errors.New(res["Message"])
  683. }
  684. for _, confChan := range chans {
  685. if !strings.Contains(confChan, "0502@default") {
  686. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  687. }
  688. }
  689. if confInfo.Running {
  690. alstatus.PaStatus("", "EMG", "end")
  691. }
  692. //confInfo.Running = false
  693. priority.RegistryTask.UpdateStatus("EMG", false)
  694. return res, nil
  695. }
  696. func EMGConfbridgeReinvite(task priority.TaskInfo) {
  697. if priority.CPAActived == 1 && priority.AllTasks.EMG.Priority > priority.AllTasks.CPA.Priority {
  698. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , CPA actived , return .")
  699. return
  700. }
  701. //if priority.CABInterrupt != 1 {
  702. // time.Sleep(time.Millisecond * 100)
  703. //} else {
  704. //time.Sleep(time.Second * 1) //wait cpa start first
  705. //}
  706. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , resume EMG .")
  707. if task.Running {
  708. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , already running ,return .")
  709. return
  710. }
  711. if priority.PAInterrupt == 1 { // PA 打断标签判断
  712. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , PA ready to go , return !")
  713. return
  714. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  715. priority.TaskCreating = "EMG"
  716. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  717. if utils.IsICP(ext) {
  718. if priority.CABInterrupt == 1 {
  719. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get CABCAB Interrupt resume EMG , ignore %s continue .", ext)
  720. continue
  721. }
  722. }
  723. ConfbridgeReinvite(ext, "call-speakers-emg", task.ConfbridgeID)
  724. }
  725. //task.Running = true
  726. priority.RegistryTask.UpdateStatus("EMG", true)
  727. time.Sleep(time.Millisecond * 200) //等待通道建立
  728. priority.TaskCreating = ""
  729. alstatus.PaStatus("", "EMG", "start")
  730. } else if priority.TaskCreating != "" {
  731. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , other PA ready to go , return !")
  732. return
  733. } else {
  734. priority.TaskCreating = "EMG"
  735. for _, ext := range Speakers {
  736. ConfbridgeReinvite(ext, "call-speakers-emg", task.ConfbridgeID)
  737. }
  738. //task.Running = true
  739. priority.RegistryTask.UpdateStatus("EMG", true)
  740. time.Sleep(time.Millisecond * 200) //等待通道建立
  741. priority.TaskCreating = ""
  742. alstatus.PaStatus("", "EMG", "start")
  743. }
  744. }
  745. func ConfbridgeList(confnum string) (chans []string, err error) {
  746. action := map[string]string{
  747. "Action": "ConfbridgeList",
  748. "Conference": confnum,
  749. }
  750. res, events, err := AminInstance.Send(action)
  751. if err != nil {
  752. return nil, err
  753. }
  754. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  755. if res["Response"] == "Success" {
  756. for _, event := range events {
  757. if event.Data["Event"] == "ConfbridgeList" {
  758. chans = append(chans, event.Data["Channel"])
  759. }
  760. }
  761. return chans, nil
  762. } else {
  763. return nil, errors.New(res["Message"])
  764. }
  765. }
  766. func ConfbridgeReinvite(src, context, confID string) {
  767. if ExtenStatus(src) != "Idle" {
  768. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  769. return
  770. }
  771. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  772. action := map[string]string{
  773. "Action": "Originate",
  774. "Channel": chanel,
  775. "Exten": "000",
  776. "Context": "confbridge-join",
  777. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  778. "Priority": "1",
  779. "Variable": fmt.Sprintf("CBID=%s", confID),
  780. "async": "true",
  781. }
  782. lfshook.NewLogger().Infof("dial action %+v", action)
  783. res, _, err := AminInstance.Send(action)
  784. if err != nil {
  785. lfshook.NewLogger().Errorf("%+v", err)
  786. }
  787. lfshook.NewLogger().Info(res)
  788. }
  789. func ICPConfbridgeReinvite(confID, paType string) {
  790. if priority.CABInterrupt == 1 || priority.CABInterrupt == 1 {
  791. utils.LoggerDebug.Printf("ICPConfbridgeReinvite, Get PA/CABCAB Interrupt resume %s faild .", paType)
  792. return
  793. }
  794. switch paType {
  795. case "PAD-OCC":
  796. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  797. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  798. case "CPA":
  799. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  800. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  801. case "EMG":
  802. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  803. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  804. case "SPC":
  805. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  806. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  807. case "STN":
  808. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  809. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  810. case "DCS":
  811. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  812. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  813. case "CHK":
  814. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  815. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  816. case "VOL":
  817. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  818. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  819. }
  820. }
  821. var waitMutex sync.Mutex
  822. // 设置全局变量控制任务创建过程,避免被其他任务打乱任务创建过程
  823. func WaitTaskCreate(task string, args ...string) { //arg1(task chan)
  824. utils.LoggerDebug.Printf("%s check task creating ..... ,TaskCreating = %s", task, priority.TaskCreating)
  825. waitMutex.Lock()
  826. defer waitMutex.Unlock()
  827. switch priority.TaskCreating {
  828. case "C2C":
  829. if /*task == "PA" ||*/ task == "CPA" {
  830. //获取正在创建的任务的优先级
  831. //priorityC2C := priority.GetPriorityByKey("C2C")
  832. //获取将要创建的任务的优先级
  833. //priorityTask := priority.GetPriorityByKey(task)
  834. //比较优先级,确定是否终止正在创建的任务
  835. /*if priorityC2C < priorityTask {
  836. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  837. //结束task(CPA)
  838. if len(args) > 0 {
  839. Hangup(args[0])
  840. goto DELAY
  841. }
  842. } else {
  843. //结束C2C,PA 则不需要挂断ICP(由ICP自行处理优先级)
  844. utils.LoggerDebug.Printf("%s check task C2C creating , hangup C2C ICPs ", task)
  845. HangupICP()
  846. goto DELAY
  847. }*/
  848. }
  849. return
  850. case "CPA":
  851. if task == "PA" /*|| task == "C2C"*/ {
  852. //获取正在创建的任务的优先级
  853. priorityCPA := priority.GetPriorityByKey("CPA")
  854. //获取将要创建的任务的优先级
  855. priorityTask := priority.GetPriorityByKey(task)
  856. //比较优先级,确定是否终止正在创建的任务
  857. if priorityCPA < priorityTask {
  858. utils.LoggerDebug.Printf("%s check task CPA creating , hangup %s %s", task, task, args[0])
  859. //结束task(PA,CPA)
  860. if len(args) > 0 {
  861. Hangup(args[0])
  862. goto DELAY
  863. }
  864. } else {
  865. //结束CPA,获取CPA通道
  866. if active.ActivedCab == "1" {
  867. utils.LoggerDebug.Printf("%s check task creating , hangup CPA %s ", task, args[0])
  868. Hangup("1481")
  869. goto DELAY
  870. } else if active.ActivedCab == "8" {
  871. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  872. Hangup("1411")
  873. goto DELAY
  874. }
  875. }
  876. }
  877. return
  878. case "PA":
  879. if task == "CPA" /*|| task == "C2C"*/ {
  880. //获取正在创建的任务的优先级
  881. priorityPA := priority.GetPriorityByKey("PA")
  882. //获取将要创建的任务的优先级
  883. priorityTask := priority.GetPriorityByKey(task)
  884. //比较优先级,确定是否终止正在创建的任务
  885. if priorityPA < priorityTask {
  886. utils.LoggerDebug.Printf("%s check task PA creating , hangup CPA %s ", task, args[0])
  887. //结束task(CPA)
  888. if len(args) > 0 {
  889. Hangup(args[0])
  890. goto DELAY
  891. }
  892. } else {
  893. //结束PA,获取PA通道
  894. if active.ActivedCab == "1" {
  895. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2311")
  896. Hangup("2311")
  897. goto DELAY
  898. } else if active.ActivedCab == "8" {
  899. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2381")
  900. Hangup("2381")
  901. goto DELAY
  902. }
  903. }
  904. }
  905. return
  906. default:
  907. //utils.LoggerDebug.Printf("%s waiting trd=============previous task:%s creating ..... ", task, priority.TaskCreating)
  908. for i := 0; i < 4; i++ {
  909. if priority.TaskCreating != "" {
  910. utils.LoggerDebug.Printf("%s waiting previous task:%s creating ..... ", task, priority.TaskCreating)
  911. time.Sleep(time.Millisecond * 500)
  912. } else {
  913. utils.LoggerDebug.Printf("TaskCreating is nill, Set TaskCreating=%s", task)
  914. priority.TaskCreating = task
  915. return
  916. }
  917. }
  918. priority.TaskCreating = task
  919. utils.LoggerDebug.Printf("%s waiting previous task:%s creating timeout ! Set TaskCreating=%s", task, priority.TaskCreating, task)
  920. return
  921. }
  922. DELAY:
  923. time.Sleep(100 * time.Millisecond)
  924. }