call.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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=task=%+v", toRunTask)
  85. if priority.RunningPATaskChan != "" {
  86. switch priority.RunningType {
  87. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  88. chans, err := CoreShowChannels()
  89. if err != nil {
  90. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  91. }
  92. //1. Redirect the connected PAD to 0300
  93. for _, ret := range chans {
  94. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" {
  95. err := RedirectInQueue(ret.Channel, "0300", "default", ret.Channel)
  96. if err != nil {
  97. lfshook.NewLogger().Errorf("Redirect %+v", err)
  98. }
  99. }
  100. }
  101. //2. hangup task channel (ICP + PACU)
  102. HangupAllLocalChan()
  103. priority.CleanPriorityTag()
  104. case "PAD-OCC": // Interrupt PAD-OCC task running,
  105. if toRunTask == "C2C" {
  106. break
  107. } else {
  108. //1. Hangup connected PAD
  109. //Hangup(priority.RunningPATaskChan)
  110. //2. Redirect all the other pads in 0301 to 0300
  111. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  112. if err != nil {
  113. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  114. }
  115. for _, caller := range resCaller.Entrys {
  116. go RedirectInQueue(caller.CallerIDNum, "0300", "default", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  117. }
  118. //3. Hangup OI & ICP
  119. HangupIO()
  120. HangupAllLocalChan()
  121. priority.CleanPriorityTag()
  122. }
  123. case "C2C": // Interrupt C2C task running,
  124. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  125. HangupICP()
  126. priority.CleanPriorityTag()
  127. }
  128. default:
  129. Hangup(priority.RunningPATaskChan)
  130. priority.CleanPriorityTag()
  131. }
  132. }
  133. }
  134. // Hangup all ICP
  135. func HangupICP() {
  136. Hangup("2311") //ICP1
  137. Hangup("2381") //ICP8
  138. }
  139. // Hangup all IO
  140. func HangupIO() {
  141. Hangup("1411") //IO1
  142. Hangup("1481") //IO8
  143. }
  144. // Hangup all PACU
  145. func HangupAllPACU() {
  146. //all PACU
  147. for _, ret := range Pacus {
  148. Hangup(ret)
  149. }
  150. }
  151. func HangupAllLocalChan() {
  152. chans, err := CoreShowChannels()
  153. if err != nil {
  154. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  155. }
  156. for _, ret := range chans {
  157. if strings.Contains(ret.Channel, "Local") {
  158. Hangup(ret.Channel)
  159. }
  160. }
  161. }
  162. // Hangup all PACU
  163. func HangupAllPAD() {
  164. //all PAD
  165. for _, ret := range Pads {
  166. Hangup(ret)
  167. }
  168. }
  169. // Hangup all calls
  170. func HangupAll() {
  171. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  172. }
  173. // Dial 拨打号码
  174. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  175. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  176. action := map[string]string{
  177. "Action": "Originate",
  178. "Channel": chanel,
  179. "Exten": dst,
  180. "Context": dialrule,
  181. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  182. "Priority": "1",
  183. "Variable": fmt.Sprintf("CAB=%s", callType),
  184. "async": "true",
  185. }
  186. lfshook.NewLogger().Infof("dial action %+v", action)
  187. res, _, err := AminInstance.Send(action)
  188. if err != nil {
  189. lfshook.NewLogger().Errorf("%+v", err)
  190. }
  191. lfshook.NewLogger().Info(res)
  192. }
  193. // 获取分机状态
  194. func ExtenStatus(exten string) (Status string) {
  195. action := map[string]string{
  196. "Action": "ExtensionState",
  197. "Exten": exten,
  198. "Context": "default",
  199. }
  200. res, _, err := AminInstance.Send(action)
  201. if err != nil {
  202. lfshook.NewLogger().Errorf("%+v", err)
  203. return ""
  204. }
  205. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  206. return res["StatusText"]
  207. }
  208. // PACU ChanSpy
  209. func ChanSpy(src, dst string, whisper, bargein bool) {
  210. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  211. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  212. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  213. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  214. /*
  215. if whisper {
  216. data = fmt.Sprintf("%s,w", data)
  217. }
  218. if bargein {
  219. data = fmt.Sprintf("%s,B", data)
  220. }
  221. */
  222. action := map[string]string{
  223. "Action": "Originate",
  224. "Channel": channel, // 不存在的通话
  225. "Application": "ChanSpy",
  226. "Data": data, // 存在的通话
  227. "CallerID": dst,
  228. "Async": "true",
  229. }
  230. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  231. _, _, err := AminInstance.Send(action)
  232. if err != nil {
  233. lfshook.NewLogger().Errorf("%+v", err)
  234. }
  235. }
  236. // Redirect 转接
  237. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  238. //callerID := "redirect"
  239. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  240. if !utils.IsChannel(channel) {
  241. //callerID = channel
  242. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  243. return err
  244. }
  245. }
  246. action := map[string]string{
  247. "Action": "Redirect",
  248. "Channel": channel,
  249. "Exten": dst,
  250. "Context": dialrule,
  251. "CallerID": callerID,
  252. "Priority": "1",
  253. "async": "true",
  254. }
  255. lfshook.NewLogger().Infof("redirect %+v", action)
  256. res, _, err := AminInstance.Send(action)
  257. if err != nil {
  258. lfshook.NewLogger().Error(err)
  259. }
  260. lfshook.NewLogger().Info(res)
  261. return err
  262. }