call.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. case "PAD-OCC":
  26. HangupRunningTask("InActiveHangup")
  27. }
  28. }
  29. }
  30. // check EMG resume
  31. func CheckEmgResume() {
  32. /*
  33. time.AfterFunc(2*time.Second, func() {
  34. if priority.CheckPriority("EMG") || priority.RunningType == "C2C" {
  35. PlaybackPacu(strconv.Quote(priority.ResumeEmgPara.FileName), priority.ResumeEmgPara.Count, priority.ResumeEmgPara.Delay, priority.ResumeEmgPara.BroadcastType)
  36. }
  37. })*/
  38. }
  39. // Hangup 挂断指定分机或通道
  40. func Hangup(channel string) {
  41. lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
  42. if !utils.IsChannel(channel) {
  43. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  44. }
  45. action := map[string]string{
  46. "Action": "hangup",
  47. "Channel": channel,
  48. }
  49. lfshook.NewLogger().Infof("hangup action %+v", action)
  50. if _, _, err := AminInstance.Send(action); err != nil {
  51. lfshook.NewLogger().Errorf("Hangup %+v", err)
  52. }
  53. }
  54. // Hangup 挂断所有分机,除指定分机和PAD
  55. func HangupAllExcept(caller string) {
  56. //all PACU
  57. for _, ret := range Pacus {
  58. Hangup(ret)
  59. }
  60. switch caller {
  61. case "2311":
  62. Hangup("1411") //IO1
  63. Hangup("1481") //IO8
  64. //Hangup("2311") //ICP1
  65. Hangup("2381") //ICP8
  66. case "2381":
  67. Hangup("1411") //IO1
  68. Hangup("1481") //IO8
  69. Hangup("2311") //ICP1
  70. //Hangup("2381") //ICP8
  71. case "1411":
  72. //Hangup("1411") //IO1
  73. Hangup("1481") //IO8
  74. Hangup("2311") //ICP1
  75. Hangup("2381") //ICP8
  76. case "1481":
  77. Hangup("1411") //IO1
  78. //Hangup("1481") //IO8
  79. Hangup("2311") //ICP1
  80. Hangup("2381") //ICP8
  81. case "":
  82. Hangup("1411") //IO1
  83. Hangup("1481") //IO8
  84. Hangup("2311") //ICP1
  85. Hangup("2381") //ICP8
  86. }
  87. }
  88. // Hangup all ICP
  89. func HangupRunningTask(toRunTask string) {
  90. lfshook.NewLogger().Infof("===HangupRunningTask=toRuntask=%s==== RunningTask:%s===", toRunTask, priority.RunningType)
  91. //same type return
  92. if toRunTask == priority.RunningType {
  93. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  94. return
  95. }
  96. }
  97. if priority.RunningPATaskChan != "" {
  98. switch priority.RunningType {
  99. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  100. //lfshook.NewLogger().Infof("===HangupRunningTask=PAD=======%s==== ", priority.RunningType)
  101. /* chans, err := CoreShowChannels()
  102. if err != nil {
  103. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  104. }
  105. lfshook.NewLogger().Infof("===HangupRunningTask=PAD1=======%+v==== ", chans)
  106. //1. Redirect the connected PAD to 0300
  107. for _, ret := range chans {
  108. exten := strings.Split(strings.Split(ret.Channel, "/")[1], "-")[0]
  109. if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsPAIU(exten) {
  110. //err := RedirectInQueue(ret.Channel, "0300", "default", "PAD")
  111. err := Redirect(ret.Channel, "0300", "default", exten, "PAD")
  112. if err != nil {
  113. lfshook.NewLogger().Errorf("Redirect %+v", err)
  114. }
  115. }
  116. }
  117. */
  118. //2. hangup task channel (ICP + PACU)
  119. HangupAllLocalChan()
  120. //HangupICP()
  121. priority.CleanPriorityTag()
  122. case "PAD-OCC": // Interrupt PAD-OCC task running,
  123. if toRunTask == "C2C" {
  124. break
  125. } else {
  126. //1. 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. //2. Hangup connected PAD
  136. Hangup(priority.RunningPATaskChan)
  137. //3. Hangup OI & ICP
  138. HangupIO()
  139. HangupAllLocalChan()
  140. priority.CleanPriorityTag()
  141. }
  142. case "C2C": // Interrupt C2C task running,
  143. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  144. HangupICP()
  145. priority.CleanPriorityTag()
  146. }
  147. default:
  148. Hangup(priority.RunningPATaskChan)
  149. priority.CleanPriorityTag()
  150. }
  151. }
  152. }
  153. // Hangup all ICP
  154. func HangupICP() {
  155. Hangup("2311") //ICP1
  156. Hangup("2381") //ICP8
  157. }
  158. // Hangup all IO
  159. func HangupIO() {
  160. Hangup("1411") //IO1
  161. Hangup("1481") //IO8
  162. }
  163. // Hangup all PACU
  164. func HangupAllPACU() {
  165. //all PACU
  166. for _, ret := range Pacus {
  167. Hangup(ret)
  168. }
  169. }
  170. func HangupAllLocalChan() {
  171. chans, err := CoreShowChannels()
  172. if err != nil {
  173. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  174. }
  175. for _, ret := range chans {
  176. if strings.Contains(ret.Channel, "Local") {
  177. Hangup(ret.Channel)
  178. }
  179. }
  180. }
  181. // Hangup all PACU
  182. func HangupAllPAD() {
  183. //all PAD
  184. for _, ret := range Pads {
  185. Hangup(ret)
  186. }
  187. }
  188. // Hangup all calls
  189. func HangupAll() {
  190. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  191. }
  192. // Dial 拨打号码
  193. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  194. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  195. action := map[string]string{
  196. "Action": "Originate",
  197. "Channel": chanel,
  198. "Exten": dst,
  199. "Context": dialrule,
  200. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  201. "Priority": "1",
  202. "Variable": fmt.Sprintf("CAB=%s", callType),
  203. "async": "true",
  204. }
  205. lfshook.NewLogger().Infof("dial action %+v", action)
  206. res, _, err := AminInstance.Send(action)
  207. if err != nil {
  208. lfshook.NewLogger().Errorf("%+v", err)
  209. }
  210. lfshook.NewLogger().Info(res)
  211. }
  212. // 获取分机状态
  213. func ExtenStatus(exten string) (Status string) {
  214. action := map[string]string{
  215. "Action": "ExtensionState",
  216. "Exten": exten,
  217. "Context": "default",
  218. }
  219. res, _, err := AminInstance.Send(action)
  220. if err != nil {
  221. lfshook.NewLogger().Errorf("%+v", err)
  222. return ""
  223. }
  224. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  225. return res["StatusText"]
  226. }
  227. // PACU ChanSpy
  228. func ChanSpy(src, dst string, whisper, bargein bool) {
  229. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  230. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  231. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  232. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  233. /*
  234. if whisper {
  235. data = fmt.Sprintf("%s,w", data)
  236. }
  237. if bargein {
  238. data = fmt.Sprintf("%s,B", data)
  239. }
  240. */
  241. action := map[string]string{
  242. "Action": "Originate",
  243. "Channel": channel, // 不存在的通话
  244. "Application": "ChanSpy",
  245. "Data": data, // 存在的通话
  246. "CallerID": dst,
  247. "Async": "true",
  248. }
  249. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  250. _, _, err := AminInstance.Send(action)
  251. if err != nil {
  252. lfshook.NewLogger().Errorf("%+v", err)
  253. }
  254. }
  255. // Redirect 转接
  256. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  257. //callerID := "redirect"
  258. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  259. if !utils.IsChannel(channel) {
  260. //callerID = channel
  261. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  262. return err
  263. }
  264. }
  265. action := map[string]string{
  266. "Action": "Redirect",
  267. "Channel": channel,
  268. "Exten": dst,
  269. "Context": dialrule,
  270. "CallerID": callerID,
  271. "Priority": "1",
  272. "async": "true",
  273. }
  274. lfshook.NewLogger().Infof("redirect %+v", action)
  275. res, _, err := AminInstance.Send(action)
  276. if err != nil {
  277. lfshook.NewLogger().Error(err)
  278. }
  279. lfshook.NewLogger().Info(res)
  280. return err
  281. }
  282. // Redirect 转接
  283. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  284. //callerID := "redirect"
  285. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  286. if !utils.IsChannel(channel) {
  287. callerID = channel
  288. if channel, err = GetChannelByExten(channel); err != nil {
  289. return err
  290. }
  291. }
  292. action := map[string]string{
  293. "Action": "Redirect",
  294. "Channel": channel,
  295. "Exten": dst,
  296. "Context": dialrule,
  297. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  298. "Priority": "1",
  299. "async": "true",
  300. }
  301. lfshook.NewLogger().Infof("redirect %+v", action)
  302. res, _, err := AminInstance.Send(action)
  303. if err != nil {
  304. lfshook.NewLogger().Error(err)
  305. }
  306. lfshook.NewLogger().Info(res)
  307. return err
  308. }