call.go 20 KB

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