call.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. var task priority.TaskInfo
  119. var taskName string
  120. var ok bool
  121. lfshook.NewLogger().Infof("InterruptRunningTask toRuntask:%s ", toRunTask)
  122. if toRunTask != "PA" && toRunTask != "PAD-ICP" && toRunTask != "PAD-TMS" { // ignore C2C
  123. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask1()
  124. if !ok {
  125. return ""
  126. }
  127. } else { // have to check C2C
  128. taskName, task, ok = priority.RegistryTask.HighestPriorityRunningTask()
  129. if !ok {
  130. return ""
  131. }
  132. }
  133. lfshook.NewLogger().Infof("InterruptRunningTask RunningTask:%+v ", task)
  134. //same type return
  135. if toRunTask == taskName {
  136. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" || toRunTask == "PAD-OCC" {
  137. //Auto Answer PAD-OCC one by one, clean the old confbridge and channels before answer a new PAD .
  138. if toRunTask == "PAD-OCC" {
  139. Hangup(task.RunChannel) //pad
  140. ConfbridgeKick(task.ConfbridgeID, "all")
  141. HangupIO() //io
  142. //lfshook.NewLogger().Infof("===InterruptRunningTask=ret==== ")
  143. }
  144. return taskName
  145. }
  146. }
  147. //pad all reset
  148. if toRunTask == "AlarmHoldResetAll" {
  149. HangupAllLocalChan()
  150. return taskName
  151. }
  152. switch task.RunType {
  153. case "SPC":
  154. if toRunTask == "C2C" {
  155. HangupICP()
  156. //alstatus.PaStatus("", "SPC", "end")
  157. } else {
  158. ConfbridgeKick(task.ConfbridgeID, "all")
  159. alstatus.PaStatus("", "SPC", "end")
  160. }
  161. time.Sleep(time.Millisecond * 200)
  162. case "CHK":
  163. if toRunTask == "C2C" {
  164. HangupICP()
  165. //alstatus.PaStatus("", "CHK", "end")
  166. } else {
  167. ConfbridgeKick(task.ConfbridgeID, "all")
  168. alstatus.PaStatus("", "CHK", "end")
  169. }
  170. time.Sleep(time.Millisecond * 200)
  171. case "DCS":
  172. if toRunTask == "STN" {
  173. return taskName
  174. } else if toRunTask == "C2C" {
  175. HangupICP()
  176. //alstatus.PaStatus("", "DCS", "end")
  177. } else {
  178. ConfbridgeKick(task.ConfbridgeID, "all")
  179. alstatus.PaStatus("", "DCS", "end")
  180. }
  181. time.Sleep(time.Millisecond * 200)
  182. case "STN":
  183. if toRunTask == "DCS" {
  184. return taskName
  185. } else if toRunTask == "C2C" {
  186. HangupICP()
  187. //alstatus.PaStatus("", "STN", "end")
  188. } else {
  189. ConfbridgeKick(task.ConfbridgeID, "all")
  190. alstatus.PaStatus("", "STN", "end")
  191. }
  192. time.Sleep(time.Millisecond * 200)
  193. case "CPA":
  194. //kick CPA members
  195. if toRunTask != "C2C" {
  196. CPAConfbridgeKick(task.ConfbridgeID)
  197. alstatus.PaStatus("", "CPA", "end")
  198. } else if toRunTask == "C2C" {
  199. HangupICP()
  200. //alstatus.PaStatus("", "CPA", "end")
  201. }
  202. time.Sleep(time.Millisecond * 200)
  203. case "EMG":
  204. //kick EMG members
  205. if toRunTask != "C2C" {
  206. EMGConfbridgeKick(task.ConfbridgeID)
  207. alstatus.PaStatus("", "EMG", "end")
  208. } else if toRunTask == "C2C" {
  209. HangupICP()
  210. //alstatus.PaStatus("", "EMG", "end")
  211. }
  212. time.Sleep(time.Millisecond * 200)
  213. case "C2C": // Interrupt C2C task running,
  214. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  215. HangupICP()
  216. return taskName
  217. }
  218. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  219. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  220. break
  221. }
  222. priority.InterruptedPad = "PAD-ICP"
  223. lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
  224. chans, err := CoreShowChannels()
  225. if err != nil {
  226. lfshook.NewLogger().Infof("InterruptRunningTask CoreShowChannels err:%+v", err)
  227. return taskName
  228. }
  229. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  230. for _, ret := range chans {
  231. // Redirect the connected PAD to 0300
  232. if utils.IsPAIU(ret.CallerIDNum) {
  233. lfshook.NewLogger().Infof("====interrupt PAD ==== %+v ", ret)
  234. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  235. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  236. if err != nil {
  237. lfshook.NewLogger().Infof("InterruptRunningTask Redirect err:%+v", err)
  238. return taskName
  239. }
  240. lfshook.NewLogger().Infof("====interrupt PAD =1111=== %+v ", ret)
  241. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  242. active.NotifyPaiu(number, "hold")
  243. //HangupAllLocalChan()
  244. }
  245. }
  246. }
  247. for _, ret := range chans {
  248. //hangup pad call ICP channel
  249. if utils.IsPAIU(ret.CallerIDNum) {
  250. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  251. lfshook.NewLogger().Infof("Hangup PAD Channel :%+v", ret.Channel)
  252. Hangup(ret.Channel)
  253. }
  254. }
  255. }
  256. priority.RegistryTask.StopAndUnregister("PAD-ICP")
  257. priority.RegistryTask.StopAndUnregister("PAD-TMS")
  258. //2. hangup task channel (ICP + PACU)
  259. //lfshook.NewLogger().Infof("===InterruptRunningTask=2. hangup task channel === ")
  260. HangupAllLocalChan()
  261. HangupICP()
  262. //pad end
  263. if priority.PADStart == 1 {
  264. alstatus.PaStatus("", "PAD", "end")
  265. priority.PADStart = 0
  266. }
  267. case "PAD-OCC": // Interrupt PAD-OCC task running,
  268. if toRunTask == "C2C" {
  269. HangupICP()
  270. break
  271. } else {
  272. priority.InterruptedPad = "PAD-OCC"
  273. priority.OCCAnswer = 0
  274. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  275. if err != nil {
  276. lfshook.NewLogger().Infof("QueueStatus err:%+v", err)
  277. return taskName
  278. }
  279. if resCaller == nil {
  280. return taskName
  281. }
  282. if resCaller.Entrys != nil {
  283. for _, caller := range resCaller.Entrys {
  284. //lfshook.NewLogger().Infof("===QueueStatus=entry=%+v", caller)
  285. time.Sleep(time.Millisecond * 50)
  286. RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  287. }
  288. }
  289. priority.RegistryTask.StopAndUnregister("PAD-OCC")
  290. //2. Hangup connected PAD
  291. //lfshook.NewLogger().Infof("===InterruptRunningTask=Hangup connected PAD=== ")
  292. Hangup(task.RunChannel)
  293. //3. Hangup OI & ICP
  294. HangupIO()
  295. HangupAllLocalChan()
  296. ConfbridgeKick(task.ConfbridgeID, "all")
  297. //occ pad end
  298. if priority.PADOccStart == 1 {
  299. alstatus.OccPad("end")
  300. priority.PADOccStart = 0
  301. }
  302. }
  303. default:
  304. lfshook.NewLogger().Infof("InterruptRunningTask goto default !")
  305. //if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  306. // Hangup(priority.RunningPATaskChan)
  307. //}
  308. if toRunTask != "C2C" && task.RunType != "PA" {
  309. Hangup(task.RunChannel)
  310. ConfbridgeKick(task.ConfbridgeID, "all")
  311. }
  312. }
  313. return taskName
  314. }
  315. // Hangup all ICP
  316. func HangupICP() {
  317. Hangup("2311") //ICP1
  318. Hangup("2381") //ICP8
  319. }
  320. // Hangup all IO
  321. func HangupIO() {
  322. Hangup("1411") //IO1
  323. Hangup("1481") //IO8
  324. }
  325. // Hangup all PACU
  326. func HangupAllPACU() {
  327. //all PACU
  328. for _, ret := range Pacus {
  329. Hangup(ret)
  330. }
  331. }
  332. func HangupAllLocalChan() {
  333. chans, err := CoreShowChannels()
  334. if err != nil {
  335. lfshook.NewLogger().Infof("CoreShowChannels %+v", err)
  336. return
  337. }
  338. for _, ret := range chans {
  339. if strings.Contains(ret.Channel, "Local") && !strings.Contains(ret.Channel, "0502@default") {
  340. //lfshook.NewLogger().Infof("HangupAllLocalChan=====hangup========= %+v", ret)
  341. Hangup(ret.Channel)
  342. }
  343. }
  344. }
  345. // Hangup all PACU
  346. func HangupAllPAD() {
  347. //all PAD
  348. for _, ret := range Pads {
  349. Hangup(ret)
  350. }
  351. }
  352. // Hangup all calls
  353. func HangupAll() {
  354. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  355. }
  356. // Dial 拨打号码
  357. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  358. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  359. action := map[string]string{
  360. "Action": "Originate",
  361. "Channel": chanel,
  362. "Exten": dst,
  363. "Context": dialrule,
  364. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  365. "Priority": "1",
  366. "Variable": fmt.Sprintf("CAB=%s", callType),
  367. "async": "true",
  368. }
  369. lfshook.NewLogger().Infof("================dial action %+v", action)
  370. res, _, err := AminInstance.Send(action)
  371. if err != nil {
  372. lfshook.NewLogger().Errorf("%+v", err)
  373. }
  374. lfshook.NewLogger().Info(res)
  375. }
  376. // Dial 拨打号码
  377. func DialICP(src, dst, dialrule, callerID, callerName string) {
  378. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  379. action := map[string]string{
  380. "Action": "Originate",
  381. "Channel": chanel,
  382. "Exten": dst,
  383. "Context": dialrule,
  384. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  385. "Priority": "1",
  386. "Variable": fmt.Sprintf("CBID=%s", callerID),
  387. "async": "true",
  388. }
  389. lfshook.NewLogger().Infof("dial action %+v", action)
  390. res, _, err := AminInstance.Send(action)
  391. if err != nil {
  392. lfshook.NewLogger().Errorf("%+v", err)
  393. }
  394. lfshook.NewLogger().Info(res)
  395. }
  396. // 获取分机状态
  397. func ExtenStatus(exten string) (Status string) {
  398. action := map[string]string{
  399. "Action": "ExtensionState",
  400. "Exten": exten,
  401. "Context": "default",
  402. }
  403. res, _, err := AminInstance.Send(action)
  404. if err != nil {
  405. lfshook.NewLogger().Errorf("%+v", err)
  406. return ""
  407. }
  408. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  409. return res["StatusText"]
  410. }
  411. // PACU ChanSpy
  412. func ChanSpy(src, dst string, whisper, bargein bool) {
  413. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  414. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  415. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  416. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  417. /*
  418. if whisper {
  419. data = fmt.Sprintf("%s,w", data)
  420. }
  421. if bargein {
  422. data = fmt.Sprintf("%s,B", data)
  423. }
  424. */
  425. action := map[string]string{
  426. "Action": "Originate",
  427. "Channel": channel, // 不存在的通话
  428. "Application": "ChanSpy",
  429. "Data": data, // 存在的通话
  430. "CallerID": dst,
  431. "Async": "true",
  432. }
  433. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  434. _, _, err := AminInstance.Send(action)
  435. if err != nil {
  436. lfshook.NewLogger().Errorf("%+v", err)
  437. }
  438. }
  439. // Redirect 转接
  440. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  441. //callerID := "redirect"
  442. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  443. if !utils.IsChannel(channel) {
  444. //callerID = channel
  445. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  446. return err
  447. }
  448. }
  449. action := map[string]string{
  450. "Action": "Redirect",
  451. "Channel": channel,
  452. "Exten": dst,
  453. "Context": dialrule,
  454. "CallerID": callerID,
  455. "Priority": "1",
  456. "async": "true",
  457. }
  458. lfshook.NewLogger().Infof("RedirectInQueue: %+v", action)
  459. res, _, err := AminInstance.Send(action)
  460. if err != nil {
  461. lfshook.NewLogger().Error(err)
  462. }
  463. lfshook.NewLogger().Info(res)
  464. return err
  465. }
  466. // Redirect 转接
  467. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  468. //callerID := "redirect"
  469. //lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  470. if !utils.IsChannel(channel) {
  471. callerID = channel
  472. if channel, err = GetChannelByExten(channel); err != nil {
  473. return err
  474. }
  475. }
  476. action := map[string]string{
  477. "Action": "Redirect",
  478. "Channel": channel,
  479. "Exten": dst,
  480. "Context": dialrule,
  481. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  482. "Priority": "1",
  483. "async": "true",
  484. }
  485. lfshook.NewLogger().Infof("Redirect: %+v", action)
  486. res, _, err := AminInstance.Send(action)
  487. if err != nil {
  488. lfshook.NewLogger().Error(err)
  489. }
  490. lfshook.NewLogger().Info(res)
  491. return err
  492. }
  493. func SetPadTimer() {
  494. toRunPadpriority := priority.GetPriorityByKey(priority.InterruptedPad) //Get PAD priori 获取之前打断的报警优先级
  495. //toRunpriority := priority.GetPriorityByKey("PAD-ICP")
  496. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  497. if ok {
  498. lfshook.NewLogger().Infof("PAD SetPadTimer runing priority:%d toRun priority:%d", taskTmp.Priority, toRunPadpriority)
  499. if taskTmp.Priority < toRunPadpriority { //higher priority task running ,do not set timer
  500. return
  501. }
  502. }
  503. res, err := QueueStatus("0300", "") // check OCC queue , if empty OCC-PAD start
  504. if err != nil {
  505. lfshook.NewLogger().Infof("QueueStatus err%+v", err)
  506. return
  507. }
  508. if res == nil {
  509. return
  510. }
  511. if res.Calls != "0" {
  512. lfshook.NewLogger().Infof("PAD SetPadTimer Set QueueTimer timeout 30s !")
  513. //active.SetTimer = true
  514. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  515. if active.QueueTimer != nil {
  516. if active.QueueTimer.Stop() {
  517. lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  518. } else {
  519. lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  520. }
  521. }
  522. lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  523. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  524. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  525. //if both not active , return
  526. if active.ActivedCab == "" {
  527. return
  528. }
  529. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  530. if err != nil {
  531. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  532. return
  533. }
  534. if res == nil {
  535. return
  536. }
  537. if res.Calls == "0" { // OCC queue empty
  538. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  539. if err != nil {
  540. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  541. return
  542. }
  543. if resCaller.Entrys != nil {
  544. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  545. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  546. })
  547. for _, caller := range resCaller.Entrys {
  548. priority.ICPAnswer = 0
  549. //lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=")
  550. lfshook.NewLogger().Infof("Q300==SetPadTimer==Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  551. //order by pos
  552. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  553. time.Sleep(time.Millisecond * 100) //200 ms delay
  554. }
  555. }
  556. //==============test info =====================
  557. /*
  558. time.Sleep(2 * time.Second)
  559. occque, err1 := QueueStatus("0301", "") // check ICP queue, get entries
  560. if err1 != nil {
  561. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  562. return
  563. }
  564. for _, caller := range occque.Entrys {
  565. lfshook.NewLogger().Infof("Q301==SetPadTimer==Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  566. }*/
  567. }
  568. })
  569. }
  570. }
  571. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  572. action := map[string]string{
  573. "Action": "ConfbridgeKick",
  574. "Conference": confnum,
  575. "Channel": channel,
  576. }
  577. res, _, err = AminInstance.Send(action)
  578. if err != nil {
  579. return nil, err
  580. }
  581. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  582. if res["Response"] != "Success" {
  583. return nil, errors.New(res["Message"])
  584. }
  585. return res, nil
  586. }
  587. func CPAConfbridgeKick(confnum string) (res map[string]string, err error) {
  588. chans, err := ConfbridgeList(confnum)
  589. if err != nil {
  590. return nil, errors.New(res["Message"])
  591. }
  592. for _, confChan := range chans {
  593. if !strings.Contains(confChan, "PJSIP/1481") {
  594. ConfbridgeKick(confnum, confChan)
  595. }
  596. }
  597. return res, nil
  598. }
  599. func CPAConfbridgeReinvite(confID string) {
  600. time.Sleep(time.Millisecond * 500)
  601. for _, ext := range Speakers {
  602. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  603. }
  604. }
  605. func EMGConfbridgeKick(confnum string) (res map[string]string, err error) {
  606. chans, err := ConfbridgeList(confnum)
  607. if err != nil {
  608. return nil, errors.New(res["Message"])
  609. }
  610. for _, confChan := range chans {
  611. if !strings.Contains(confChan, "0502@default") {
  612. ConfbridgeKick(confnum, confChan)
  613. }
  614. }
  615. return res, nil
  616. }
  617. func EMGConfbridgeReinvite(confID string) {
  618. time.Sleep(time.Millisecond * 500)
  619. for _, ext := range Speakers {
  620. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  621. }
  622. }
  623. func ConfbridgeList(confnum string) (chans []string, err error) {
  624. action := map[string]string{
  625. "Action": "ConfbridgeList",
  626. "Conference": confnum,
  627. }
  628. res, events, err := AminInstance.Send(action)
  629. if err != nil {
  630. return nil, err
  631. }
  632. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  633. if res["Response"] == "Success" {
  634. for _, event := range events {
  635. if event.Data["Event"] == "ConfbridgeList" {
  636. chans = append(chans, event.Data["Channel"])
  637. }
  638. }
  639. return chans, nil
  640. } else {
  641. return nil, errors.New(res["Message"])
  642. }
  643. }
  644. func ConfbridgeReinvite(src, context, confID string) {
  645. if ExtenStatus(src) != "Idle" {
  646. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  647. return
  648. }
  649. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  650. action := map[string]string{
  651. "Action": "Originate",
  652. "Channel": chanel,
  653. "Exten": "000",
  654. "Context": "confbridge-join",
  655. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  656. "Priority": "1",
  657. "Variable": fmt.Sprintf("CBID=%s", confID),
  658. "async": "true",
  659. }
  660. lfshook.NewLogger().Infof("dial action %+v", action)
  661. res, _, err := AminInstance.Send(action)
  662. if err != nil {
  663. lfshook.NewLogger().Errorf("%+v", err)
  664. }
  665. lfshook.NewLogger().Info(res)
  666. }
  667. func ICPConfbridgeReinvite(confID, paType string) {
  668. switch paType {
  669. case "PAD-OCC":
  670. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  671. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  672. case "CPA":
  673. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  674. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  675. case "EMG":
  676. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  677. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  678. case "SPC":
  679. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  680. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  681. case "STN":
  682. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  683. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  684. case "DCS":
  685. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  686. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  687. case "CHK":
  688. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  689. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  690. case "VOL":
  691. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  692. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  693. }
  694. }