call.go 6.6 KB

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