call.go 30 KB

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