package action import ( "fmt" "pbx-api-gin/internal/app/stc/active" "pbx-api-gin/internal/app/stc/priority" "pbx-api-gin/pkg/lfshook" "pbx-api-gin/pkg/utils" "sort" "strconv" "strings" "time" ) 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"} // Function triggered before no cab occupied signal interrupt func InActiveHangup() { if active.ActivedCab == "" { switch priority.RunningType { case "PA": HangupRunningTask("InActiveHangup") case "CPA": HangupRunningTask("InActiveHangup") case "VOL": HangupRunningTask("InActiveHangup") case "PAD-OCC": HangupRunningTask("InActiveHangup") } } } // check EMG resume func CheckEmgResume() { /* time.AfterFunc(2*time.Second, func() { if priority.CheckPriority("EMG") || priority.RunningType == "C2C" { PlaybackPacu(strconv.Quote(priority.ResumeEmgPara.FileName), priority.ResumeEmgPara.Count, priority.ResumeEmgPara.Delay, priority.ResumeEmgPara.BroadcastType) } })*/ } // 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=toRuntask=%s==== RunningTask:%s===", toRunTask, priority.RunningType) //same type return if toRunTask == priority.RunningType { if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" { lfshook.NewLogger().Infof("===HangupRunningTask=ret==== ") return } } if toRunTask == "AlarmHoldResetAll" { Hangup(priority.RunningPATaskChan) priority.CleanPriorityTag() HangupAllLocalChan() return } if priority.RunningPATaskChan != "" { if priority.RunningType == "C2C" { // Interrupt C2C task running, if toRunTask == "PA" || toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" { HangupICP() //priority.CleanPriorityTag() } } switch priority.RunningType { case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running, priority.InterruptedPad = priority.RunningType //just in case if toRunTask == "C2C" { //deal in DTMF event lfshook.NewLogger().Infof("===HangupRunningTask==C2C=return==== ") return } lfshook.NewLogger().Infof("===HangupRunningTask==hangup===PAD-ICP==%s==== ", priority.RunningType) chans, err := CoreShowChannels() if err != nil { lfshook.NewLogger().Errorf("CoreShowChannels %+v", err) } //1. Redirect the connected PAD to 0300,hangup the other pad channel for _, ret := range chans { if utils.IsPAIU(ret.CallerIDNum) { if ret.ConnectedLineNum == "" { //redirect pad chanspy channel //lfshook.NewLogger().Infof("===Redirect=Chan===%+v==== ", ret.Channel) err := Redirect(ret.Channel, "0300", "default", "", "PAD") if err != nil { lfshook.NewLogger().Errorf("Redirect %+v", err) } number := strings.Split(strings.Split(ret.Channel, "-")[0], "/")[1] active.NotifyPaiu(number, "hold") HangupAllLocalChan() } else if utils.IsPAIU(ret.CallerIDNum) && ret.ChannelStateDesc == "Up" && utils.IsICP(ret.ConnectedLineNum) { lfshook.NewLogger().Infof("===Hangup=Chan===%+v==== ", ret.Channel) Hangup(ret.Channel) } } } //2. hangup task channel (ICP + PACU) lfshook.NewLogger().Infof("===HangupRunningTask=2. hangup task channel === ") HangupAllLocalChan() //HangupICP() priority.CleanPriorityTag() case "PAD-OCC": // Interrupt PAD-OCC task running, if toRunTask == "C2C" { break } else { priority.InterruptedPad = priority.RunningType //1. 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 time.Sleep(time.Microsecond * 200) } //2. Hangup connected PAD lfshook.NewLogger().Infof("===HangupRunningTask=Hangup connected PAD=== ") Hangup(priority.RunningPATaskChan) //3. Hangup OI & ICP HangupIO() HangupAllLocalChan() priority.CleanPriorityTag() } default: lfshook.NewLogger().Infof("===HangupRunningTask=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 } // Redirect 转接 func Redirect(channel, dst, dialrule, callerID, callerName 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 = GetChannelByExten(channel); err != nil { return err } } action := map[string]string{ "Action": "Redirect", "Channel": channel, "Exten": dst, "Context": dialrule, "CallerID": fmt.Sprintf("%s<%s>", callerName, 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 } func SetPadTimer() { toRunpriority, _ := strconv.Atoi(priority.GetPriorityByKey(priority.InterruptedPad)) // 获取之前打断的报警优先级 lfshook.NewLogger().Infof("==SetPadTimer====runing:%d==toRun:%d=", priority.RunningTypePriority, toRunpriority) if priority.RunningTypePriority < toRunpriority || priority.RunningTypePriority == 0 { res, err := QueueStatus("0300", "") // check OCC queue , if empty OCC-PAD start if err != nil { lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err) return } if res.Calls != "0" { lfshook.NewLogger().Infof("==SetPadTimer==QueueTimer===1====") active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout res, err := QueueStatus("0301", "") // check OCC queue , if empty OCC-PAD start if err != nil { lfshook.NewLogger().Infof("===OCC-QueueStatus==%+v", err) return } if res.Calls == "0" { // OCC queue empty resCaller, err := QueueStatus("0300", "") // check ICP queue, get entries if err != nil { lfshook.NewLogger().Infof("==ICP=QueueStatus==%+v", err) return } sort.Slice(resCaller.Entrys, func(i, j int) bool { return resCaller.Entrys[i].Position < resCaller.Entrys[j].Position }) for _, caller := range resCaller.Entrys { priority.ICPAnswer = 0 lfshook.NewLogger().Infof("====SetPadTimer==QueueTimer==2=") lfshook.NewLogger().Infof("====Redirect to 0301 entry:%s=Pos:%s==", caller.CallerIDNum, caller.Position) //order by pos RedirectInQueue(caller.CallerIDNum, "0301", "queues-occ", caller.CallerIDNum) // redirect All ICP-PAD redirect to OCC queue time.Sleep(time.Microsecond * 500) //200 ms delay } } }) } } }