call.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. "pbx-api-gin/pkg/lfshook"
  8. "pbx-api-gin/pkg/utils"
  9. "sort"
  10. "strings"
  11. "time"
  12. )
  13. var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444",
  14. "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483",
  15. "2484", "2485", "2411", "2412"}
  16. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  17. var Speakers = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181", "2311", "2381"}
  18. // Function triggered before no cab occupied signal interrupt
  19. func InActiveHangup() {
  20. if active.ActivedCab == "" {
  21. switch priority.RunningType {
  22. case "PA":
  23. HangupRunningTask("InActiveHangup")
  24. case "CPA":
  25. HangupRunningTask("InActiveHangup")
  26. case "VOL":
  27. HangupRunningTask("InActiveHangup")
  28. case "PAD-OCC":
  29. HangupRunningTask("InActiveHangup")
  30. }
  31. }
  32. }
  33. // Hangup 挂断指定分机或通道
  34. func Hangup(channel string) {
  35. lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
  36. if !utils.IsChannel(channel) {
  37. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  38. }
  39. action := map[string]string{
  40. "Action": "hangup",
  41. "Channel": channel,
  42. }
  43. lfshook.NewLogger().Infof("hangup action %+v", action)
  44. if _, _, err := AminInstance.Send(action); err != nil {
  45. lfshook.NewLogger().Errorf("Hangup %+v", err)
  46. }
  47. }
  48. // Hangup 挂断所有分机,除指定分机和PAD
  49. func HangupAllExcept(caller string) {
  50. //all PACU
  51. for _, ret := range Pacus {
  52. Hangup(ret)
  53. }
  54. switch caller {
  55. case "2311":
  56. Hangup("1411") //IO1
  57. Hangup("1481") //IO8
  58. //Hangup("2311") //ICP1
  59. Hangup("2381") //ICP8
  60. case "2381":
  61. Hangup("1411") //IO1
  62. Hangup("1481") //IO8
  63. Hangup("2311") //ICP1
  64. //Hangup("2381") //ICP8
  65. case "1411":
  66. //Hangup("1411") //IO1
  67. Hangup("1481") //IO8
  68. Hangup("2311") //ICP1
  69. Hangup("2381") //ICP8
  70. case "1481":
  71. Hangup("1411") //IO1
  72. //Hangup("1481") //IO8
  73. Hangup("2311") //ICP1
  74. Hangup("2381") //ICP8
  75. case "":
  76. Hangup("1411") //IO1
  77. Hangup("1481") //IO8
  78. Hangup("2311") //ICP1
  79. Hangup("2381") //ICP8
  80. }
  81. }
  82. // Hangup all ICP
  83. func HangupRunningTask(toRunTask string) {
  84. lfshook.NewLogger().Infof("===HangupRunningTask=toRuntask=%s==== RunningTask:%s===chan:%s", toRunTask, priority.RunningType, priority.RunningPATaskChan)
  85. //same type return
  86. if toRunTask == priority.RunningType {
  87. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  88. lfshook.NewLogger().Infof("===HangupRunningTask=ret==== ")
  89. return
  90. }
  91. }
  92. //pad all reset
  93. if toRunTask == "AlarmHoldResetAll" {
  94. if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  95. Hangup(priority.RunningPATaskChan)
  96. }
  97. priority.CleanPriorityTag()
  98. HangupAllLocalChan()
  99. return
  100. }
  101. //kick EMG member
  102. if active.EMGCONFID != "" {
  103. EMGConfbridgeKick(active.EMGCONFID)
  104. return
  105. }
  106. //kick CPA member
  107. //if active.CPACONFID != "" {
  108. // CPAConfbridgeKick(active.CPACONFID)
  109. // return
  110. //}
  111. if priority.RunningPATaskChan != "" {
  112. if priority.RunningType == "C2C" { // Interrupt C2C task running,
  113. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  114. HangupICP()
  115. //priority.CleanPriorityTag()
  116. }
  117. }
  118. switch priority.RunningType {
  119. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  120. priority.InterruptedPad = priority.RunningType
  121. //just in case
  122. if toRunTask == "C2C" { //deal in DTMF event
  123. lfshook.NewLogger().Infof("===HangupRunningTask==C2C=return==== ")
  124. return
  125. }
  126. lfshook.NewLogger().Infof("===HangupRunningTask==hangup===PAD-ICP==%s==== ", priority.RunningType)
  127. chans, err := CoreShowChannels()
  128. if err != nil {
  129. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  130. }
  131. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  132. for _, ret := range chans {
  133. if utils.IsPAIU(ret.CallerIDNum) {
  134. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  135. //lfshook.NewLogger().Infof("===Redirect=Chan===%+v==== ", ret.Channel)
  136. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  137. if err != nil {
  138. lfshook.NewLogger().Errorf("Redirect %+v", err)
  139. }
  140. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  141. active.NotifyPaiu(number, "hold")
  142. //HangupAllLocalChan()
  143. } else if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  144. lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  145. Hangup(ret.Channel)
  146. }
  147. }
  148. }
  149. //2. hangup task channel (ICP + PACU)
  150. lfshook.NewLogger().Infof("===HangupRunningTask=2. hangup task channel === ")
  151. HangupAllLocalChan()
  152. //HangupICP()
  153. priority.CleanPriorityTag()
  154. case "PAD-OCC": // Interrupt PAD-OCC task running,
  155. if toRunTask == "C2C" {
  156. break
  157. } else {
  158. priority.InterruptedPad = priority.RunningType
  159. //1. Redirect all the other pads in 0301 to 0300
  160. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  161. if err != nil {
  162. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  163. }
  164. for _, caller := range resCaller.Entrys {
  165. go RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  166. time.Sleep(time.Millisecond * 100)
  167. }
  168. //2. Hangup connected PAD
  169. lfshook.NewLogger().Infof("===HangupRunningTask=Hangup connected PAD=== ")
  170. Hangup(priority.RunningPATaskChan)
  171. //3. Hangup OI & ICP
  172. HangupIO()
  173. HangupAllLocalChan()
  174. priority.CleanPriorityTag()
  175. }
  176. default:
  177. lfshook.NewLogger().Infof("===HangupRunningTask=default=== ")
  178. if !strings.Contains(priority.RunningPATaskChan, "0502@default") {
  179. Hangup(priority.RunningPATaskChan)
  180. }
  181. priority.CleanPriorityTag()
  182. }
  183. }
  184. }
  185. // Hangup all ICP
  186. func HangupICP() {
  187. Hangup("2311") //ICP1
  188. Hangup("2381") //ICP8
  189. }
  190. // Hangup all IO
  191. func HangupIO() {
  192. Hangup("1411") //IO1
  193. Hangup("1481") //IO8
  194. }
  195. // Hangup all PACU
  196. func HangupAllPACU() {
  197. //all PACU
  198. for _, ret := range Pacus {
  199. Hangup(ret)
  200. }
  201. }
  202. func HangupAllLocalChan() {
  203. chans, err := CoreShowChannels()
  204. if err != nil {
  205. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  206. }
  207. for _, ret := range chans {
  208. if strings.Contains(ret.Channel, "Local") && !strings.Contains(ret.Channel, "0502@default") {
  209. lfshook.NewLogger().Infof("HangupAllLocalChan=====hangup========= %+v", ret)
  210. Hangup(ret.Channel)
  211. }
  212. }
  213. }
  214. // Hangup all PACU
  215. func HangupAllPAD() {
  216. //all PAD
  217. for _, ret := range Pads {
  218. Hangup(ret)
  219. }
  220. }
  221. // Hangup all calls
  222. func HangupAll() {
  223. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  224. }
  225. // Dial 拨打号码
  226. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  227. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  228. action := map[string]string{
  229. "Action": "Originate",
  230. "Channel": chanel,
  231. "Exten": dst,
  232. "Context": dialrule,
  233. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  234. "Priority": "1",
  235. "Variable": fmt.Sprintf("CAB=%s", callType),
  236. "async": "true",
  237. }
  238. lfshook.NewLogger().Infof("dial action %+v", action)
  239. res, _, err := AminInstance.Send(action)
  240. if err != nil {
  241. lfshook.NewLogger().Errorf("%+v", err)
  242. }
  243. lfshook.NewLogger().Info(res)
  244. }
  245. // Dial 拨打号码
  246. func DialICP(src, dst, dialrule, callerID, callerName string) {
  247. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  248. action := map[string]string{
  249. "Action": "Originate",
  250. "Channel": chanel,
  251. "Exten": dst,
  252. "Context": dialrule,
  253. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  254. "Priority": "1",
  255. "Variable": fmt.Sprintf("CBID=%s", callerID),
  256. "async": "true",
  257. }
  258. lfshook.NewLogger().Infof("dial action %+v", action)
  259. res, _, err := AminInstance.Send(action)
  260. if err != nil {
  261. lfshook.NewLogger().Errorf("%+v", err)
  262. }
  263. lfshook.NewLogger().Info(res)
  264. }
  265. // 获取分机状态
  266. func ExtenStatus(exten string) (Status string) {
  267. action := map[string]string{
  268. "Action": "ExtensionState",
  269. "Exten": exten,
  270. "Context": "default",
  271. }
  272. res, _, err := AminInstance.Send(action)
  273. if err != nil {
  274. lfshook.NewLogger().Errorf("%+v", err)
  275. return ""
  276. }
  277. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  278. return res["StatusText"]
  279. }
  280. // PACU ChanSpy
  281. func ChanSpy(src, dst string, whisper, bargein bool) {
  282. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  283. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  284. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  285. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  286. /*
  287. if whisper {
  288. data = fmt.Sprintf("%s,w", data)
  289. }
  290. if bargein {
  291. data = fmt.Sprintf("%s,B", data)
  292. }
  293. */
  294. action := map[string]string{
  295. "Action": "Originate",
  296. "Channel": channel, // 不存在的通话
  297. "Application": "ChanSpy",
  298. "Data": data, // 存在的通话
  299. "CallerID": dst,
  300. "Async": "true",
  301. }
  302. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  303. _, _, err := AminInstance.Send(action)
  304. if err != nil {
  305. lfshook.NewLogger().Errorf("%+v", err)
  306. }
  307. }
  308. // Redirect 转接
  309. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  310. //callerID := "redirect"
  311. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  312. if !utils.IsChannel(channel) {
  313. //callerID = channel
  314. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  315. return err
  316. }
  317. }
  318. action := map[string]string{
  319. "Action": "Redirect",
  320. "Channel": channel,
  321. "Exten": dst,
  322. "Context": dialrule,
  323. "CallerID": callerID,
  324. "Priority": "1",
  325. "async": "true",
  326. }
  327. lfshook.NewLogger().Infof("redirect %+v", action)
  328. res, _, err := AminInstance.Send(action)
  329. if err != nil {
  330. lfshook.NewLogger().Error(err)
  331. }
  332. lfshook.NewLogger().Info(res)
  333. return err
  334. }
  335. // Redirect 转接
  336. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  337. //callerID := "redirect"
  338. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  339. if !utils.IsChannel(channel) {
  340. callerID = channel
  341. if channel, err = GetChannelByExten(channel); err != nil {
  342. return err
  343. }
  344. }
  345. action := map[string]string{
  346. "Action": "Redirect",
  347. "Channel": channel,
  348. "Exten": dst,
  349. "Context": dialrule,
  350. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  351. "Priority": "1",
  352. "async": "true",
  353. }
  354. lfshook.NewLogger().Infof("redirect %+v", action)
  355. res, _, err := AminInstance.Send(action)
  356. if err != nil {
  357. lfshook.NewLogger().Error(err)
  358. }
  359. lfshook.NewLogger().Info(res)
  360. return err
  361. }
  362. func SetPadTimer() {
  363. toRunpriority := priority.GetPriorityByKey(priority.InterruptedPad) // 获取之前打断的报警优先级
  364. lfshook.NewLogger().Infof("==SetPadTimer====runing:%d==toRun:%d=", priority.RunningTypePriority, toRunpriority)
  365. if priority.RunningTypePriority < toRunpriority || priority.RunningTypePriority == 0 {
  366. res, err := QueueStatus("0300", "") // check OCC queue , if empty OCC-PAD start
  367. if err != nil {
  368. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  369. return
  370. }
  371. if res.Calls != "0" {
  372. lfshook.NewLogger().Infof("==SetPadTimer==QueueTimer===1====")
  373. active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  374. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  375. if err != nil {
  376. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  377. return
  378. }
  379. if res.Calls == "0" { // OCC queue empty
  380. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  381. if err != nil {
  382. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  383. return
  384. }
  385. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  386. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  387. })
  388. for _, caller := range resCaller.Entrys {
  389. priority.ICPAnswer = 0
  390. //lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=")
  391. lfshook.NewLogger().Infof("==SetPadTimer==Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  392. //order by pos
  393. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  394. time.Sleep(time.Millisecond * 100) //200 ms delay
  395. }
  396. }
  397. })
  398. }
  399. }
  400. }
  401. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  402. action := map[string]string{
  403. "Action": "ConfbridgeKick",
  404. "Conference": confnum,
  405. "Channel": channel,
  406. }
  407. res, _, err = AminInstance.Send(action)
  408. if err != nil {
  409. return nil, err
  410. }
  411. lfshook.NewLogger().Infof("=======ConfbridgeKick==%+v", res)
  412. if res["Response"] != "Success" {
  413. return nil, errors.New(res["Message"])
  414. }
  415. return res, nil
  416. }
  417. /*
  418. func CPAConfbridgeKick(confnum string) (res map[string]string, err error) {
  419. lfshook.NewLogger().Infof("=======CPAConfbridgeKick==%s", confnum)
  420. chans, err := ConfbridgeList(confnum)
  421. if err != nil {
  422. return nil, errors.New(res["Message"])
  423. }
  424. for _, confChan := range chans {
  425. lfshook.NewLogger().Infof("======CPAConfbridgeKick==1=%+v", confChan)
  426. if !strings.Contains(confChan, "PJSIP/1481") {
  427. ConfbridgeKick(confnum, confChan)
  428. }
  429. }
  430. return res, nil
  431. }
  432. */
  433. func CPAConfbridgeReinvite(confID string) {
  434. lfshook.NewLogger().Infof("=====CPAConfbridgeReinvite==")
  435. for _, ext := range Speakers {
  436. ConfbridgeReinvite(ext, "call-speakers-cpa", confID)
  437. }
  438. }
  439. func EMGConfbridgeKick(confnum string) (res map[string]string, err error) {
  440. lfshook.NewLogger().Infof("=====EMGConfbridgeKick==%s", confnum)
  441. chans, err := ConfbridgeList(confnum)
  442. if err != nil {
  443. return nil, errors.New(res["Message"])
  444. }
  445. for _, confChan := range chans {
  446. lfshook.NewLogger().Infof("====EMGConfbridgeKick==1=%+v", confChan)
  447. if !strings.Contains(confChan, "0502@default") {
  448. ConfbridgeKick(confnum, confChan)
  449. }
  450. }
  451. return res, nil
  452. }
  453. func EMGConfbridgeReinvite(confID string) {
  454. lfshook.NewLogger().Infof("=======EMGConfbridgeReinvite==")
  455. time.Sleep(time.Millisecond * 800)
  456. for _, ext := range Speakers {
  457. ConfbridgeReinvite(ext, "call-speakers-emg", confID)
  458. }
  459. }
  460. func ConfbridgeList(confnum string) (chans []string, err error) {
  461. action := map[string]string{
  462. "Action": "ConfbridgeList",
  463. "Conference": confnum,
  464. }
  465. res, events, err := AminInstance.Send(action)
  466. if err != nil {
  467. return nil, err
  468. }
  469. lfshook.NewLogger().Infof("===ConfbridgeList=res=%+v", res)
  470. if res["Response"] == "Success" {
  471. for _, event := range events {
  472. if event.Data["Event"] == "ConfbridgeList" {
  473. //lfshook.NewLogger().Infof("=======ConfbridgeList==%+v", event)
  474. chans = append(chans, event.Data["Channel"])
  475. }
  476. }
  477. return chans, nil
  478. } else {
  479. return nil, errors.New(res["Message"])
  480. }
  481. }
  482. func ConfbridgeReinvite(src, context, confID string) {
  483. if ExtenStatus(src) != "Idle" {
  484. //lfshook.NewLogger().Infof("===============ext:%s==!Idle===========", src)
  485. return
  486. }
  487. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  488. action := map[string]string{
  489. "Action": "Originate",
  490. "Channel": chanel,
  491. "Exten": "000",
  492. "Context": "confbridge-join",
  493. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  494. "Priority": "1",
  495. "Variable": fmt.Sprintf("CBID=%s", confID),
  496. "async": "true",
  497. }
  498. lfshook.NewLogger().Infof("dial action %+v", action)
  499. res, _, err := AminInstance.Send(action)
  500. if err != nil {
  501. lfshook.NewLogger().Errorf("%+v", err)
  502. }
  503. lfshook.NewLogger().Info(res)
  504. }
  505. func ICPConfbridgeReinvite(confID, paType string) {
  506. switch paType {
  507. case "PAD-OCC":
  508. go DialICP("8", "2311", "confbridge-join", confID, "1") //ICP1---call
  509. go DialICP("8", "2381", "confbridge-join", confID, "8") //ICP8---call
  510. case "CPA":
  511. go DialICP("2", "2311", "confbridge-join", confID, "1") //ICP1---call
  512. go DialICP("2", "2381", "confbridge-join", confID, "8") //ICP8---call
  513. case "EMG":
  514. go DialICP("3", "2311", "confbridge-join", confID, "1") //ICP1---call
  515. go DialICP("3", "2381", "confbridge-join", confID, "8") //ICP8---call
  516. case "SPC":
  517. go DialICP("6", "2311", "confbridge-join", confID, "1") //ICP1---call
  518. go DialICP("6", "2381", "confbridge-join", confID, "8") //ICP8---call
  519. case "STN":
  520. go DialICP("4", "2311", "confbridge-join", confID, "1") //ICP1---call
  521. go DialICP("4", "2381", "confbridge-join", confID, "8") //ICP8---call
  522. case "DCS":
  523. go DialICP("5", "2311", "confbridge-join", confID, "1") //ICP1---call
  524. go DialICP("5", "2381", "confbridge-join", confID, "8") //ICP8---call
  525. case "CHK":
  526. go DialICP("10", "2311", "confbridge-join", confID, "1") //ICP1---call
  527. go DialICP("10", "2381", "confbridge-join", confID, "8") //ICP8---call
  528. case "VOL":
  529. go DialICP("11", "2311", "confbridge-join", confID, "1") //ICP1---call
  530. go DialICP("11", "2381", "confbridge-join", confID, "8") //ICP8---call
  531. }
  532. }