call.go 7.5 KB

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