call.go 25 KB

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