| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- package alstatus
- import (
- "fmt"
- "net"
- "pbx-api-gin/internal/app/stc/active"
- msgdata "pbx-api-gin/internal/app/stc/data"
- "pbx-api-gin/internal/app/stc/socket"
- "pbx-api-gin/pkg/utils"
- "time"
- )
- func SendToStc(conn net.Conn, data []byte) {
- _, err := conn.Write(data)
- if err != nil {
- utils.LoggerDebug.Printf("Send To STC msg err:%+v", err)
- conn.Close()
- }
- //lfshook.NewLogger().Logger.Infof("SendToStc data:%x", data)
- }
- // report alarm status to STC
- func SendQueueStatus(pads string, count int) {
- if !active.CheckReunoinMode() {
- return
- }
- //Not Master role , ignore
- if !active.Master {
- return
- }
- DataLen := len(pads) + 2
- protocol := msgdata.NewProtocol()
- protocol.MessageID = 0x29
- protocol.DataLength = uint16(DataLen)
- protocol.Data = make([]byte, DataLen)
- protocol.Data[0] = byte(count) //排队数量
- copy(protocol.Data[1:], []byte(pads))
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- fmt.Println("Encode error:", errEn)
- return
- }
- //check if actived
- utils.LoggerDebug.Printf("PADs number:%s count:%d ", protocol.Data[1:], protocol.Data[0])
- if socket.Conn != nil {
- SendToStc(socket.Conn, encoded)
- }
- if socket.Conn6 != nil {
- SendToStc(socket.Conn6, encoded)
- }
- }
- // report alarm status to STC
- func AlarmStatus(exten string, status string) {
- if !active.CheckReunoinMode() {
- return
- }
- //Not Master role , ignore
- if !active.Master {
- return
- }
- //check exten if it is a alarm exten
- if !utils.IsPAIU(exten) { // if not alarm device , return
- return
- }
- protocol := msgdata.NewProtocol()
- protocol.MessageID = 0x26
- protocol.DataLength = 0x1E
- protocol.Data = make([]byte, 30)
- protocol.Data[0] = exten[2] - '0' //车厢号
- protocol.Data[1] = exten[3] - '0' //位置号
- //报警器工作状态
- switch status {
- case "unavailable", "Unavailable": //offline
- protocol.Data[2] = 0x00
- case "idle", "Idle": //idle
- protocol.Data[2] = 0x01
- case "dial": //dial
- protocol.Data[2] = 0x02
- return
- case "queue": //PAD alarm
- protocol.Data[2] = 0x03
- case "connect": //connect
- protocol.Data[2] = 0x04
- case "allreset": //allreset
- protocol.MessageID = 0x29
- protocol.Data[0] = 0x02 //all reset
- protocol.Data[1] = 0
- protocol.Data[2] = 0
- case "allhold": //allhold
- protocol.MessageID = 0x29
- protocol.Data[0] = 0x01 //all hold
- protocol.Data[1] = 0
- protocol.Data[2] = 0
- }
- //填充时间
- now := time.Now()
- ntm := now.Format("2006-01-02 15:04:05")
- //fmt.Println(s)
- copy(protocol.Data[3:], []byte(ntm))
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- fmt.Println("Encode error:", errEn)
- return
- }
- //check if actived
- utils.LoggerDebug.Printf("PAD number:%s CarNum:%x Pos:%x Status:%x(0=Offline,1=Idle,2=calling,3=Hold,4=Connected)", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
- if socket.Conn != nil {
- SendToStc(socket.Conn, encoded)
- }
- if socket.Conn6 != nil {
- SendToStc(socket.Conn6, encoded)
- }
- }
- // report alarm status to STC
- func SendICPStatus(exten string, status string) {
- if !active.CheckReunoinMode() {
- return
- }
- //Not Master role , ignore
- if !active.Master {
- return
- }
- //check exten if it is a alarm exten
- if !utils.IsPAIU(exten) { // if not alarm device , return
- return
- }
- protocol := msgdata.NewProtocol()
- protocol.MessageID = 0x25
- protocol.DataLength = 0x04
- protocol.Data = make([]byte, 4)
- protocol.Data[0] = exten[2] - '0' //车厢号
- protocol.Data[1] = exten[3] - '0' //位置号
- //报警器工作状态
- switch status {
- case "unavailable", "Unavailable": //offline
- protocol.Data[2] = 0x00
- case "idle", "Idle": //idle
- protocol.Data[2] = 0x01
- case "dial": //dial
- protocol.Data[2] = 0x02
- return
- case "connect": //PAD alarm
- protocol.Data[2] = 0x03
- }
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- fmt.Println("Encode error:", errEn)
- return
- }
- //check if actived
- utils.LoggerDebug.Printf("PAD number:%s CarNum:%x Pos:%x Status:%x(0=Offline,1=Idle,2=calling,3=Hold,4=Connected)", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
- if socket.Conn != nil {
- SendToStc(socket.Conn, encoded)
- }
- if socket.Conn6 != nil {
- SendToStc(socket.Conn6, encoded)
- }
- }
- // report broadcast status to STC
- func PaStatus(src string, patype string, operation string) {
- if !active.CheckReunoinMode() {
- return
- }
- //Not Master role , ignore
- if !active.Master {
- return
- }
- //过滤掉非EMG运行模式下的continue状态发送
- //if operation == "continue" {
- // taskName, _, _ := priority.RegistryTask.HighestPriorityRunningTask()
- // if taskName != "EMG" {
- // return
- // }
- //}
- utils.LoggerDebug.Printf("PA Status Src:%s Type:%s Status:%s", src, patype, operation)
- protocol := msgdata.NewProtocol()
- protocol.MessageID = 0x22
- protocol.DataLength = 0x04
- protocol.Data = make([]byte, 4)
- //广播发起方
- switch src {
- case "1211", "2111": //mc1
- protocol.Data[0] = 0x01
- case "1261": //mc8
- protocol.Data[0] = 0x08
- default: //
- protocol.Data[0] = 0x00
- }
- //广播类型
- switch patype {
- case "PA": //人工广播---ami
- protocol.Data[1] = 0x01
- case "STNA": //自动报站
- protocol.Data[1] = 0x02
- case "STNS": //手动报站
- protocol.Data[1] = 0x03
- case "SVM": //服务消息
- protocol.Data[1] = 0x04
- case "PMC": //远程广播
- protocol.Data[1] = 0x05
- case "SVA": //服务音频
- protocol.Data[1] = 0x06
- case "DCS": //开关门提示
- protocol.Data[1] = 0x07
- }
- //操作类型
- switch operation {
- case "start": //
- protocol.Data[2] = 0x01
- case "end": //
- protocol.Data[2] = 0x02
- case "refuse": //
- protocol.Data[2] = 0x03
- case "fail": //
- protocol.Data[2] = 0x04
- case "continue": //
- protocol.Data[2] = 0x05
- }
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- utils.LoggerDebug.Printf("Encode error:%+v", errEn)
- return
- }
- if socket.Conn != nil {
- SendToStc(socket.Conn, encoded)
- }
- if socket.Conn6 != nil {
- SendToStc(socket.Conn6, encoded)
- }
- }
- // report broadcast status to STC
- func SendRecordFile(filename, rcdtype string) {
- if !active.CheckReunoinMode() {
- return
- }
- //time.Sleep(5 * time.Second)
- /*
- if !utils.FileExists(filename) {
- lfshook.NewLogger().Logger.Infof("===Recording filename not exist:%+v=", filename)
- return
- }
- */
- //Not Master role , ignore
- if !active.Master {
- return
- }
- protocol := msgdata.NewProtocol()
- protocol.MessageID = 0x31
- filenameHex := []byte(filename)
- dataLen := len(filenameHex) + 1
- protocol.DataLength = uint16(dataLen)
- protocol.Data = make([]byte, dataLen)
- copy(protocol.Data[1:], filenameHex)
- switch rcdtype {
- case "C2C": //
- protocol.Data[0] = 0x01
- case "PA": //
- protocol.Data[0] = 0x02
- case "PAD": //
- protocol.Data[0] = 0x05
- case "CPA": //
- protocol.Data[0] = 0x06
- case "OTR": //
- protocol.Data[0] = 0x03
- //lfshook.NewLogger().Logger.Infof("===Recording filename:%+v=", protocol.Data)
- }
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- utils.LoggerDebug.Printf("Encode error:%+v", errEn)
- return
- }
- if socket.Conn != nil {
- SendToStc(socket.Conn, encoded)
- }
- if socket.Conn6 != nil {
- SendToStc(socket.Conn6, encoded)
- }
- }
|