call.go 21 KB

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