package action import ( "fmt" "pbx-api-gin/internal/app/stc/priority" "pbx-api-gin/pkg/lfshook" "pbx-api-gin/pkg/utils" "strings" ) var Pads = []string{"2413", "2414", "2415", "2421", "2422", "2423", "2424", "2425", "2431", "2432", "2433", "2434", "2435", "2441", "2442", "2443", "2444", "2445", "2451", "2452", "2453", "2454", "2455", "2461", "2462", "2463", "2464", "2465", "2471", "2472", "2473", "2474", "2475", "2481", "2482", "2483", "2484", "2485", "2411", "2412"} var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"} // Hangup 挂断指定分机或通道 func Hangup(channel string) { lfshook.NewLogger().Infof("hangup extensions/channel %s", channel) if !utils.IsChannel(channel) { channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel) } action := map[string]string{ "Action": "hangup", "Channel": channel, } lfshook.NewLogger().Infof("hangup action %+v", action) if _, _, err := AminInstance.Send(action); err != nil { lfshook.NewLogger().Errorf("Hangup %+v", err) } } // Hangup 挂断所有分机,除指定分机和PAD func HangupAllExcept(caller string) { //all PACU for _, ret := range Pacus { Hangup(ret) } switch caller { case "2311": Hangup("1411") //IO1 Hangup("1481") //IO8 //Hangup("2311") //ICP1 Hangup("2381") //ICP8 case "2381": Hangup("1411") //IO1 Hangup("1481") //IO8 Hangup("2311") //ICP1 //Hangup("2381") //ICP8 case "1411": //Hangup("1411") //IO1 Hangup("1481") //IO8 Hangup("2311") //ICP1 Hangup("2381") //ICP8 case "1481": Hangup("1411") //IO1 //Hangup("1481") //IO8 Hangup("2311") //ICP1 Hangup("2381") //ICP8 case "": Hangup("1411") //IO1 Hangup("1481") //IO8 Hangup("2311") //ICP1 Hangup("2381") //ICP8 } } // Hangup all ICP func HangupRunningTask(toRunTask string) { lfshook.NewLogger().Infof("===HangupRunningTask=task=%+v", toRunTask) if priority.RunningPATaskChan != "" { switch priority.RunningType { case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running, chans, err := CoreShowChannels() if err != nil { lfshook.NewLogger().Errorf("CoreShowChannels %+v", err) } //1. Redirect the connected PAD to 0300 for _, ret := range chans { if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" { err := RedirectInQueue(ret.Channel, "0300", "default", ret.Channel) if err != nil { lfshook.NewLogger().Errorf("Redirect %+v", err) } } } //2. hangup task channel (ICP + PACU) HangupAllLocalChan() priority.CleanPriorityTag() case "PAD-OCC": // Interrupt PAD-OCC task running, if toRunTask == "C2C" { break } else { //1. Hangup connected PAD //Hangup(priority.RunningPATaskChan) //2. Redirect all the other pads in 0301 to 0300 resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries if err != nil { lfshook.NewLogger().Infof("===QueueStatus==%+v", err) } for _, caller := range resCaller.Entrys { go RedirectInQueue(caller.CallerIDNum, "0300", "default", caller.CallerIDNum) // redirect All PAD redirect to ICP queue } //3. Hangup OI & ICP HangupIO() HangupAllLocalChan() priority.CleanPriorityTag() } case "C2C": // Interrupt C2C task running, if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" { HangupICP() priority.CleanPriorityTag() } default: Hangup(priority.RunningPATaskChan) priority.CleanPriorityTag() } } } // Hangup all ICP func HangupICP() { Hangup("2311") //ICP1 Hangup("2381") //ICP8 } // Hangup all IO func HangupIO() { Hangup("1411") //IO1 Hangup("1481") //IO8 } // Hangup all PACU func HangupAllPACU() { //all PACU for _, ret := range Pacus { Hangup(ret) } } func HangupAllLocalChan() { chans, err := CoreShowChannels() if err != nil { lfshook.NewLogger().Errorf("CoreShowChannels %+v", err) } for _, ret := range chans { if strings.Contains(ret.Channel, "Local") { Hangup(ret.Channel) } } } // Hangup all PACU func HangupAllPAD() { //all PAD for _, ret := range Pads { Hangup(ret) } } // Hangup all calls func HangupAll() { utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all") } // Dial 拨打号码 func Dial(src, dst, dialrule, callerID, callerName string, callType string) { chanel := fmt.Sprintf("%s/%s@default", "Local", src) action := map[string]string{ "Action": "Originate", "Channel": chanel, "Exten": dst, "Context": dialrule, "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID), "Priority": "1", "Variable": fmt.Sprintf("CAB=%s", callType), "async": "true", } lfshook.NewLogger().Infof("dial action %+v", action) res, _, err := AminInstance.Send(action) if err != nil { lfshook.NewLogger().Errorf("%+v", err) } lfshook.NewLogger().Info(res) } // 获取分机状态 func ExtenStatus(exten string) (Status string) { action := map[string]string{ "Action": "ExtensionState", "Exten": exten, "Context": "default", } res, _, err := AminInstance.Send(action) if err != nil { lfshook.NewLogger().Errorf("%+v", err) return "" } //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res) return res["StatusText"] } // PACU ChanSpy func ChanSpy(src, dst string, whisper, bargein bool) { lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst) //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst) channel := fmt.Sprintf("Local/%s@aio-rule", dst) data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src) /* if whisper { data = fmt.Sprintf("%s,w", data) } if bargein { data = fmt.Sprintf("%s,B", data) } */ action := map[string]string{ "Action": "Originate", "Channel": channel, // 不存在的通话 "Application": "ChanSpy", "Data": data, // 存在的通话 "CallerID": dst, "Async": "true", } lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action) _, _, err := AminInstance.Send(action) if err != nil { lfshook.NewLogger().Errorf("%+v", err) } } // Redirect 转接 func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) { //callerID := "redirect" lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst) if !utils.IsChannel(channel) { //callerID = channel if channel, err = GetChannelByExtenNotBridged(channel); err != nil { return err } } action := map[string]string{ "Action": "Redirect", "Channel": channel, "Exten": dst, "Context": dialrule, "CallerID": callerID, "Priority": "1", "async": "true", } lfshook.NewLogger().Infof("redirect %+v", action) res, _, err := AminInstance.Send(action) if err != nil { lfshook.NewLogger().Error(err) } lfshook.NewLogger().Info(res) return err }