call.go 25 KB

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