call.go 8.6 KB

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