call.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package action
  2. import (
  3. "fmt"
  4. "pbx-api-gin/internal/app/stc/priority"
  5. "pbx-api-gin/pkg/lfshook"
  6. "pbx-api-gin/pkg/utils"
  7. "strings"
  8. "time"
  9. )
  10. var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444",
  11. "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483",
  12. "2484", "2485", "2411", "2412"}
  13. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  14. // Function triggered before no cab occupied signal interrupt
  15. func InActiveHangup() {
  16. switch priority.RunningType {
  17. case "PA":
  18. HangupRunningTask("")
  19. case "CPA":
  20. HangupRunningTask("")
  21. case "VOL":
  22. HangupRunningTask("")
  23. }
  24. }
  25. // check EMG resume
  26. func CheckEmgResume() {
  27. time.AfterFunc(2*time.Second, func() {
  28. if priority.CheckPriority("EMG") || priority.RunningType == "C2C" {
  29. PlaybackPacu(priority.ResumeEmgPara.FileName, priority.ResumeEmgPara.Count, priority.ResumeEmgPara.Delay, priority.ResumeEmgPara.BroadcastType)
  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 ", toRunTask, priority.RunningType)
  85. //same type return
  86. if toRunTask == priority.RunningType {
  87. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  88. return
  89. }
  90. }
  91. if priority.RunningPATaskChan != "" {
  92. switch priority.RunningType {
  93. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  94. chans, err := CoreShowChannels()
  95. if err != nil {
  96. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  97. }
  98. //1. Redirect the connected PAD to 0300
  99. for _, ret := range chans {
  100. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" {
  101. err := RedirectInQueue(ret.Channel, "0300", "default", ret.Channel)
  102. if err != nil {
  103. lfshook.NewLogger().Errorf("Redirect %+v", err)
  104. }
  105. }
  106. }
  107. //2. hangup task channel (ICP + PACU)
  108. HangupAllLocalChan()
  109. priority.CleanPriorityTag()
  110. case "PAD-OCC": // Interrupt PAD-OCC task running,
  111. if toRunTask == "C2C" {
  112. break
  113. } else {
  114. //1. Hangup connected PAD
  115. //Hangup(priority.RunningPATaskChan)
  116. //2. Redirect all the other pads in 0301 to 0300
  117. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  118. if err != nil {
  119. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  120. }
  121. for _, caller := range resCaller.Entrys {
  122. go RedirectInQueue(caller.CallerIDNum, "0300", "default", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  123. }
  124. //3. Hangup OI & ICP
  125. HangupIO()
  126. HangupAllLocalChan()
  127. priority.CleanPriorityTag()
  128. }
  129. case "C2C": // Interrupt C2C task running,
  130. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  131. HangupICP()
  132. priority.CleanPriorityTag()
  133. }
  134. default:
  135. Hangup(priority.RunningPATaskChan)
  136. priority.CleanPriorityTag()
  137. }
  138. }
  139. }
  140. // Hangup all ICP
  141. func HangupICP() {
  142. Hangup("2311") //ICP1
  143. Hangup("2381") //ICP8
  144. }
  145. // Hangup all IO
  146. func HangupIO() {
  147. Hangup("1411") //IO1
  148. Hangup("1481") //IO8
  149. }
  150. // Hangup all PACU
  151. func HangupAllPACU() {
  152. //all PACU
  153. for _, ret := range Pacus {
  154. Hangup(ret)
  155. }
  156. }
  157. func HangupAllLocalChan() {
  158. chans, err := CoreShowChannels()
  159. if err != nil {
  160. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  161. }
  162. for _, ret := range chans {
  163. if strings.Contains(ret.Channel, "Local") {
  164. Hangup(ret.Channel)
  165. }
  166. }
  167. }
  168. // Hangup all PACU
  169. func HangupAllPAD() {
  170. //all PAD
  171. for _, ret := range Pads {
  172. Hangup(ret)
  173. }
  174. }
  175. // Hangup all calls
  176. func HangupAll() {
  177. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  178. }
  179. // Dial 拨打号码
  180. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  181. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  182. action := map[string]string{
  183. "Action": "Originate",
  184. "Channel": chanel,
  185. "Exten": dst,
  186. "Context": dialrule,
  187. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  188. "Priority": "1",
  189. "Variable": fmt.Sprintf("CAB=%s", callType),
  190. "async": "true",
  191. }
  192. lfshook.NewLogger().Infof("dial action %+v", action)
  193. res, _, err := AminInstance.Send(action)
  194. if err != nil {
  195. lfshook.NewLogger().Errorf("%+v", err)
  196. }
  197. lfshook.NewLogger().Info(res)
  198. }
  199. // 获取分机状态
  200. func ExtenStatus(exten string) (Status string) {
  201. action := map[string]string{
  202. "Action": "ExtensionState",
  203. "Exten": exten,
  204. "Context": "default",
  205. }
  206. res, _, err := AminInstance.Send(action)
  207. if err != nil {
  208. lfshook.NewLogger().Errorf("%+v", err)
  209. return ""
  210. }
  211. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  212. return res["StatusText"]
  213. }
  214. // PACU ChanSpy
  215. func ChanSpy(src, dst string, whisper, bargein bool) {
  216. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  217. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  218. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  219. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  220. /*
  221. if whisper {
  222. data = fmt.Sprintf("%s,w", data)
  223. }
  224. if bargein {
  225. data = fmt.Sprintf("%s,B", data)
  226. }
  227. */
  228. action := map[string]string{
  229. "Action": "Originate",
  230. "Channel": channel, // 不存在的通话
  231. "Application": "ChanSpy",
  232. "Data": data, // 存在的通话
  233. "CallerID": dst,
  234. "Async": "true",
  235. }
  236. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  237. _, _, err := AminInstance.Send(action)
  238. if err != nil {
  239. lfshook.NewLogger().Errorf("%+v", err)
  240. }
  241. }
  242. // Redirect 转接
  243. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  244. //callerID := "redirect"
  245. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  246. if !utils.IsChannel(channel) {
  247. //callerID = channel
  248. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  249. return err
  250. }
  251. }
  252. action := map[string]string{
  253. "Action": "Redirect",
  254. "Channel": channel,
  255. "Exten": dst,
  256. "Context": dialrule,
  257. "CallerID": callerID,
  258. "Priority": "1",
  259. "async": "true",
  260. }
  261. lfshook.NewLogger().Infof("redirect %+v", action)
  262. res, _, err := AminInstance.Send(action)
  263. if err != nil {
  264. lfshook.NewLogger().Error(err)
  265. }
  266. lfshook.NewLogger().Info(res)
  267. return err
  268. }