call.go 25 KB

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