call.go 28 KB

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