call.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. if priority.PAInterrupt == 1 {
  621. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Get PA Interrupt PAInterrupt == 1 return !")
  622. return false
  623. }
  624. time.Sleep(time.Millisecond * 100)
  625. for _, ext := range Speakers {
  626. if utils.IsICP(ext) {
  627. if priority.CABInterrupt == 1 {
  628. utils.LoggerDebug.Printf("CPA CPAConfbridgeReinvite , Get PA/CABCAB Interrupt resume CPA , ignore %s continue .", ext)
  629. continue
  630. }
  631. }
  632. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  633. }
  634. return true
  635. }
  636. func EMGConfbridgeKick(confnum string) (res map[string]string, err error) {
  637. utils.LoggerDebug.Printf("EMG EMGConfbridgeKick , kick all members .")
  638. chans, err := ConfbridgeList(confnum)
  639. if err != nil {
  640. return nil, errors.New(res["Message"])
  641. }
  642. for _, confChan := range chans {
  643. if !strings.Contains(confChan, "0502@default") {
  644. ConfbridgeKick(confnum, confChan)
  645. }
  646. }
  647. return res, nil
  648. }
  649. func EMGConfbridgeReinvite(confID string) {
  650. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , resume EMG .")
  651. if priority.PAInterrupt == 1 {
  652. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get PA Interrupt PAInterrupt == 1 return !")
  653. return
  654. }
  655. time.Sleep(time.Millisecond * 100)
  656. for _, ext := range Speakers {
  657. if utils.IsICP(ext) {
  658. if priority.CABInterrupt == 1 {
  659. utils.LoggerDebug.Printf("EMG ConfbridgeReinvite , Get CABCAB Interrupt resume EMG , ignore %s continue .", ext)
  660. continue
  661. }
  662. }
  663. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  664. }
  665. }
  666. func ConfbridgeList(confnum string) (chans []string, err error) {
  667. action := map[string]string{
  668. "Action": "ConfbridgeList",
  669. "Conference": confnum,
  670. }
  671. res, events, err := AminInstance.Send(action)
  672. if err != nil {
  673. return nil, err
  674. }
  675. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  676. if res["Response"] == "Success" {
  677. for _, event := range events {
  678. if event.Data["Event"] == "ConfbridgeList" {
  679. chans = append(chans, event.Data["Channel"])
  680. }
  681. }
  682. return chans, nil
  683. } else {
  684. return nil, errors.New(res["Message"])
  685. }
  686. }
  687. func ConfbridgeReinvite(src, context, confID string) {
  688. if ExtenStatus(src) != "Idle" {
  689. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  690. return
  691. }
  692. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  693. action := map[string]string{
  694. "Action": "Originate",
  695. "Channel": chanel,
  696. "Exten": "000",
  697. "Context": "confbridge-join",
  698. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  699. "Priority": "1",
  700. "Variable": fmt.Sprintf("CBID=%s", confID),
  701. "async": "true",
  702. }
  703. lfshook.NewLogger().Infof("dial action %+v", action)
  704. res, _, err := AminInstance.Send(action)
  705. if err != nil {
  706. lfshook.NewLogger().Errorf("%+v", err)
  707. }
  708. lfshook.NewLogger().Info(res)
  709. }
  710. func ICPConfbridgeReinvite(confID, paType string) {
  711. if priority.CABInterrupt == 1 || priority.PAInterrupt == 1 {
  712. utils.LoggerDebug.Printf("ICPConfbridgeReinvite, Get PA/CABCAB Interrupt resume %s faild .", paType)
  713. return
  714. }
  715. switch paType {
  716. case "PAD-OCC":
  717. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  718. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  719. case "CPA":
  720. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  721. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  722. case "EMG":
  723. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  724. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  725. case "SPC":
  726. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  727. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  728. case "STN":
  729. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  730. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  731. case "DCS":
  732. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  733. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  734. case "CHK":
  735. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  736. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  737. case "VOL":
  738. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  739. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  740. }
  741. }
  742. var waitMutex sync.Mutex
  743. // 设置全局变量控制任务创建过程,避免被其他任务打乱任务创建过程
  744. func WaitTaskCreate(task string, args ...string) { //arg1(task chan)
  745. utils.LoggerDebug.Printf("%s check task creating ..... ,TaskCreating = %s", task, priority.TaskCreating)
  746. waitMutex.Lock()
  747. defer waitMutex.Unlock()
  748. switch priority.TaskCreating {
  749. case "C2C":
  750. if /*task == "PA" ||*/ task == "CPA" {
  751. //获取正在创建的任务的优先级
  752. priorityC2C := priority.GetPriorityByKey("C2C")
  753. //获取将要创建的任务的优先级
  754. priorityTask := priority.GetPriorityByKey(task)
  755. //比较优先级,确定是否终止正在创建的任务
  756. if priorityC2C < priorityTask {
  757. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  758. //结束task(CPA)
  759. if len(args) > 0 {
  760. Hangup(args[0])
  761. goto DELAY
  762. }
  763. } else {
  764. //结束C2C,PA 则不需要挂断ICP(由ICP自行处理优先级)
  765. utils.LoggerDebug.Printf("%s check task C2C creating , hangup C2C ICPs ", task)
  766. HangupICP()
  767. goto DELAY
  768. }
  769. }
  770. return
  771. case "CPA":
  772. if task == "PA" /*|| task == "C2C"*/ {
  773. //获取正在创建的任务的优先级
  774. priorityCPA := priority.GetPriorityByKey("CPA")
  775. //获取将要创建的任务的优先级
  776. priorityTask := priority.GetPriorityByKey(task)
  777. //比较优先级,确定是否终止正在创建的任务
  778. if priorityCPA < priorityTask {
  779. utils.LoggerDebug.Printf("%s check task CPA creating , hangup %s %s", task, task, args[0])
  780. //结束task(PA,CPA)
  781. if len(args) > 0 {
  782. Hangup(args[0])
  783. goto DELAY
  784. }
  785. } else {
  786. //结束CPA,获取CPA通道
  787. if active.ActivedCab == "1" {
  788. utils.LoggerDebug.Printf("%s check task creating , hangup CPA %s ", task, args[0])
  789. Hangup("1481")
  790. goto DELAY
  791. } else if active.ActivedCab == "8" {
  792. utils.LoggerDebug.Printf("%s check task C2C creating , hangup CPA %s ", task, args[0])
  793. Hangup("1411")
  794. goto DELAY
  795. }
  796. }
  797. }
  798. return
  799. case "PA":
  800. if task == "CPA" /*|| task == "C2C"*/ {
  801. //获取正在创建的任务的优先级
  802. priorityPA := priority.GetPriorityByKey("PA")
  803. //获取将要创建的任务的优先级
  804. priorityTask := priority.GetPriorityByKey(task)
  805. //比较优先级,确定是否终止正在创建的任务
  806. if priorityPA < priorityTask {
  807. utils.LoggerDebug.Printf("%s check task PA creating , hangup CPA %s ", task, args[0])
  808. //结束task(CPA)
  809. if len(args) > 0 {
  810. Hangup(args[0])
  811. goto DELAY
  812. }
  813. } else {
  814. //结束PA,获取PA通道
  815. if active.ActivedCab == "1" {
  816. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2311")
  817. Hangup("2311")
  818. goto DELAY
  819. } else if active.ActivedCab == "8" {
  820. utils.LoggerDebug.Printf("%s check task PA creating , hangup PA %s ", task, "2381")
  821. Hangup("2381")
  822. goto DELAY
  823. }
  824. }
  825. }
  826. return
  827. default:
  828. //utils.LoggerDebug.Printf("%s waiting trd=============previous task:%s creating ..... ", task, priority.TaskCreating)
  829. for i := 0; i < 4; i++ {
  830. if priority.TaskCreating != "" {
  831. utils.LoggerDebug.Printf("%s waiting previous task:%s creating ..... ", task, priority.TaskCreating)
  832. time.Sleep(time.Millisecond * 500)
  833. } else {
  834. utils.LoggerDebug.Printf("TaskCreating is nill, Set TaskCreating=%s", task)
  835. priority.TaskCreating = task
  836. return
  837. }
  838. }
  839. priority.TaskCreating = task
  840. utils.LoggerDebug.Printf("%s waiting previous task:%s creating timeout ! Set TaskCreating=%s", task, priority.TaskCreating, task)
  841. return
  842. }
  843. DELAY:
  844. time.Sleep(100 * time.Millisecond)
  845. }