call.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. task.Running = false
  192. //alstatus.PaStatus("", "CPA", "end")
  193. } else if toRunTask == "C2C" {
  194. HangupICP()
  195. }
  196. time.Sleep(time.Millisecond * 200)
  197. case "EMG":
  198. //kick EMG members
  199. if toRunTask == "EMG" {
  200. ConfbridgeKick(task.ConfbridgeID, "all")
  201. } else if toRunTask != "C2C" {
  202. EMGConfbridgeKick(task)
  203. task.Running = false
  204. //alstatus.PaStatus("", "EMG", "end")
  205. } else if toRunTask == "C2C" {
  206. HangupICP()
  207. }
  208. time.Sleep(time.Millisecond * 200)
  209. case "C2C": // Interrupt C2C task running,
  210. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  211. HangupICP()
  212. return taskName
  213. }
  214. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  215. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  216. break
  217. }
  218. priority.InterruptedPad = "PAD-ICP"
  219. //lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
  220. chans, err := CoreShowChannels()
  221. if err != nil {
  222. lfshook.NewLogger().Infof("InterruptRunningTask CoreShowChannels err:%+v", err)
  223. return taskName
  224. }
  225. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  226. for _, ret := range chans {
  227. // Redirect the connected PAD to 0300
  228. if utils.IsPAIU(ret.CallerIDNum) {
  229. //lfshook.NewLogger().Infof("====interrupt PAD ==== %+v ", ret)
  230. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  231. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  232. if err != nil {
  233. lfshook.NewLogger().Infof("InterruptRunningTask Redirect err:%+v", err)
  234. return taskName
  235. }
  236. //lfshook.NewLogger().Infof("====interrupt PAD =1111=== %+v ", ret)
  237. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  238. active.NotifyPaiu(number, "hold")
  239. //HangupAllLocalChan()
  240. }
  241. }
  242. }
  243. for _, ret := range chans {
  244. //hangup pad call ICP channel
  245. if utils.IsPAIU(ret.CallerIDNum) {
  246. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  247. //lfshook.NewLogger().Infof("Hangup PAD Channel :%+v", ret.Channel)
  248. Hangup(ret.Channel)
  249. }
  250. }
  251. }
  252. alstatus.PaStatus("", "PAD", "end")
  253. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  254. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  255. //2. hangup task channel (ICP + PACU)
  256. //lfshook.NewLogger().Infof("===InterruptRunningTask=2. hangup task channel === ")
  257. HangupAllLocalChan()
  258. HangupICP()
  259. case "PAD-OCC": // Interrupt PAD-OCC task running,
  260. if toRunTask == "C2C" {
  261. HangupICP()
  262. break
  263. } else {
  264. priority.InterruptedPad = "PAD-OCC"
  265. priority.OCCAnswer = 0
  266. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  267. if err != nil {
  268. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  269. return taskName
  270. }
  271. if resCaller == nil {
  272. return taskName
  273. }
  274. if resCaller.Entrys != nil {
  275. for _, caller := range resCaller.Entrys {
  276. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  277. time.Sleep(time.Millisecond * 50)
  278. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  279. }
  280. }
  281. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  282. //2. Hangup connected PAD
  283. //lfshook.NewLogger().Infof("===InterruptRunningTask=Hangup connected PAD=== ")
  284. Hangup(task.RunChannel)
  285. //3. interrupt OCC-PAD Hangup OI
  286. if active.ActivedCab == "1" {
  287. Hangup("1411")
  288. } else if active.ActivedCab == "8" {
  289. Hangup("1481")
  290. } else {
  291. if active.ActivedCabDelay == "1" {
  292. Hangup("1411")
  293. } else if active.ActivedCabDelay == "8" {
  294. Hangup("1481")
  295. } else {
  296. Hangup("1411")
  297. }
  298. }
  299. HangupAllLocalChan()
  300. ConfbridgeKick(task.ConfbridgeID, "all")
  301. //occ pad end
  302. if priority.PADOccStart == 1 {
  303. alstatus.OccPad("end")
  304. alstatus.PaStatus("", "PAD", "end")
  305. priority.PADOccStart = 0
  306. priority.OCCAnswer = 0
  307. }
  308. }
  309. default:
  310. lfshook.NewLogger().Infof("InterruptRunningTask goto default !")
  311. //if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  312. // Hangup(priority.RunningPATaskChan)
  313. //}
  314. if toRunTask != "C2C" && task.RunType != "PA" {
  315. Hangup(task.RunChannel)
  316. ConfbridgeKick(task.ConfbridgeID, "all")
  317. }
  318. }
  319. return taskName
  320. }
  321. // Hangup all ICP
  322. func HangupICP() {
  323. Hangup("2311") //ICP1
  324. Hangup("2381") //ICP8
  325. }
  326. // Hangup all IO
  327. func HangupIO() {
  328. Hangup("1411") //IO1
  329. Hangup("1481") //IO8
  330. }
  331. // Hangup all PACU
  332. func HangupAllPACU() {
  333. //all PACU
  334. for _, ret := range Pacus {
  335. Hangup(ret)
  336. }
  337. }
  338. func HangupAllLocalChan() {
  339. chans, err := CoreShowChannels()
  340. if err != nil {
  341. lfshook.NewLogger().Infof("CoreShowChannels %+v", err)
  342. return
  343. }
  344. for _, ret := range chans {
  345. if strings.Contains(ret.Channel, "Local") && !strings.Contains(ret.Channel, "0502@default") {
  346. //lfshook.NewLogger().Infof("HangupAllLocalChan=====hangup========= %+v", ret)
  347. Hangup(ret.Channel)
  348. }
  349. }
  350. }
  351. // Hangup all PACU
  352. func HangupAllPAD() {
  353. //all PAD
  354. for _, ret := range Pads {
  355. Hangup(ret)
  356. }
  357. }
  358. // Hangup all calls
  359. func HangupAll() {
  360. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  361. }
  362. // Dial 拨打号码
  363. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  364. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  365. action := map[string]string{
  366. "Action": "Originate",
  367. "Channel": chanel,
  368. "Exten": dst,
  369. "Context": dialrule,
  370. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  371. "Priority": "1",
  372. "Variable": fmt.Sprintf("CAB=%s", callType),
  373. "async": "true",
  374. }
  375. //lfshook.NewLogger().Infof("================dial action %+v", action)
  376. res, _, err := AminInstance.Send(action)
  377. if err != nil {
  378. lfshook.NewLogger().Errorf("%+v", err)
  379. }
  380. lfshook.NewLogger().Info(res)
  381. }
  382. // Dial 拨打号码
  383. func DialICP(src, dst, dialrule, callerID, callerName string) {
  384. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  385. action := map[string]string{
  386. "Action": "Originate",
  387. "Channel": chanel,
  388. "Exten": dst,
  389. "Context": dialrule,
  390. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  391. "Priority": "1",
  392. "Variable": fmt.Sprintf("CBID=%s", callerID),
  393. "async": "true",
  394. }
  395. lfshook.NewLogger().Infof("dial action %+v", action)
  396. res, _, err := AminInstance.Send(action)
  397. if err != nil {
  398. lfshook.NewLogger().Errorf("%+v", err)
  399. }
  400. lfshook.NewLogger().Info(res)
  401. }
  402. // 获取分机状态
  403. func ExtenStatus(exten string) (Status string) {
  404. action := map[string]string{
  405. "Action": "ExtensionState",
  406. "Exten": exten,
  407. "Context": "default",
  408. }
  409. res, _, err := AminInstance.Send(action)
  410. if err != nil {
  411. lfshook.NewLogger().Errorf("%+v", err)
  412. return ""
  413. }
  414. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  415. return res["StatusText"]
  416. }
  417. // PACU ChanSpy
  418. func ChanSpy(src, dst string, whisper, bargein bool) {
  419. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  420. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  421. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  422. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  423. /*
  424. if whisper {
  425. data = fmt.Sprintf("%s,w", data)
  426. }
  427. if bargein {
  428. data = fmt.Sprintf("%s,B", data)
  429. }
  430. */
  431. action := map[string]string{
  432. "Action": "Originate",
  433. "Channel": channel, // 不存在的通话
  434. "Application": "ChanSpy",
  435. "Data": data, // 存在的通话
  436. "CallerID": dst,
  437. "Async": "true",
  438. }
  439. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  440. _, _, err := AminInstance.Send(action)
  441. if err != nil {
  442. lfshook.NewLogger().Errorf("%+v", err)
  443. }
  444. }
  445. // Redirect 转接
  446. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  447. //callerID := "redirect"
  448. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  449. if !utils.IsChannel(channel) {
  450. //callerID = channel
  451. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  452. return err
  453. }
  454. }
  455. action := map[string]string{
  456. "Action": "Redirect",
  457. "Channel": channel,
  458. "Exten": dst,
  459. "Context": dialrule,
  460. "CallerID": callerID,
  461. "Priority": "1",
  462. "async": "true",
  463. }
  464. lfshook.NewLogger().Infof("RedirectInQueue: %+v", action)
  465. res, _, err := AminInstance.Send(action)
  466. if err != nil {
  467. lfshook.NewLogger().Error(err)
  468. }
  469. lfshook.NewLogger().Info(res)
  470. return err
  471. }
  472. // Redirect 转接
  473. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  474. //callerID := "redirect"
  475. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  476. if !utils.IsChannel(channel) {
  477. callerID = channel
  478. if channel, err = GetChannelByExten(channel); err != nil {
  479. return err
  480. }
  481. }
  482. action := map[string]string{
  483. "Action": "Redirect",
  484. "Channel": channel,
  485. "Exten": dst,
  486. "Context": dialrule,
  487. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  488. "Priority": "1",
  489. "async": "true",
  490. }
  491. lfshook.NewLogger().Infof("Redirect: %+v", action)
  492. res, _, err := AminInstance.Send(action)
  493. if err != nil {
  494. lfshook.NewLogger().Error(err)
  495. }
  496. lfshook.NewLogger().Info(res)
  497. return err
  498. }
  499. func SetPadTimer() {
  500. //toRunPadpriority := priority.GetPriorityByKey(priority.InterruptedPad) //Get PAD priori 获取之前打断的报警优先级
  501. toRunPadpriority := priority.GetPriorityByKey("PAD-ICP")
  502. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  503. if ok {
  504. if taskTmp.Priority < toRunPadpriority { //higher priority task running ,do not set timer
  505. utils.LoggerDebug.Printf("PAD SetPadTimer runing priority:%d toRun priority:%d , return !", taskTmp.Priority, toRunPadpriority)
  506. return
  507. }
  508. }
  509. utils.LoggerDebug.Printf("PAD SetPadTimer check 0300")
  510. res, err := QueueStatus("0300", "")
  511. if err != nil {
  512. lfshook.NewLogger().Infof("QueueStatus err%+v", err)
  513. return
  514. }
  515. if res == nil {
  516. return
  517. }
  518. if res.Calls != "0" {
  519. utils.LoggerDebug.Printf("PAD SetPadTimer Set QueueTimer timeout %ds !", active.PADTimeout)
  520. //active.SetTimer = true
  521. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  522. if active.QueueTimer != nil {
  523. if active.QueueTimer.Stop() {
  524. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  525. } else {
  526. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  527. }
  528. }
  529. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  530. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  531. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  532. //if both not active , return
  533. if active.ActivedCab == "" {
  534. return
  535. }
  536. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  537. if err != nil {
  538. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  539. return
  540. }
  541. if res == nil {
  542. return
  543. }
  544. if res.Calls == "0" { // OCC queue empty
  545. /*resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  546. if err != nil {
  547. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  548. return
  549. }
  550. if resCaller.Entrys != nil {
  551. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  552. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  553. })
  554. for _, caller := range resCaller.Entrys {
  555. priority.ICPAnswer = 0
  556. //order by pos
  557. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  558. time.Sleep(time.Millisecond * 100) //200 ms delay
  559. }
  560. }*/
  561. for _, ret := range alstatus.PadQueues {
  562. //utils.LoggerDebug.Printf("PAD to OCC exten:%s", ret.Exten)
  563. priority.ICPAnswer = 0
  564. //order by pos
  565. RedirectInQueue(ret.Exten, "0301", "queues-occ", ret.Exten) // redirect All ICP-PAD redirect to OCC queue
  566. time.Sleep(time.Millisecond * 100)
  567. }
  568. }
  569. })
  570. }
  571. }
  572. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  573. action := map[string]string{
  574. "Action": "ConfbridgeKick",
  575. "Conference": confnum,
  576. "Channel": channel,
  577. }
  578. res, _, err = AminInstance.Send(action)
  579. if err != nil {
  580. return nil, err
  581. }
  582. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  583. if res["Response"] != "Success" {
  584. return nil, errors.New(res["Message"])
  585. }
  586. return res, nil
  587. }
  588. func CPAConfbridgeKick(confInfo priority.TaskInfo) (res map[string]string, err error) {
  589. utils.LoggerDebug.Printf("CPA CPAConfbridgeKick , kick all members .")
  590. chans, err := ConfbridgeList(confInfo.ConfbridgeID)
  591. if err != nil {
  592. return nil, errors.New(res["Message"])
  593. }
  594. for _, confChan := range chans {
  595. if active.TrainDevide == 1 {
  596. if active.CabNum == "1" { //在1车车厢内
  597. if !strings.Contains(confChan, "PJSIP/1411") {
  598. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  599. }
  600. } else { //在8车车厢内
  601. if !strings.Contains(confChan, "PJSIP/1481") {
  602. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  603. }
  604. }
  605. } else {
  606. if active.ActivedCab != "1" {
  607. if !strings.Contains(confChan, "PJSIP/1411") {
  608. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  609. }
  610. } else {
  611. if !strings.Contains(confChan, "PJSIP/1481") {
  612. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  613. }
  614. }
  615. }
  616. }
  617. if confInfo.Running {
  618. alstatus.PaStatus("", "CPA", "end")
  619. }
  620. return res, nil
  621. }
  622. func CPAConfbridgeReinvite(confID string) bool {
  623. time.Sleep(time.Millisecond * 100)
  624. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , resume CPA .")
  625. chans, err := ConfbridgeList(confID)
  626. if len(chans) > 1 || err != nil {
  627. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , return . ConfbridgeList chans > 1 or err !")
  628. return false
  629. }
  630. if priority.PAInterrupt == 1 { // PA 打断标签判断
  631. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , PA ready to go , return !")
  632. return false
  633. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  634. priority.TaskCreating = "CPA"
  635. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  636. if utils.IsICP(ext) {
  637. if priority.CABInterrupt == 1 {
  638. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  639. continue
  640. }
  641. }
  642. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  643. }
  644. time.Sleep(time.Millisecond * 200) //等待通道建立
  645. priority.TaskCreating = ""
  646. alstatus.PaStatus("", "CPA", "start")
  647. } else if priority.TaskCreating != "" {
  648. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , other PA ready to go , return !")
  649. return false
  650. } else {
  651. priority.TaskCreating = "CPA"
  652. for _, ext := range Speakers {
  653. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  654. }
  655. time.Sleep(time.Millisecond * 200) //等待通道建立
  656. priority.TaskCreating = ""
  657. alstatus.PaStatus("", "CPA", "start")
  658. }
  659. return true
  660. }
  661. func EMGConfbridgeKick(confInfo priority.TaskInfo) (res map[string]string, err error) {
  662. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , kick all members .")
  663. chans, err := ConfbridgeList(confInfo.ConfbridgeID)
  664. if err != nil {
  665. return nil, errors.New(res["Message"])
  666. }
  667. for _, confChan := range chans {
  668. if !strings.Contains(confChan, "0502@default") {
  669. ConfbridgeKick(confInfo.ConfbridgeID, confChan)
  670. }
  671. }
  672. if confInfo.Running {
  673. alstatus.PaStatus("", "EMG", "end")
  674. }
  675. return res, nil
  676. }
  677. func EMGConfbridgeReinvite(confID string) {
  678. //if priority.CABInterrupt != 1 {
  679. // time.Sleep(time.Millisecond * 100)
  680. //} else {
  681. time.Sleep(time.Second * 1) //wait cpa start first
  682. //}
  683. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , resume EMG .")
  684. if priority.PAInterrupt == 1 { // PA 打断标签判断
  685. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , PA ready to go , return !")
  686. return
  687. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  688. priority.TaskCreating = "EMG"
  689. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  690. if utils.IsICP(ext) {
  691. if priority.CABInterrupt == 1 {
  692. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get CABCAB Interrupt resume EMG , ignore %s continue .", ext)
  693. continue
  694. }
  695. }
  696. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  697. }
  698. time.Sleep(time.Millisecond * 200) //等待通道建立
  699. priority.TaskCreating = ""
  700. alstatus.PaStatus("", "EMG", "start")
  701. } else if priority.TaskCreating != "" {
  702. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , other PA ready to go , return !")
  703. return
  704. } else {
  705. priority.TaskCreating = "EMG"
  706. for _, ext := range Speakers {
  707. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  708. }
  709. time.Sleep(time.Millisecond * 200) //等待通道建立
  710. priority.TaskCreating = ""
  711. alstatus.PaStatus("", "EMG", "start")
  712. }
  713. }
  714. func ConfbridgeList(confnum string) (chans []string, err error) {
  715. action := map[string]string{
  716. "Action": "ConfbridgeList",
  717. "Conference": confnum,
  718. }
  719. res, events, err := AminInstance.Send(action)
  720. if err != nil {
  721. return nil, err
  722. }
  723. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  724. if res["Response"] == "Success" {
  725. for _, event := range events {
  726. if event.Data["Event"] == "ConfbridgeList" {
  727. chans = append(chans, event.Data["Channel"])
  728. }
  729. }
  730. return chans, nil
  731. } else {
  732. return nil, errors.New(res["Message"])
  733. }
  734. }
  735. func ConfbridgeReinvite(src, context, confID string) {
  736. if ExtenStatus(src) != "Idle" {
  737. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  738. return
  739. }
  740. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  741. action := map[string]string{
  742. "Action": "Originate",
  743. "Channel": chanel,
  744. "Exten": "000",
  745. "Context": "confbridge-join",
  746. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  747. "Priority": "1",
  748. "Variable": fmt.Sprintf("CBID=%s", confID),
  749. "async": "true",
  750. }
  751. lfshook.NewLogger().Infof("dial action %+v", action)
  752. res, _, err := AminInstance.Send(action)
  753. if err != nil {
  754. lfshook.NewLogger().Errorf("%+v", err)
  755. }
  756. lfshook.NewLogger().Info(res)
  757. }
  758. func ICPConfbridgeReinvite(confID, paType string) {
  759. /*if priority.CABInterrupt == 1 || priority.CABInterrupt == 1 {
  760. utils.LoggerDebug.Printf("ICPConfbridgeReinvite, Get PA/CABCAB Interrupt resume %s faild .", paType)
  761. return
  762. }*/
  763. switch paType {
  764. case "PAD-OCC":
  765. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  766. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  767. case "CPA":
  768. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  769. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  770. case "EMG":
  771. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  772. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  773. case "SPC":
  774. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  775. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  776. case "STN":
  777. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  778. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  779. case "DCS":
  780. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  781. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  782. case "CHK":
  783. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  784. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  785. case "VOL":
  786. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  787. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  788. }
  789. }
  790. var waitMutex sync.Mutex
  791. // 设置全局变量控制任务创建过程,避免被其他任务打乱任务创建过程
  792. func WaitTaskCreate(task string, args ...string) { //arg1(task chan)
  793. utils.LoggerDebug.Printf("%s check task creating ..... ,TaskCreating = %s", task, priority.TaskCreating)
  794. waitMutex.Lock()
  795. defer waitMutex.Unlock()
  796. switch priority.TaskCreating {
  797. case "C2C":
  798. if /*task == "PA" ||*/ task == "CPA" {
  799. //获取正在创建的任务的优先级
  800. priorityC2C := priority.GetPriorityByKey("C2C")
  801. //获取将要创建的任务的优先级
  802. priorityTask := priority.GetPriorityByKey(task)
  803. //比较优先级,确定是否终止正在创建的任务
  804. if priorityC2C < priorityTask {
  805. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  806. //结束task(CPA)
  807. if len(args) > 0 {
  808. Hangup(args[0])
  809. goto DELAY
  810. }
  811. } else {
  812. //结束C2C,PA 则不需要挂断ICP(由ICP自行处理优先级)
  813. utils.LoggerDebug.Printf("%s check task C2C creating , hangup C2C ICPs ", task)
  814. HangupICP()
  815. goto DELAY
  816. }
  817. }
  818. return
  819. case "CPA":
  820. if task == "PA" /*|| task == "C2C"*/ {
  821. //获取正在创建的任务的优先级
  822. priorityCPA := priority.GetPriorityByKey("CPA")
  823. //获取将要创建的任务的优先级
  824. priorityTask := priority.GetPriorityByKey(task)
  825. //比较优先级,确定是否终止正在创建的任务
  826. if priorityCPA < priorityTask {
  827. utils.LoggerDebug.Printf("%s check task CPA creating , hangup %s %s", task, task, args[0])
  828. //结束task(PA,CPA)
  829. if len(args) > 0 {
  830. Hangup(args[0])
  831. goto DELAY
  832. }
  833. } else {
  834. //结束CPA,获取CPA通道
  835. if active.ActivedCab == "1" {
  836. utils.LoggerDebug.Printf("%s check task creating , hangup CPA %s ", task, args[0])
  837. Hangup("1481")
  838. goto DELAY
  839. } else if active.ActivedCab == "8" {
  840. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  841. Hangup("1411")
  842. goto DELAY
  843. }
  844. }
  845. }
  846. return
  847. case "PA":
  848. if task == "CPA" /*|| task == "C2C"*/ {
  849. //获取正在创建的任务的优先级
  850. priorityPA := priority.GetPriorityByKey("PA")
  851. //获取将要创建的任务的优先级
  852. priorityTask := priority.GetPriorityByKey(task)
  853. //比较优先级,确定是否终止正在创建的任务
  854. if priorityPA < priorityTask {
  855. utils.LoggerDebug.Printf("%s check task PA creating , hangup CPA %s ", task, args[0])
  856. //结束task(CPA)
  857. if len(args) > 0 {
  858. Hangup(args[0])
  859. goto DELAY
  860. }
  861. } else {
  862. //结束PA,获取PA通道
  863. if active.ActivedCab == "1" {
  864. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2311")
  865. Hangup("2311")
  866. goto DELAY
  867. } else if active.ActivedCab == "8" {
  868. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2381")
  869. Hangup("2381")
  870. goto DELAY
  871. }
  872. }
  873. }
  874. return
  875. default:
  876. //utils.LoggerDebug.Printf("%s waiting trd=============previous task:%s creating ..... ", task, priority.TaskCreating)
  877. for i := 0; i < 4; i++ {
  878. if priority.TaskCreating != "" {
  879. utils.LoggerDebug.Printf("%s waiting previous task:%s creating ..... ", task, priority.TaskCreating)
  880. time.Sleep(time.Millisecond * 500)
  881. } else {
  882. utils.LoggerDebug.Printf("TaskCreating is nill, Set TaskCreating=%s", task)
  883. priority.TaskCreating = task
  884. return
  885. }
  886. }
  887. priority.TaskCreating = task
  888. utils.LoggerDebug.Printf("%s waiting previous task:%s creating timeout ! Set TaskCreating=%s", task, priority.TaskCreating, task)
  889. return
  890. }
  891. DELAY:
  892. time.Sleep(100 * time.Millisecond)
  893. }