call.go 26 KB

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