call.go 11 KB

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