call.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444",
  14. "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483",
  15. "2484", "2485", "2411", "2412"}
  16. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  17. // Function triggered before no cab occupied signal interrupt
  18. func InActiveHangup() {
  19. if active.ActivedCab == "" {
  20. switch priority.RunningType {
  21. case "PA":
  22. HangupRunningTask("InActiveHangup")
  23. case "CPA":
  24. HangupRunningTask("InActiveHangup")
  25. case "VOL":
  26. HangupRunningTask("InActiveHangup")
  27. case "PAD-OCC":
  28. HangupRunningTask("InActiveHangup")
  29. }
  30. }
  31. }
  32. // check EMG resume
  33. func CheckEmgResume() {
  34. /*
  35. time.AfterFunc(2*time.Second, func() {
  36. if priority.CheckPriority("EMG") || priority.RunningType == "C2C" {
  37. PlaybackPacu(strconv.Quote(priority.ResumeEmgPara.FileName), priority.ResumeEmgPara.Count, priority.ResumeEmgPara.Delay, priority.ResumeEmgPara.BroadcastType)
  38. }
  39. })*/
  40. }
  41. // Hangup 挂断指定分机或通道
  42. func Hangup(channel string) {
  43. lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
  44. if !utils.IsChannel(channel) {
  45. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  46. }
  47. action := map[string]string{
  48. "Action": "hangup",
  49. "Channel": channel,
  50. }
  51. lfshook.NewLogger().Infof("hangup action %+v", action)
  52. if _, _, err := AminInstance.Send(action); err != nil {
  53. lfshook.NewLogger().Errorf("Hangup %+v", err)
  54. }
  55. }
  56. // Hangup 挂断所有分机,除指定分机和PAD
  57. func HangupAllExcept(caller string) {
  58. //all PACU
  59. for _, ret := range Pacus {
  60. Hangup(ret)
  61. }
  62. switch caller {
  63. case "2311":
  64. Hangup("1411") //IO1
  65. Hangup("1481") //IO8
  66. //Hangup("2311") //ICP1
  67. Hangup("2381") //ICP8
  68. case "2381":
  69. Hangup("1411") //IO1
  70. Hangup("1481") //IO8
  71. Hangup("2311") //ICP1
  72. //Hangup("2381") //ICP8
  73. case "1411":
  74. //Hangup("1411") //IO1
  75. Hangup("1481") //IO8
  76. Hangup("2311") //ICP1
  77. Hangup("2381") //ICP8
  78. case "1481":
  79. Hangup("1411") //IO1
  80. //Hangup("1481") //IO8
  81. Hangup("2311") //ICP1
  82. Hangup("2381") //ICP8
  83. case "":
  84. Hangup("1411") //IO1
  85. Hangup("1481") //IO8
  86. Hangup("2311") //ICP1
  87. Hangup("2381") //ICP8
  88. }
  89. }
  90. // Hangup all ICP
  91. func HangupRunningTask(toRunTask string) {
  92. lfshook.NewLogger().Infof("===HangupRunningTask=toRuntask=%s==== RunningTask:%s===", toRunTask, priority.RunningType)
  93. //same type return
  94. if toRunTask == priority.RunningType {
  95. if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  96. lfshook.NewLogger().Infof("===HangupRunningTask=ret==== ")
  97. return
  98. }
  99. }
  100. if toRunTask == "AlarmHoldResetAll" {
  101. Hangup(priority.RunningPATaskChan)
  102. priority.CleanPriorityTag()
  103. HangupAllLocalChan()
  104. return
  105. }
  106. if priority.RunningPATaskChan != "" {
  107. if priority.C2CRuning == 1 { // Interrupt C2C task running,
  108. if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
  109. HangupICP()
  110. //priority.CleanPriorityTag()
  111. }
  112. }
  113. switch priority.RunningType {
  114. case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
  115. //just in case
  116. if toRunTask == "C2C" {
  117. return
  118. }
  119. lfshook.NewLogger().Infof("===HangupRunningTask==hangup===PAD-ICP==%s==== ", priority.RunningType)
  120. chans, err := CoreShowChannels()
  121. if err != nil {
  122. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  123. }
  124. //1. Redirect the connected PAD to 0300,hangup the other pad channel
  125. for _, ret := range chans {
  126. if utils.IsPAIU(ret.CallerIDNum) {
  127. if ret.ConnectedLineNum == "<unknown>" { //redirect pad chanspy channel
  128. //lfshook.NewLogger().Infof("===Redirect=Chan===%+v==== ", ret.Channel)
  129. err := Redirect(ret.Channel, "0300", "default", "", "PAD")
  130. if err != nil {
  131. lfshook.NewLogger().Errorf("Redirect %+v", err)
  132. }
  133. number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1]
  134. active.NotifyPaiu(number, "hold")
  135. HangupAllLocalChan()
  136. } else if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) {
  137. lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel)
  138. Hangup(ret.Channel)
  139. }
  140. }
  141. }
  142. //2. hangup task channel (ICP + PACU)
  143. lfshook.NewLogger().Infof("===HangupRunningTask=2. hangup task channel === ")
  144. HangupAllLocalChan()
  145. //HangupICP()
  146. priority.CleanPriorityTag()
  147. case "PAD-OCC": // Interrupt PAD-OCC task running,
  148. if toRunTask == "C2C" {
  149. break
  150. } else {
  151. //1. Redirect all the other pads in 0301 to 0300
  152. resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
  153. if err != nil {
  154. lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
  155. }
  156. for _, caller := range resCaller.Entrys {
  157. go RedirectInQueue(caller.CallerIDNum, "0300", "default", caller.CallerIDNum) // redirect All PAD redirect to ICP queue
  158. time.Sleep(time.Microsecond * 200)
  159. }
  160. //2. Hangup connected PAD
  161. lfshook.NewLogger().Infof("===HangupRunningTask=Hangup connected PAD=== ")
  162. Hangup(priority.RunningPATaskChan)
  163. //3. Hangup OI & ICP
  164. HangupIO()
  165. HangupAllLocalChan()
  166. priority.CleanPriorityTag()
  167. }
  168. default:
  169. lfshook.NewLogger().Infof("===HangupRunningTask=default=== ")
  170. Hangup(priority.RunningPATaskChan)
  171. priority.CleanPriorityTag()
  172. }
  173. }
  174. }
  175. // Hangup all ICP
  176. func HangupICP() {
  177. Hangup("2311") //ICP1
  178. Hangup("2381") //ICP8
  179. }
  180. // Hangup all IO
  181. func HangupIO() {
  182. Hangup("1411") //IO1
  183. Hangup("1481") //IO8
  184. }
  185. // Hangup all PACU
  186. func HangupAllPACU() {
  187. //all PACU
  188. for _, ret := range Pacus {
  189. Hangup(ret)
  190. }
  191. }
  192. func HangupAllLocalChan() {
  193. chans, err := CoreShowChannels()
  194. if err != nil {
  195. lfshook.NewLogger().Errorf("CoreShowChannels %+v", err)
  196. }
  197. for _, ret := range chans {
  198. if strings.Contains(ret.Channel, "Local") {
  199. Hangup(ret.Channel)
  200. }
  201. }
  202. }
  203. // Hangup all PACU
  204. func HangupAllPAD() {
  205. //all PAD
  206. for _, ret := range Pads {
  207. Hangup(ret)
  208. }
  209. }
  210. // Hangup all calls
  211. func HangupAll() {
  212. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  213. }
  214. // Dial 拨打号码
  215. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  216. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  217. action := map[string]string{
  218. "Action": "Originate",
  219. "Channel": chanel,
  220. "Exten": dst,
  221. "Context": dialrule,
  222. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  223. "Priority": "1",
  224. "Variable": fmt.Sprintf("CAB=%s", callType),
  225. "async": "true",
  226. }
  227. lfshook.NewLogger().Infof("dial action %+v", action)
  228. res, _, err := AminInstance.Send(action)
  229. if err != nil {
  230. lfshook.NewLogger().Errorf("%+v", err)
  231. }
  232. lfshook.NewLogger().Info(res)
  233. }
  234. // 获取分机状态
  235. func ExtenStatus(exten string) (Status string) {
  236. action := map[string]string{
  237. "Action": "ExtensionState",
  238. "Exten": exten,
  239. "Context": "default",
  240. }
  241. res, _, err := AminInstance.Send(action)
  242. if err != nil {
  243. lfshook.NewLogger().Errorf("%+v", err)
  244. return ""
  245. }
  246. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  247. return res["StatusText"]
  248. }
  249. // PACU ChanSpy
  250. func ChanSpy(src, dst string, whisper, bargein bool) {
  251. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  252. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  253. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  254. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  255. /*
  256. if whisper {
  257. data = fmt.Sprintf("%s,w", data)
  258. }
  259. if bargein {
  260. data = fmt.Sprintf("%s,B", data)
  261. }
  262. */
  263. action := map[string]string{
  264. "Action": "Originate",
  265. "Channel": channel, // 不存在的通话
  266. "Application": "ChanSpy",
  267. "Data": data, // 存在的通话
  268. "CallerID": dst,
  269. "Async": "true",
  270. }
  271. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  272. _, _, err := AminInstance.Send(action)
  273. if err != nil {
  274. lfshook.NewLogger().Errorf("%+v", err)
  275. }
  276. }
  277. // Redirect 转接
  278. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  279. //callerID := "redirect"
  280. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  281. if !utils.IsChannel(channel) {
  282. //callerID = channel
  283. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  284. return err
  285. }
  286. }
  287. action := map[string]string{
  288. "Action": "Redirect",
  289. "Channel": channel,
  290. "Exten": dst,
  291. "Context": dialrule,
  292. "CallerID": callerID,
  293. "Priority": "1",
  294. "async": "true",
  295. }
  296. lfshook.NewLogger().Infof("redirect %+v", action)
  297. res, _, err := AminInstance.Send(action)
  298. if err != nil {
  299. lfshook.NewLogger().Error(err)
  300. }
  301. lfshook.NewLogger().Info(res)
  302. return err
  303. }
  304. // Redirect 转接
  305. func Redirect(channel, dst, dialrule, callerID, callerName string) (err error) {
  306. //callerID := "redirect"
  307. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  308. if !utils.IsChannel(channel) {
  309. callerID = channel
  310. if channel, err = GetChannelByExten(channel); err != nil {
  311. return err
  312. }
  313. }
  314. action := map[string]string{
  315. "Action": "Redirect",
  316. "Channel": channel,
  317. "Exten": dst,
  318. "Context": dialrule,
  319. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  320. "Priority": "1",
  321. "async": "true",
  322. }
  323. lfshook.NewLogger().Infof("redirect %+v", action)
  324. res, _, err := AminInstance.Send(action)
  325. if err != nil {
  326. lfshook.NewLogger().Error(err)
  327. }
  328. lfshook.NewLogger().Info(res)
  329. return err
  330. }
  331. func SetPadTimer() {
  332. toRunpriority, _ := strconv.Atoi(priority.GetPriorityByKey("PAD-ICP"))
  333. lfshook.NewLogger().Infof("==SetPadTimer====runing:%d==toRun:%d=", priority.RunningTypePriority, toRunpriority)
  334. if priority.RunningTypePriority < toRunpriority || priority.RunningTypePriority == 0 {
  335. //if active.QueueTimer == nil {
  336. res, err := QueueStatus("0300", "") // check OCC queue , if empty OCC-PAD start
  337. if err != nil {
  338. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  339. return
  340. }
  341. if res.Calls != "0" {
  342. lfshook.NewLogger().Infof("==SetPadTimer==QueueTimer===1====")
  343. active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
  344. res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start
  345. if err != nil {
  346. lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err)
  347. return
  348. }
  349. if res.Calls == "0" { // OCC queue empty
  350. resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries
  351. if err != nil {
  352. lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err)
  353. return
  354. }
  355. sort.Slice(resCaller.Entrys, func(i, j int) bool {
  356. return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position
  357. })
  358. for _, caller := range resCaller.Entrys {
  359. priority.ICPAnswer = 0
  360. lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=")
  361. lfshook.NewLogger().Infof("====Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position)
  362. //order by pos
  363. RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue
  364. time.Sleep(time.Microsecond * 500) //200 ms delay
  365. }
  366. }
  367. })
  368. }
  369. }
  370. }