call.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. "strings"
  11. "sync"
  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. utils.LoggerDebug.Printf("InterruptRunningTask RunningTask:%+v", taskName)
  135. //lfshook.NewLogger().Infof("InterruptRunningTask RunningTask:%+v ", task)
  136. //same type return
  137. if toRunTask == taskName {
  138. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" || toRunTask == "PAD-OCC" {
  139. //Auto Answer PAD-OCC one by one, clean the old confbridge and channels before answer a new PAD .
  140. if toRunTask == "PAD-OCC" {
  141. Hangup(task.RunChannel) //pad
  142. ConfbridgeKick(task.ConfbridgeID, "all")
  143. HangupIO() //io
  144. //lfshook.NewLogger().Infof("===InterruptRunningTask=ret==== ")
  145. }
  146. return taskName
  147. }
  148. }
  149. //pad all reset
  150. if toRunTask == "AlarmHoldResetAll" {
  151. HangupAllLocalChan()
  152. return taskName
  153. }
  154. switch task.RunType {
  155. case "SPC":
  156. if toRunTask == "C2C" {
  157. HangupICP()
  158. //alstatus.PaStatus("", "SPC", "end")
  159. } else {
  160. ConfbridgeKick(task.ConfbridgeID, "all")
  161. alstatus.PaStatus("", "SPC", "end")
  162. }
  163. time.Sleep(time.Millisecond * 200)
  164. case "CHK":
  165. if toRunTask == "C2C" {
  166. HangupICP()
  167. //alstatus.PaStatus("", "CHK", "end")
  168. } else {
  169. ConfbridgeKick(task.ConfbridgeID, "all")
  170. alstatus.PaStatus("", "CHK", "end")
  171. }
  172. time.Sleep(time.Millisecond * 200)
  173. case "DCS":
  174. if toRunTask == "STN" {
  175. return taskName
  176. } else if toRunTask == "C2C" {
  177. HangupICP()
  178. //alstatus.PaStatus("", "DCS", "end")
  179. } else {
  180. ConfbridgeKick(task.ConfbridgeID, "all")
  181. alstatus.PaStatus("", "DCS", "end")
  182. }
  183. time.Sleep(time.Millisecond * 200)
  184. case "STN":
  185. if toRunTask == "DCS" {
  186. return taskName
  187. } else if toRunTask == "C2C" {
  188. HangupICP()
  189. //alstatus.PaStatus("", "STN", "end")
  190. } else {
  191. ConfbridgeKick(task.ConfbridgeID, "all")
  192. alstatus.PaStatus("", "STN", "end")
  193. }
  194. time.Sleep(time.Millisecond * 200)
  195. case "CPA":
  196. //kick CPA members
  197. if toRunTask != "C2C" {
  198. CPAConfbridgeKick(task.ConfbridgeID)
  199. alstatus.PaStatus("", "CPA", "end")
  200. } else if toRunTask == "C2C" {
  201. HangupICP()
  202. //alstatus.PaStatus("", "CPA", "end")
  203. }
  204. time.Sleep(time.Millisecond * 200)
  205. case "EMG":
  206. //kick EMG members
  207. if toRunTask == "EMG" {
  208. ConfbridgeKick(task.ConfbridgeID, "all")
  209. //alstatus.PaStatus("", "EMG", "end")
  210. } else if toRunTask != "C2C" {
  211. EMGConfbridgeKick(task.ConfbridgeID)
  212. //alstatus.PaStatus("", "EMG", "end")
  213. } else if toRunTask == "C2C" {
  214. HangupICP()
  215. //alstatus.PaStatus("", "EMG", "end")
  216. }
  217. time.Sleep(time.Millisecond * 200)
  218. case "C2C": // Interrupt C2C task running,
  219. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  220. HangupICP()
  221. return taskName
  222. }
  223. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  224. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  225. break
  226. }
  227. priority.InterruptedPad = "PAD-ICP"
  228. //lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
  229. chans, err := CoreShowChannels()
  230. if err != nil {
  231. lfshook.NewLogger().Infof("InterruptRunningTask CoreShowChannels err:%+v", err)
  232. return taskName
  233. }
  234. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  235. for _, ret := range chans {
  236. // Redirect the connected PAD to 0300
  237. if utils.IsPAIU(ret.CallerIDNum) {
  238. //lfshook.NewLogger().Infof("====interrupt PAD ==== %+v ", ret)
  239. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  240. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  241. if err != nil {
  242. lfshook.NewLogger().Infof("InterruptRunningTask Redirect err:%+v", err)
  243. return taskName
  244. }
  245. //lfshook.NewLogger().Infof("====interrupt PAD =1111=== %+v ", ret)
  246. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  247. active.NotifyPaiu(number, "hold")
  248. //HangupAllLocalChan()
  249. }
  250. }
  251. }
  252. for _, ret := range chans {
  253. //hangup pad call ICP channel
  254. if utils.IsPAIU(ret.CallerIDNum) {
  255. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  256. //lfshook.NewLogger().Infof("Hangup PAD Channel :%+v", ret.Channel)
  257. Hangup(ret.Channel)
  258. }
  259. }
  260. }
  261. alstatus.PaStatus("", "PAD", "end")
  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. 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. alstatus.PaStatus("", "PAD", "end")
  302. priority.PADOccStart = 0
  303. priority.OCCAnswer = 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. toRunPadpriority := priority.GetPriorityByKey("PAD-ICP")
  499. _, taskTmp, ok := priority.RegistryTask.HighestPriorityRunningTask()
  500. if ok {
  501. if taskTmp.Priority < toRunPadpriority { //higher priority task running ,do not set timer
  502. utils.LoggerDebug.Printf("PAD SetPadTimer runing priority:%d toRun priority:%d , return !", taskTmp.Priority, toRunPadpriority)
  503. return
  504. }
  505. }
  506. utils.LoggerDebug.Printf("PAD SetPadTimer check 0300")
  507. res, err := QueueStatus("0300", "")
  508. if err != nil {
  509. lfshook.NewLogger().Infof("QueueStatus err%+v", err)
  510. return
  511. }
  512. if res == nil {
  513. return
  514. }
  515. if res.Calls != "0" {
  516. utils.LoggerDebug.Printf("PAD SetPadTimer Set QueueTimer timeout %ds !", active.PADTimeout)
  517. //active.SetTimer = true
  518. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !=============")
  519. if active.QueueTimer != nil {
  520. if active.QueueTimer.Stop() {
  521. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer true !============")
  522. } else {
  523. //lfshook.NewLogger().Logger.Infof("=========Release PAD timer false ! ============")
  524. }
  525. }
  526. //lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
  527. active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
  528. //active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  529. //if both not active , return
  530. if active.ActivedCab == "" {
  531. return
  532. }
  533. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  534. if err != nil {
  535. lfshook.NewLogger().Infof("OCC QueueStatus err:%+v", err)
  536. return
  537. }
  538. if res == nil {
  539. return
  540. }
  541. if res.Calls == "0" { // OCC queue empty
  542. /*resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  543. if err != nil {
  544. lfshook.NewLogger().Infof("ICP QueueStatus err:%+v", err)
  545. return
  546. }
  547. if resCaller.Entrys != nil {
  548. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  549. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  550. })
  551. for _, caller := range resCaller.Entrys {
  552. priority.ICPAnswer = 0
  553. //order by pos
  554. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  555. time.Sleep(time.Millisecond * 100) //200 ms delay
  556. }
  557. }*/
  558. for _, ret := range alstatus.PadQueues {
  559. //utils.LoggerDebug.Printf("PAD to OCC exten:%s", ret.Exten)
  560. priority.ICPAnswer = 0
  561. //order by pos
  562. RedirectInQueue(ret.Exten, "0301", "queues-occ", ret.Exten) // redirect All ICP-PAD redirect to OCC queue
  563. time.Sleep(time.Millisecond * 100)
  564. }
  565. }
  566. })
  567. }
  568. }
  569. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  570. action := map[string]string{
  571. "Action": "ConfbridgeKick",
  572. "Conference": confnum,
  573. "Channel": channel,
  574. }
  575. res, _, err = AminInstance.Send(action)
  576. if err != nil {
  577. return nil, err
  578. }
  579. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  580. if res["Response"] != "Success" {
  581. return nil, errors.New(res["Message"])
  582. }
  583. return res, nil
  584. }
  585. func CPAConfbridgeKick(confnum string) (res map[string]string, err error) {
  586. utils.LoggerDebug.Printf("CPA CPAConfbridgeKick , kick all members .")
  587. chans, err := ConfbridgeList(confnum)
  588. if err != nil {
  589. return nil, errors.New(res["Message"])
  590. }
  591. for _, confChan := range chans {
  592. if !strings.Contains(confChan, "PJSIP/1481") {
  593. ConfbridgeKick(confnum, confChan)
  594. }
  595. }
  596. return res, nil
  597. }
  598. func CPAConfbridgeReinvite(confID string) bool {
  599. time.Sleep(time.Millisecond * 100)
  600. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , resume CPA .")
  601. /*if priority.TaskCreating == "" || priority.CABInterrupt == 1 { //高优先级被cabcab打断,或者高优先级直接挂断
  602. for _, ext := range Speakers {
  603. if utils.IsICP(ext) {
  604. if priority.CABInterrupt == 0 {
  605. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  606. continue
  607. }
  608. }
  609. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  610. }
  611. } else {
  612. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Interrupt by other ,return !")
  613. return false
  614. }
  615. return true*/
  616. if priority.PAInterrupt == 1 { // PA 打断标签判断
  617. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , PA ready to go , return !")
  618. return false
  619. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  620. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  621. if utils.IsICP(ext) {
  622. if priority.CABInterrupt == 1 {
  623. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , Get CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  624. continue
  625. }
  626. }
  627. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  628. }
  629. } else if priority.TaskCreating != "" {
  630. utils.LoggerDebug.Printf("CPA ConfbridgeReinvite , other PA ready to go , return !")
  631. return false
  632. } else {
  633. for _, ext := range Speakers {
  634. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  635. }
  636. }
  637. return true
  638. }
  639. func EMGConfbridgeKick(confnum string) (res map[string]string, err error) {
  640. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , kick all members .")
  641. chans, err := ConfbridgeList(confnum)
  642. if err != nil {
  643. return nil, errors.New(res["Message"])
  644. }
  645. for _, confChan := range chans {
  646. if !strings.Contains(confChan, "0502@default") {
  647. ConfbridgeKick(confnum, confChan)
  648. }
  649. }
  650. return res, nil
  651. }
  652. func EMGConfbridgeReinvite(confID string) {
  653. time.Sleep(time.Millisecond * 100)
  654. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , resume EMG .")
  655. if priority.PAInterrupt == 1 { // PA 打断标签判断
  656. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , PA ready to go , return !")
  657. return
  658. } else if priority.CABInterrupt == 1 { //CABCAB 打断标签判断
  659. for _, ext := range Speakers { //高优先级被cabcab打断,或者高优先级直接挂断
  660. if utils.IsICP(ext) {
  661. if priority.CABInterrupt == 1 {
  662. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get CABCAB Interrupt resume EMG , ignore %s continue .", ext)
  663. continue
  664. }
  665. }
  666. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  667. }
  668. } else if priority.TaskCreating != "" {
  669. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , other PA ready to go , return !")
  670. return
  671. } else {
  672. for _, ext := range Speakers {
  673. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  674. }
  675. }
  676. }
  677. func ConfbridgeList(confnum string) (chans []string, err error) {
  678. action := map[string]string{
  679. "Action": "ConfbridgeList",
  680. "Conference": confnum,
  681. }
  682. res, events, err := AminInstance.Send(action)
  683. if err != nil {
  684. return nil, err
  685. }
  686. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  687. if res["Response"] == "Success" {
  688. for _, event := range events {
  689. if event.Data["Event"] == "ConfbridgeList" {
  690. chans = append(chans, event.Data["Channel"])
  691. }
  692. }
  693. return chans, nil
  694. } else {
  695. return nil, errors.New(res["Message"])
  696. }
  697. }
  698. func ConfbridgeReinvite(src, context, confID string) {
  699. if ExtenStatus(src) != "Idle" {
  700. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  701. return
  702. }
  703. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  704. action := map[string]string{
  705. "Action": "Originate",
  706. "Channel": chanel,
  707. "Exten": "000",
  708. "Context": "confbridge-join",
  709. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  710. "Priority": "1",
  711. "Variable": fmt.Sprintf("CBID=%s", confID),
  712. "async": "true",
  713. }
  714. lfshook.NewLogger().Infof("dial action %+v", action)
  715. res, _, err := AminInstance.Send(action)
  716. if err != nil {
  717. lfshook.NewLogger().Errorf("%+v", err)
  718. }
  719. lfshook.NewLogger().Info(res)
  720. }
  721. func ICPConfbridgeReinvite(confID, paType string) {
  722. if priority.CABInterrupt == 1 || priority.CABInterrupt == 1 {
  723. utils.LoggerDebug.Printf("ICPConfbridgeReinvite, Get PA/CABCAB Interrupt resume %s faild .", paType)
  724. return
  725. }
  726. switch paType {
  727. case "PAD-OCC":
  728. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  729. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  730. case "CPA":
  731. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  732. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  733. case "EMG":
  734. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  735. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  736. case "SPC":
  737. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  738. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  739. case "STN":
  740. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  741. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  742. case "DCS":
  743. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  744. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  745. case "CHK":
  746. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  747. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  748. case "VOL":
  749. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  750. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  751. }
  752. }
  753. var waitMutex sync.Mutex
  754. // 设置全局变量控制任务创建过程,避免被其他任务打乱任务创建过程
  755. func WaitTaskCreate(task string, args ...string) { //arg1(task chan)
  756. utils.LoggerDebug.Printf("%s check task creating ..... ,TaskCreating = %s", task, priority.TaskCreating)
  757. waitMutex.Lock()
  758. defer waitMutex.Unlock()
  759. switch priority.TaskCreating {
  760. case "C2C":
  761. if /*task == "PA" ||*/ task == "CPA" {
  762. //获取正在创建的任务的优先级
  763. priorityC2C := priority.GetPriorityByKey("C2C")
  764. //获取将要创建的任务的优先级
  765. priorityTask := priority.GetPriorityByKey(task)
  766. //比较优先级,确定是否终止正在创建的任务
  767. if priorityC2C < priorityTask {
  768. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  769. //结束task(CPA)
  770. if len(args) > 0 {
  771. Hangup(args[0])
  772. goto DELAY
  773. }
  774. } else {
  775. //结束C2C,PA 则不需要挂断ICP(由ICP自行处理优先级)
  776. utils.LoggerDebug.Printf("%s check task C2C creating , hangup C2C ICPs ", task)
  777. HangupICP()
  778. goto DELAY
  779. }
  780. }
  781. return
  782. case "CPA":
  783. if task == "PA" /*|| task == "C2C"*/ {
  784. //获取正在创建的任务的优先级
  785. priorityCPA := priority.GetPriorityByKey("CPA")
  786. //获取将要创建的任务的优先级
  787. priorityTask := priority.GetPriorityByKey(task)
  788. //比较优先级,确定是否终止正在创建的任务
  789. if priorityCPA < priorityTask {
  790. utils.LoggerDebug.Printf("%s check task CPA creating , hangup %s %s", task, task, args[0])
  791. //结束task(PA,CPA)
  792. if len(args) > 0 {
  793. Hangup(args[0])
  794. goto DELAY
  795. }
  796. } else {
  797. //结束CPA,获取CPA通道
  798. if active.ActivedCab == "1" {
  799. utils.LoggerDebug.Printf("%s check task creating , hangup CPA %s ", task, args[0])
  800. Hangup("1481")
  801. goto DELAY
  802. } else if active.ActivedCab == "8" {
  803. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  804. Hangup("1411")
  805. goto DELAY
  806. }
  807. }
  808. }
  809. return
  810. case "PA":
  811. if task == "CPA" /*|| task == "C2C"*/ {
  812. //获取正在创建的任务的优先级
  813. priorityPA := priority.GetPriorityByKey("PA")
  814. //获取将要创建的任务的优先级
  815. priorityTask := priority.GetPriorityByKey(task)
  816. //比较优先级,确定是否终止正在创建的任务
  817. if priorityPA < priorityTask {
  818. utils.LoggerDebug.Printf("%s check task PA creating , hangup CPA %s ", task, args[0])
  819. //结束task(CPA)
  820. if len(args) > 0 {
  821. Hangup(args[0])
  822. goto DELAY
  823. }
  824. } else {
  825. //结束PA,获取PA通道
  826. if active.ActivedCab == "1" {
  827. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2311")
  828. Hangup("2311")
  829. goto DELAY
  830. } else if active.ActivedCab == "8" {
  831. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2381")
  832. Hangup("2381")
  833. goto DELAY
  834. }
  835. }
  836. }
  837. return
  838. default:
  839. //utils.LoggerDebug.Printf("%s waiting trd=============previous task:%s creating ..... ", task, priority.TaskCreating)
  840. for i := 0; i < 4; i++ {
  841. if priority.TaskCreating != "" {
  842. utils.LoggerDebug.Printf("%s waiting previous task:%s creating ..... ", task, priority.TaskCreating)
  843. time.Sleep(time.Millisecond * 500)
  844. } else {
  845. utils.LoggerDebug.Printf("TaskCreating is nill, Set TaskCreating=%s", task)
  846. priority.TaskCreating = task
  847. return
  848. }
  849. }
  850. priority.TaskCreating = task
  851. utils.LoggerDebug.Printf("%s waiting previous task:%s creating timeout ! Set TaskCreating=%s", task, priority.TaskCreating, task)
  852. return
  853. }
  854. DELAY:
  855. time.Sleep(100 * time.Millisecond)
  856. }