call.go 6.8 KB

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