call.go 26 KB

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