call.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. "strconv"
  11. "strings"
  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. // 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. // check EMG resume
  34. func CheckEmgResume() {
  35. /*
  36. time.AfterFunc(2*time.Second, func() {
  37. if priority.CheckPriority("EMG") || priority.RunningType == "C2C" {
  38. PlaybackPacu(strconv.Quote(priority.ResumeEmgPara.FileName), priority.ResumeEmgPara.Count, priority.ResumeEmgPara.Delay, priority.ResumeEmgPara.BroadcastType)
  39. }
  40. })*/
  41. }
  42. // Hangup 挂断指定分机或通道
  43. func Hangup(channel string) {
  44. lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
  45. if !utils.IsChannel(channel) {
  46. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  47. }
  48. action := map[string]string{
  49. "Action": "hangup",
  50. "Channel": channel,
  51. }
  52. lfshook.NewLogger().Infof("hangup action %+v", action)
  53. if _, _, err := AminInstance.Send(action); err != nil {
  54. lfshook.NewLogger().Errorf("Hangup %+v", err)
  55. }
  56. }
  57. // Hangup 挂断所有分机,除指定分机和PAD
  58. func HangupAllExcept(caller string) {
  59. //all PACU
  60. for _, ret := range Pacus {
  61. Hangup(ret)
  62. }
  63. switch caller {
  64. case "2311":
  65. Hangup("1411") //IO1
  66. Hangup("1481") //IO8
  67. //Hangup("2311") //ICP1
  68. Hangup("2381") //ICP8
  69. case "2381":
  70. Hangup("1411") //IO1
  71. Hangup("1481") //IO8
  72. Hangup("2311") //ICP1
  73. //Hangup("2381") //ICP8
  74. case "1411":
  75. //Hangup("1411") //IO1
  76. Hangup("1481") //IO8
  77. Hangup("2311") //ICP1
  78. Hangup("2381") //ICP8
  79. case "1481":
  80. Hangup("1411") //IO1
  81. //Hangup("1481") //IO8
  82. Hangup("2311") //ICP1
  83. Hangup("2381") //ICP8
  84. case "":
  85. Hangup("1411") //IO1
  86. Hangup("1481") //IO8
  87. Hangup("2311") //ICP1
  88. Hangup("2381") //ICP8
  89. }
  90. }
  91. // Hangup all ICP
  92. func HangupRunningTask(toRunTask string) {
  93. lfshook.NewLogger().Infof("===HangupRunningTask=toRuntask=%s==== RunningTask:%s===", toRunTask, priority.RunningType)
  94. //same type return
  95. if toRunTask == priority.RunningType {
  96. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  97. lfshook.NewLogger().Infof("===HangupRunningTask=ret==== ")
  98. return
  99. }
  100. }
  101. if toRunTask == "AlarmHoldResetAll" {
  102. Hangup(priority.RunningPATaskChan)
  103. priority.CleanPriorityTag()
  104. HangupAllLocalChan()
  105. return
  106. }
  107. if priority.RunningPATaskChan != "" {
  108. if priority.RunningType == "C2C" { // Interrupt C2C task running,
  109. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  110. HangupICP()
  111. //priority.CleanPriorityTag()
  112. }
  113. }
  114. switch priority.RunningType {
  115. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  116. priority.InterruptedPad = priority.RunningType
  117. //just in case
  118. if toRunTask == "C2C" { //deal in DTMF event
  119. lfshook.NewLogger().Infof("===HangupRunningTask==C2C=return==== ")
  120. return
  121. }
  122. lfshook.NewLogger().Infof("===HangupRunningTask==hangup===PAD-ICP==%s==== ", priority.RunningType)
  123. chans, err := CoreShowChannels()
  124. if err != nil {
  125. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  126. }
  127. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  128. for _, ret := range chans {
  129. if utils.IsPAIU(ret.CallerIDNum) {
  130. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  131. //lfshook.NewLogger().Infof("===Redirect=Chan===%+v==== ", ret.Channel)
  132. err := Redirect(ret.Channel, "0300", "queues-icp-redirect", "", "PAD")
  133. if err != nil {
  134. lfshook.NewLogger().Errorf("Redirect %+v", err)
  135. }
  136. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  137. active.NotifyPaiu(number, "hold")
  138. HangupAllLocalChan()
  139. } else if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  140. lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  141. Hangup(ret.Channel)
  142. }
  143. }
  144. }
  145. //2. hangup task channel (ICP + PACU)
  146. lfshook.NewLogger().Infof("===HangupRunningTask=2. hangup task channel === ")
  147. HangupAllLocalChan()
  148. //HangupICP()
  149. priority.CleanPriorityTag()
  150. case "PAD-OCC": // Interrupt PAD-OCC task running,
  151. if toRunTask == "C2C" {
  152. break
  153. } else {
  154. priority.InterruptedPad = priority.RunningType
  155. //1. Redirect all the other pads in 0301 to 0300
  156. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  157. if err != nil {
  158. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  159. }
  160. for _, caller := range resCaller.Entrys {
  161. go RedirectInQueue(caller.CallerIDNum, "0300", "queues-icp-redirect", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  162. time.Sleep(time.Microsecond * 200)
  163. }
  164. //2. Hangup connected PAD
  165. lfshook.NewLogger().Infof("===HangupRunningTask=Hangup connected PAD=== ")
  166. Hangup(priority.RunningPATaskChan)
  167. //3. Hangup OI & ICP
  168. HangupIO()
  169. HangupAllLocalChan()
  170. priority.CleanPriorityTag()
  171. }
  172. default:
  173. lfshook.NewLogger().Infof("===HangupRunningTask=default=== ")
  174. Hangup(priority.RunningPATaskChan)
  175. priority.CleanPriorityTag()
  176. }
  177. }
  178. }
  179. // Hangup all ICP
  180. func HangupICP() {
  181. Hangup("2311") //ICP1
  182. Hangup("2381") //ICP8
  183. }
  184. // Hangup all IO
  185. func HangupIO() {
  186. Hangup("1411") //IO1
  187. Hangup("1481") //IO8
  188. }
  189. // Hangup all PACU
  190. func HangupAllPACU() {
  191. //all PACU
  192. for _, ret := range Pacus {
  193. Hangup(ret)
  194. }
  195. }
  196. func HangupAllLocalChan() {
  197. chans, err := CoreShowChannels()
  198. if err != nil {
  199. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  200. }
  201. for _, ret := range chans {
  202. if strings.Contains(ret.Channel, "Local") {
  203. Hangup(ret.Channel)
  204. }
  205. }
  206. }
  207. // Hangup all PACU
  208. func HangupAllPAD() {
  209. //all PAD
  210. for _, ret := range Pads {
  211. Hangup(ret)
  212. }
  213. }
  214. // Hangup all calls
  215. func HangupAll() {
  216. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  217. }
  218. // Dial 拨打号码
  219. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  220. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  221. action := map[string]string{
  222. "Action": "Originate",
  223. "Channel": chanel,
  224. "Exten": dst,
  225. "Context": dialrule,
  226. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  227. "Priority": "1",
  228. "Variable": fmt.Sprintf("CAB=%s", callType),
  229. "async": "true",
  230. }
  231. lfshook.NewLogger().Infof("dial action %+v", action)
  232. res, _, err := AminInstance.Send(action)
  233. if err != nil {
  234. lfshook.NewLogger().Errorf("%+v", err)
  235. }
  236. lfshook.NewLogger().Info(res)
  237. }
  238. // Dial 拨打号码
  239. func DialICP(src, dst, dialrule, callerID, callerName string) {
  240. chanel := fmt.Sprintf("%s/%s@call-icp", "Local", src)
  241. action := map[string]string{
  242. "Action": "Originate",
  243. "Channel": chanel,
  244. "Exten": dst,
  245. "Context": dialrule,
  246. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  247. "Priority": "1",
  248. "Variable": fmt.Sprintf("CBID=%s", callerID),
  249. "async": "true",
  250. }
  251. lfshook.NewLogger().Infof("dial action %+v", action)
  252. res, _, err := AminInstance.Send(action)
  253. if err != nil {
  254. lfshook.NewLogger().Errorf("%+v", err)
  255. }
  256. lfshook.NewLogger().Info(res)
  257. }
  258. // 获取分机状态
  259. func ExtenStatus(exten string) (Status string) {
  260. action := map[string]string{
  261. "Action": "ExtensionState",
  262. "Exten": exten,
  263. "Context": "default",
  264. }
  265. res, _, err := AminInstance.Send(action)
  266. if err != nil {
  267. lfshook.NewLogger().Errorf("%+v", err)
  268. return ""
  269. }
  270. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  271. return res["StatusText"]
  272. }
  273. // PACU ChanSpy
  274. func ChanSpy(src, dst string, whisper, bargein bool) {
  275. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  276. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  277. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  278. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  279. /*
  280. if whisper {
  281. data = fmt.Sprintf("%s,w", data)
  282. }
  283. if bargein {
  284. data = fmt.Sprintf("%s,B", data)
  285. }
  286. */
  287. action := map[string]string{
  288. "Action": "Originate",
  289. "Channel": channel, // 不存在的通话
  290. "Application": "ChanSpy",
  291. "Data": data, // 存在的通话
  292. "CallerID": dst,
  293. "Async": "true",
  294. }
  295. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  296. _, _, err := AminInstance.Send(action)
  297. if err != nil {
  298. lfshook.NewLogger().Errorf("%+v", err)
  299. }
  300. }
  301. // Redirect 转接
  302. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  303. //callerID := "redirect"
  304. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  305. if !utils.IsChannel(channel) {
  306. //callerID = channel
  307. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  308. return err
  309. }
  310. }
  311. action := map[string]string{
  312. "Action": "Redirect",
  313. "Channel": channel,
  314. "Exten": dst,
  315. "Context": dialrule,
  316. "CallerID": callerID,
  317. "Priority": "1",
  318. "async": "true",
  319. }
  320. lfshook.NewLogger().Infof("redirect %+v", action)
  321. res, _, err := AminInstance.Send(action)
  322. if err != nil {
  323. lfshook.NewLogger().Error(err)
  324. }
  325. lfshook.NewLogger().Info(res)
  326. return err
  327. }
  328. // Redirect 转接
  329. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  330. //callerID := "redirect"
  331. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  332. if !utils.IsChannel(channel) {
  333. callerID = channel
  334. if channel, err = GetChannelByExten(channel); err != nil {
  335. return err
  336. }
  337. }
  338. action := map[string]string{
  339. "Action": "Redirect",
  340. "Channel": channel,
  341. "Exten": dst,
  342. "Context": dialrule,
  343. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  344. "Priority": "1",
  345. "async": "true",
  346. }
  347. lfshook.NewLogger().Infof("redirect %+v", action)
  348. res, _, err := AminInstance.Send(action)
  349. if err != nil {
  350. lfshook.NewLogger().Error(err)
  351. }
  352. lfshook.NewLogger().Info(res)
  353. return err
  354. }
  355. func SetPadTimer() {
  356. toRunpriority, _ := strconv.Atoi(priority.GetPriorityByKey(priority.InterruptedPad)) // 获取之前打断的报警优先级
  357. lfshook.NewLogger().Infof("==SetPadTimer====runing:%d==toRun:%d=", priority.RunningTypePriority, toRunpriority)
  358. if priority.RunningTypePriority < toRunpriority || priority.RunningTypePriority == 0 {
  359. res, err := QueueStatus("0300", "") // check OCC queue , if empty OCC-PAD start
  360. if err != nil {
  361. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  362. return
  363. }
  364. if res.Calls != "0" {
  365. lfshook.NewLogger().Infof("==SetPadTimer==QueueTimer===1====")
  366. active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  367. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  368. if err != nil {
  369. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  370. return
  371. }
  372. if res.Calls == "0" { // OCC queue empty
  373. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  374. if err != nil {
  375. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  376. return
  377. }
  378. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  379. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  380. })
  381. for _, caller := range resCaller.Entrys {
  382. priority.ICPAnswer = 0
  383. lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=")
  384. lfshook.NewLogger().Infof("====Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  385. //order by pos
  386. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  387. time.Sleep(time.Microsecond * 500) //200 ms delay
  388. }
  389. }
  390. })
  391. }
  392. }
  393. }
  394. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  395. action := map[string]string{
  396. "Action": "ConfbridgeKick",
  397. "Conference": confnum,
  398. "Channel": channel,
  399. }
  400. res, _, err = AminInstance.Send(action)
  401. if err != nil {
  402. return nil, err
  403. }
  404. if res["Response"] != "Success" {
  405. return nil, errors.New(res["Message"])
  406. }
  407. return res, nil
  408. }