| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825 |
- package broadcast
- import (
- "bytes"
- "context"
- "encoding/binary"
- "fmt"
- "io"
- "net"
- "os/exec"
- "pbx-api-gin/internal/app/ami/action"
- "pbx-api-gin/internal/app/stc/active"
- msgdata "pbx-api-gin/internal/app/stc/data"
- "pbx-api-gin/internal/app/stc/priority"
- alstatus "pbx-api-gin/internal/app/stc/sendstatus"
- "pbx-api-gin/pkg/lfshook"
- "pbx-api-gin/pkg/utils"
- "strconv"
- "sync"
- "syscall"
- "time"
- )
- var tagLog = 0
- func HandleStcCmd(ctx context.Context, conn net.Conn) {
- for {
- select {
- case <-ctx.Done():
- return
- default:
- var buf bytes.Buffer
- tmp := make([]byte, 1024)
- if conn != nil {
- n, err := conn.Read(tmp)
- if err != nil {
- if err != io.EOF {
- conn.Close()
- }
- return
- }
- buf.Write(tmp[:n])
- }
- //lfshook.NewLogger().Logger.Infof("buf:%x==============================", buf.Bytes())
- for {
- packet, err := msgdata.ExtractPacket(&buf)
- if err != nil {
- utils.LoggerDebug.Printf("Parse error: %v, resetting buffer", err)
- buf.Reset() // 解析失败,清空避免污染
- break
- }
- if packet == nil {
- break // 当前无完整包,等待下次 ReadFrom
- }
- //处理 packet...
- go processPacket(packet)
- }
- }
- }
- }
- // get train info and pacu info from heartbeat
- func getInfofromSTC(info []byte) {
- //dataCount := info[0]
- //Get train info
- value := binary.BigEndian.Uint16(info[2:4]) //info[2] + info[3] 列车号数据
- //value := 3
- active.TrainNum = "TS" + strconv.Itoa(int(value))
- lfshook.NewLogger().Logger.Infof("TrainNum: %s", active.TrainNum)
- //Get ICP volume
- icpBit8 := info[9] & 0xF
- icpBit1 := (info[9] >> 4) & 0xF
- icpVol1 := 0
- icpVol8 := 0
- if icpBit1 == 0xe {
- icpVol1 = 100
- } else {
- icpVol1 = int(icpBit1) * 7
- }
- if icpBit8 == 0xe {
- icpVol8 = 100
- } else {
- icpVol8 = int(icpBit8) * 7
- }
- active.DeviceEndpoint.ICPInfo[0].Volume = fmt.Sprintf("%d", icpVol1)
- active.DeviceEndpoint.ICPInfo[0].ID = "1"
- active.DeviceEndpoint.ICPInfo[1].Volume = fmt.Sprintf("%d", icpVol8)
- active.DeviceEndpoint.ICPInfo[1].ID = "8"
- //Get Pacu info
- eidsStat := info[1] //PACU mute
- pacuStat := info[4] //PACU status
- pacuVolume := binary.BigEndian.Uint32(info[5:9]) //info[5]-info[8]
- //lfshook.NewLogger().Logger.Infof("=====pacuVolume====info[5:9]=======%x", info[5:9])
- //lfshook.NewLogger().Logger.Infof("=====pacuStatus====info[4]=======%x", info[4])
- for i := 0; i < 8; i++ {
- eidsBit := (eidsStat >> i) & 0x01 // 右移 i 位,再与 1 取最低位
- //fmt.Printf("eidsBit bit %d = %d\n", i, eidsBit) // bit 0 是 LSB(最低位,最右)
- //get 1 bit every time
- pacuStatBit := (pacuStat >> i) & 0x01
- //fmt.Printf("pacuStatBit bit %d = %d\n", i, pacuStatBit)
- //get 4 bit every time
- //var pacuVolBit byte
- pacuVolBit := (pacuVolume >> (i * 4)) & 0x0000000F
- //fmt.Printf("pacuVolBit bit %d = %x\n", i, pacuVolBit)
- active.DeviceEndpoint.PacuInfo[i].ID = strconv.Itoa(i + 1)
- if eidsBit == 1 {
- active.DeviceEndpoint.PacuInfo[i].Mute = true
- } else {
- active.DeviceEndpoint.PacuInfo[i].Mute = false
- }
- if pacuStatBit == 0 {
- active.DeviceEndpoint.PacuInfo[i].Status = "normal"
- } else {
- active.DeviceEndpoint.PacuInfo[i].Status = "abnormal"
- }
- if int(pacuVolBit) == 14 {
- active.DeviceEndpoint.PacuInfo[i].Volume = "100"
- } else {
- active.DeviceEndpoint.PacuInfo[i].Volume = strconv.Itoa(int(pacuVolBit) * 7)
- }
- //lfshook.NewLogger().Logger.Infof("PACU INFO===ID:%s===Mute:%+v===Stat:%s===Vol:%s", active.DeviceEndpoint.PacuInfo[i].ID, active.DeviceEndpoint.PacuInfo[i].Mute, active.DeviceEndpoint.PacuInfo[i].Status, active.DeviceEndpoint.PacuInfo[i].Volume)
- }
- }
- // 处理单个数据包(原 switch 逻辑迁移过来)
- func processPacket(packet []byte) {
- if len(packet) < 6 {
- utils.LoggerDebug.Printf("Get data wrong length from STC ! Data:%x", packet)
- return
- }
- //for recv data log debug
- if packet[5] != 0x03 && packet[5] != 0x0c && packet[5] != 0x01 {
- utils.LoggerDebug.Printf("Get data from STC:%x", packet)
- }
- //check if the cmd type is avtive
- if packet[5] == 0x03 { // ACTIVE
- Active([3]byte{packet[8], packet[9], packet[10]})
- return
- }
- //check if Master role
- if !active.Master {
- if tagLog == 0 {
- utils.LoggerDebug.Printf("Not Master Role , Ignore all data from STC !")
- tagLog = 1
- }
- return
- }
- tagLog = 0
- switch packet[5] {
- case 0x01: //heartbeat
- return
- case 0x02: // STN
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("STN")
- if priority.CheckPriority("STN") {
- runningTaskName := action.InterruptRunningTask("STN") //STN interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- StationAnn(packet)
- } else {
- alstatus.PaStatus("", "STN", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "STN" {
- priority.TaskCreating = ""
- }
- case 0x05: // SPC
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("SPC")
- if priority.CheckPriority("SPC") {
- runningTaskName := action.InterruptRunningTask("SPC") //SPC interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- SpecialAnn(packet)
- } else {
- alstatus.PaStatus("", "SPC", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "SPC" {
- priority.TaskCreating = ""
- }
- case 0x06: // EMG
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("EMG")
- if priority.CheckPriority("EMG") {
- runningTaskName := action.InterruptRunningTask("EMG") //EMG interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- EmgMsg(packet)
- } else {
- alstatus.PaStatus("", "EMG", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "EMG" {
- priority.TaskCreating = ""
- }
- case 0x07: // STOP
- AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
- case 0x08: // DCS
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("DCS")
- if priority.CheckPriority("DCS") {
- runningTaskName := action.InterruptRunningTask("DCS") //DCS interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- DcsAnn(packet)
- } else {
- alstatus.PaStatus("", "DCS", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "DCS" {
- priority.TaskCreating = ""
- }
- case 0x09: // SELF CHECK
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("CHK")
- if priority.CheckPriority("CHK") {
- runningTaskName := action.InterruptRunningTask("CHK") //CHK interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- SelfCheck(packet)
- } else {
- alstatus.PaStatus("", "CHK", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "CHK" {
- priority.TaskCreating = ""
- }
- case 0x0a: // Tone-test
- if active.ActivedCab != "" {
- //检查是否有任务正在创建
- action.WaitTaskCreate("VOL")
- if priority.CheckPriority("VOL") {
- runningTaskName := action.InterruptRunningTask("VOL") //VOL interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- ToneTest(packet)
- } else {
- alstatus.PaStatus("", "VOL", "refuse")
- }
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "VOL" {
- priority.TaskCreating = ""
- }
- case 0x0e: //TMS answer PAD
- handler := packet[8]
- key := suppressKey("exten", handler)
- //Drop other handler in 2 sec
- //PACUs---call---->ICP1
- //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
- if handler == 0x01 {
- if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
- utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD: within 2 seconds")
- return
- }
- time.AfterFunc(2*time.Second, func() {
- suppressedExts.Delete(key)
- utils.LoggerDebug.Printf("Suppression released .")
- })
- }
- //检查是否有任务正在创建
- action.WaitTaskCreate("PAD-TMS")
- if priority.CheckPriority("PAD-TMS") {
- //Before Answer PAD
- if packet[8] == 0x01 {
- runningTaskName := action.InterruptRunningTask("PAD-TMS") //PAD-TMS interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- }
- AlarmHandleTMS(packet)
- if active.QueueTimer != nil {
- if active.QueueTimer.Stop() {
- utils.LoggerDebug.Printf("Stop PAD timer true !")
- } else {
- utils.LoggerDebug.Printf("Stop PAD timer false !")
- }
- }
- } else {
- alstatus.PaStatus("", "PAD-TMS", "refuse")
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "PAD-TMS" {
- priority.TaskCreating = ""
- }
- case 0x0b: // reset all PAD
- AlarmHoldResetAll(packet[8]) // reset all pad
- case 0x0c: // Set PAD timeout
- //lfshook.NewLogger().Logger.Infof("==type 0x0c===Get data from STC ====%x", packet)
- PadTimeOutSetting(packet[8:]) // timeout setting
- getInfofromSTC(packet[8:])
- case 0x0d: // ICP answer PAD
- handler := packet[8]
- key := suppressKey("exten", handler)
- //Drop other handler in 2 sec
- //PACUs---call---->ICP1
- //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
- if handler == 0x01 {
- if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
- utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD: within 2 seconds")
- return
- }
- time.AfterFunc(2*time.Second, func() {
- suppressedExts.Delete(key)
- utils.LoggerDebug.Printf("Suppression released for key: %s", key)
- })
- }
- //检查是否有任务正在创建
- action.WaitTaskCreate("PAD-ICP")
- if priority.CheckPriority("PAD-ICP") {
- //Before Answer PAD
- if packet[8] == 0x01 {
- runningTaskName := action.InterruptRunningTask("PAD-ICP") //PAD-ICP interrupt other
- if runningTaskName != "" {
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- }
- if active.QueueTimer != nil {
- utils.LoggerDebug.Printf("PAD Timeout timer != nil !")
- if active.QueueTimer.Stop() {
- utils.LoggerDebug.Printf("Stop PAD timer true !")
- } else {
- utils.LoggerDebug.Printf("Stop PAD timer false !")
- }
- }
- AlarmHandleICP(packet)
- } else {
- alstatus.PaStatus("", "PAD-ICP", "refuse")
- }
- time.Sleep(3 * time.Second)
- if priority.TaskCreating == "PAD-ICP" {
- priority.TaskCreating = ""
- }
- //case 0xf1: //Set remote master
- //default:
- //fmt.Printf("Unknown command: %x\n", packet[5])
- }
- }
- func PadTimeOutSetting(data []byte) {
- Seconds := data[7]
- if Seconds != 0 {
- active.PADTimeout = int(Seconds)
- //lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", active.PADTimeout)
- }
- }
- // STN , 自动报站广播
- func StationAnn(data []byte) (err error) {
- specialVoice := int(data[8])
- delay := data[9]
- cycleCount := data[10]
- datalen := int(data[11])
- filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
- //set spc voice tag
- priority.SpecialVoice = specialVoice
- utils.LoggerDebug.Printf("Type:STN FileName:%x Count:%x SpecialVoice:%+v Interval:%+v", filename, cycleCount, specialVoice, delay)
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "STN")
- return nil
- }
- // 激活信号
- func Active(data [3]byte) {
- //var info model.Sysinfo
- Signal := int(data[0])
- master := int(data[1])
- TrainInfo := int(data[2])
- lfshook.NewLogger().Logger.Infof("=====active:%x======cab=%s Master=%d=====data:%x======", Signal, active.CabNum, master, data)
- //Set Master
- if master == 1 && active.CabNum == "1" {
- active.Master = true
- if !utils.CheckAsterisk() {
- lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
- cmd := exec.Command("service", "asterisk", "start")
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- err := cmd.Run()
- if err != nil {
- lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
- return
- }
- }
- } else if master == 8 && active.CabNum == "8" {
- active.Master = true
- if !utils.CheckAsterisk() {
- lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
- cmd := exec.Command("service", "asterisk", "start")
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- err := cmd.Run()
- if err != nil {
- lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
- return
- }
- }
- } else if master == 8 && active.CabNum == "1" {
- if utils.CheckAsterisk() {
- lfshook.NewLogger().Infof("Check asterisk , if running slave, run cmd service asterisk stop !")
- cmd := exec.Command("service", "asterisk", "stop")
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- err := cmd.Run()
- if err != nil {
- lfshook.NewLogger().Infof("Failed to stop asterisk: %v", err)
- return
- }
- }
- } else if master == 1 && active.CabNum == "8" {
- active.Master = false
- }
- //Set train info
- if TrainInfo != 0 {
- //Get train devide info
- DevideInfo := TrainInfo & 0x20
- if DevideInfo == 0x20 {
- active.TrainDevide = 1
- active.Master = true //列车断开,设置两边都Master
- if !utils.CheckAsterisk() {
- lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
- cmd := exec.Command("service", "asterisk", "start")
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- err := cmd.Run()
- if err != nil {
- lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
- return
- }
- }
- } else {
- active.TrainDevide = 0
- }
- //Radio fault
- RadioFault1 := TrainInfo & 0x03
- RadioFault8 := TrainInfo & 0x0c
- if RadioFault1 == 0 && RadioFault8 == 1 {
- active.RadioFault = 1
- } else {
- active.RadioFault = 0
- }
- utils.LoggerDebug.Printf("RadioFault1:%x RadioFault1:%x DevideInfo:%x", RadioFault1, RadioFault8, DevideInfo)
- }
- switch Signal {
- case 0:
- if active.ActivedCab != "" {
- active.ActivedCab = ""
- action.InActiveHangup()
- }
- case 1:
- //active signal from 8 to 1
- if active.ActivedCab == "8" || active.ActivedCab == "" {
- active.ActivedCab = "1"
- action.InActiveHangup()
- }
- case 8:
- //active signal from 1 to 8
- if active.ActivedCab == "1" || active.ActivedCab == "" {
- active.ActivedCab = "8"
- action.InActiveHangup()
- }
- }
- }
- // SPC ,特殊服务消息广播
- func SpecialAnn(data []byte) {
- delay := data[8]
- cycleCount := data[9]
- datalen := int(data[10])
- filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
- utils.LoggerDebug.Printf("Type:SPC FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
- if int(cycleCount) == 255 {
- action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
- } else {
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
- }
- }
- // EMG ,紧急服务消息广播
- func EmgMsg(data []byte) {
- delay := data[8]
- cycleCount := data[9]
- datalen := int(data[10])
- filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
- utils.LoggerDebug.Printf("Type:EMG FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
- if int(cycleCount) == 255 {
- action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
- } else {
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
- }
- }
- // 停止指定类型广播
- func AnnStop(data [4]byte) {
- //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
- utils.LoggerDebug.Printf("Stop PA Type:%x (DCS=3,EMG=4,SPC=7,STN=8,SelfCheck=9,ToneTest=10)", data[0])
- switch data[0] {
- case 0x03:
- action.HangupTask("DCS") //STOP DCS
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- case 0x04:
- action.HangupTask("EMG") //STOP EMG
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- case 0x07:
- action.HangupTask("SPC") //STOP SPC
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- case 0x08:
- action.HangupTask("STN") //STOP STN
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- case 0x09:
- action.HangupTask("CHK") //STOP CHK
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- case 0x0a:
- action.HangupTask("VOL") //STOP VOL
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- default:
- action.InterruptRunningTask("")
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
- }
- // DCS 语音
- func DcsAnn(data []byte) {
- delay := data[8]
- cycleCount := data[9]
- datalen := int(data[10])
- filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
- utils.LoggerDebug.Printf("Type:DCS FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
- if int(cycleCount) == 255 {
- action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
- } else {
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
- }
- }
- // tone-test广播
- func ToneTest(data []byte) {
- check := data[8]
- delay := data[9]
- cycleCount := data[10]
- datalen := int(data[11])
- filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
- utils.LoggerDebug.Printf("Type:ToneTest FileName:%x Count:%x Interval:%+v Action:%x (0x01=start/0x02=stop)", filename, cycleCount, delay, check)
- switch check {
- case 0x01: //start
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
- case 0x02: //stop
- action.HangupAllExcept("")
- }
- }
- // 自检广播
- func SelfCheck(data []byte) {
- check := data[8]
- delay := data[9]
- cycleCount := data[10]
- //cycleCount := 0x32
- datalen := int(data[11])
- filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
- utils.LoggerDebug.Printf("Type:SelfCehck FileName:%x Count:%x Interval:%+v Action:%x (0x01=start/0x02=stop)", filename, cycleCount, delay, check)
- switch check {
- case 0x01: //start
- action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
- case 0x02: //stop
- action.HangupAllExcept("")
- }
- }
- // 全局变量:记录正在抑制的 exten
- var (
- suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
- //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
- )
- // suppressKey 生成用于抑制的 key(可以根据需求扩展)
- func suppressKey(exten string, handler byte) string {
- return fmt.Sprintf("%s_h%x", exten, handler)
- }
- // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
- func AlarmHandleICP(data []byte) {
- handler := data[8]
- carr := data[12]
- pos := data[13]
- exten := fmt.Sprintf("24%c%c", carr, pos)
- utils.LoggerDebug.Printf("ICP Handle PAD:%s Action:%x (answer=1,hold=2,hangup=3)", exten, handler)
- switch handler {
- case 0x01: //answer(ICP+Alarm+PACU)
- //NotifyPaiu(exten, "answer")
- priority.ICPAnswer = 1
- if priority.PADStart == 0 {
- alstatus.PaStatus("", "PAD", "start")
- priority.PADStart = 1
- }
- utils.LoggerDebug.Printf("ICP Answer PAD:%s .", exten)
- if active.ActivedCab == "1" {
- action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
- //goto ami event ConfbridgeJoin, ICP answer PAD
- } else if active.ActivedCab == "8" {
- action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
- //goto ami event ConfbridgeJoin, ICP answer PAD
- } else if active.ActivedCab == "" { // No cab occupied
- action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
- }
- case 0x02: //hold 重新放回队列里面
- utils.LoggerDebug.Printf("ICP Hold PAD-ICP PAD:%s !", exten)
- active.NotifyPaiu(exten, "hold")
- err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
- if err != nil {
- utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
- }
- action.InterruptRunningTask("")
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- //action.HangupICP()
- case 0x03: //hangup
- //NotifyPaiu(exten, "hangup")
- utils.LoggerDebug.Printf("STC Hangup PAD-ICP !")
- action.Hangup(exten) //Pad
- //action.HangupICP()
- action.HangupTask("PAD-ICP")
- }
- }
- // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
- func AlarmHandleTMS(data []byte) {
- handler := data[8]
- //extlen := data[9]
- carr := data[12]
- pos := data[13]
- exten := fmt.Sprintf("24%c%c", carr, pos)
- PacuNum := fmt.Sprintf("21%c1", carr)
- utils.LoggerDebug.Printf("TMS Handle PAD:%s PACU:%s Action:%x (answer=1,hold=2,hangup=3)", exten, PacuNum, handler)
- switch handler {
- case 0x01: //answer(ICP+Alarm+PACU)
- //PACU---call---->ICP1
- //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
- if priority.PADStart == 0 {
- alstatus.PaStatus("", "PAD", "start")
- priority.PADStart = 1
- }
- priority.ICPAnswer = 1
- utils.LoggerDebug.Printf("TMS Answer PAD:%s PACU:%s", exten, PacuNum)
- if action.ExtenStatus(PacuNum) == "Idle" {
- if active.ActivedCab == "1" {
- action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
- //goto ami event BridgeEnter, ICP8 whisper ICP1
- } else if active.ActivedCab == "8" {
- action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
- //goto ami event BridgeEnter, ICP1 whisper ICP8
- } else if active.ActivedCab == "" { // No cab occupied
- action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
- }
- } else {
- action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
- }
- case 0x02: //hold 重新放回队列里面
- utils.LoggerDebug.Printf("ICP Hold PAD-TMS PAD:%s !", exten)
- active.NotifyPaiu(exten, "hold")
- err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
- if err != nil {
- utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
- }
- action.HangupAllLocalChan()
- case 0x03: //hangup
- //NotifyPaiu(exten, "hangup")
- utils.LoggerDebug.Printf("STC Hangup PAD-TMS !")
- action.Hangup(exten) //Pad
- action.HangupTask("PAD-TMS")
- //action.HangupTask("PAD-ICP")
- }
- }
- // 挂断所有报警器
- func AlarmHoldResetAll(handler byte) {
- utils.LoggerDebug.Printf("Alarm Hold/Reset All !")
- //hangup all actived PAD
- action.HangupAllPAD()
- //hangup running task
- action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
- time.Sleep(time.Millisecond * 100) //wait endpoint release
- }
|