call.go 8.7 KB

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