|
|
@@ -7,6 +7,7 @@ import (
|
|
|
"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"
|
|
|
@@ -15,6 +16,7 @@ import (
|
|
|
"pbx-api-gin/pkg/utils"
|
|
|
"strconv"
|
|
|
"sync"
|
|
|
+ "syscall"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
@@ -70,20 +72,36 @@ func HandleStcCmd(ctx context.Context, conn net.Conn) {
|
|
|
}
|
|
|
|
|
|
// get train info and pacu info from heartbeat
|
|
|
-func processHeartbeat(info []byte) {
|
|
|
+func getInfofromSTC(info []byte) {
|
|
|
//dataCount := info[0]
|
|
|
//Get train info
|
|
|
- value := binary.BigEndian.Uint16(info[2:4]) //info[2] + info[3]
|
|
|
+ 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)
|
|
|
+ lfshook.NewLogger().Logger.Infof("TrainNum: %s", active.TrainNum)
|
|
|
|
|
|
//Get ICP volume
|
|
|
icpBit8 := info[9] & 0xF
|
|
|
icpBit1 := (info[9] >> 4) & 0xF
|
|
|
- active.DeviceEndpoint.ICPInfo[0].Volume = fmt.Sprintf("%d", icpBit1)
|
|
|
+ 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", icpBit8)
|
|
|
+ active.DeviceEndpoint.ICPInfo[1].Volume = fmt.Sprintf("%d", icpVol8)
|
|
|
active.DeviceEndpoint.ICPInfo[1].ID = "8"
|
|
|
|
|
|
//Get Pacu info
|
|
|
@@ -92,8 +110,8 @@ func processHeartbeat(info []byte) {
|
|
|
|
|
|
pacuVolume := binary.BigEndian.Uint32(info[5:9]) //info[5]-info[8]
|
|
|
|
|
|
- //lfshook.NewLogger().Logger.Infof("=eidsStat:%x=======pacuStat:%x=====", eidsStat, pacuStat)
|
|
|
-
|
|
|
+ //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(最低位,最右)
|
|
|
@@ -103,8 +121,8 @@ func processHeartbeat(info []byte) {
|
|
|
//fmt.Printf("pacuStatBit bit %d = %d\n", i, pacuStatBit)
|
|
|
|
|
|
//get 4 bit every time
|
|
|
- var pacuVolBit int8
|
|
|
- pacuVolBit = int8((pacuVolume >> i) & 0xF)
|
|
|
+ //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)
|
|
|
@@ -118,7 +136,11 @@ func processHeartbeat(info []byte) {
|
|
|
} else {
|
|
|
active.DeviceEndpoint.PacuInfo[i].Status = "abnormal"
|
|
|
}
|
|
|
- active.DeviceEndpoint.PacuInfo[i].Volume = strconv.Itoa(int(pacuVolBit))
|
|
|
+ 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)
|
|
|
}
|
|
|
}
|
|
|
@@ -154,13 +176,15 @@ func processPacket(packet []byte) {
|
|
|
|
|
|
switch packet[5] {
|
|
|
case 0x01: //heartbeat
|
|
|
- dataLen := binary.BigEndian.Uint16(packet[6:8])
|
|
|
-
|
|
|
- if dataLen == 10 {
|
|
|
- processHeartbeat(packet[8:])
|
|
|
- } else {
|
|
|
- //lfshook.NewLogger().Logger.Infof("=========Heartbeat data err !====len < 10=========")
|
|
|
- }
|
|
|
+ return
|
|
|
+ //lfshook.NewLogger().Logger.Infof("===Get Heartbeat===packet:%x===========", packet)
|
|
|
+ //dataLen := binary.BigEndian.Uint16(packet[6:8])
|
|
|
+
|
|
|
+ //if dataLen == 10 {
|
|
|
+ // processHeartbeat(packet[8:])
|
|
|
+ //} else {
|
|
|
+ //lfshook.NewLogger().Logger.Infof("=========Heartbeat data err !====len < 10=========")
|
|
|
+ //}
|
|
|
case 0x02: // STN
|
|
|
if active.ActivedCab != "" {
|
|
|
if priority.CheckPriority("STN") {
|
|
|
@@ -250,9 +274,12 @@ func processPacket(packet []byte) {
|
|
|
case 0x0b: // reset all PAD
|
|
|
AlarmHoldResetAll(packet[8]) // reset all pad
|
|
|
|
|
|
- case 0x0c: // Set PAD time oute
|
|
|
+ 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
|
|
|
if priority.CheckPriority("PAD-ICP") {
|
|
|
|
|
|
@@ -312,72 +339,51 @@ func StationAnn(data []byte) (err error) {
|
|
|
// 激活信号
|
|
|
func Active(data [3]byte) {
|
|
|
|
|
|
- //lfshook.NewLogger().Logger.Infof("===cab%s======Active Type %+v", active.CabNum, data)
|
|
|
-
|
|
|
- //check asterisk available
|
|
|
- /* if active.Master { // master true
|
|
|
- if !utils.CheckAsterisk() { //check asterisk not available and set master false
|
|
|
- if active.MasterTag > 2 {
|
|
|
- active.Master = false
|
|
|
- active.MasterTag = 0
|
|
|
- } else {
|
|
|
- active.MasterTag++
|
|
|
- }
|
|
|
- //utils.ExecCmd("/etc/init.d/asterisk", "stop", "PBX")
|
|
|
- } else {
|
|
|
- active.MasterTag = 0
|
|
|
- }
|
|
|
-
|
|
|
- } else { // master false, check and start asterisk
|
|
|
-
|
|
|
- //Master == false
|
|
|
- if !utils.CheckAsterisk() {
|
|
|
- if active.CabNum == "8" {
|
|
|
- //utils.ExecCmd("/etc/init.d/asterisk", "start", "PBX")
|
|
|
- utils.ExecCmd("/usr/sbin/asterisk")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //Master == false cab == 1
|
|
|
- //if active.CabNum == "1" {
|
|
|
- // if utils.CheckAsterisk() {
|
|
|
- // utils.ExecCmd("/etc/init.d/asterisk", "stop", "PBX")
|
|
|
- // }
|
|
|
- //}
|
|
|
- }
|
|
|
- */
|
|
|
-
|
|
|
//var info model.Sysinfo
|
|
|
Signal := int(data[0])
|
|
|
master := int(data[1])
|
|
|
TrainInfo := int(data[2])
|
|
|
|
|
|
- utils.LoggerDebug.Printf("CabNumber:%s Active:%x Master:%x Train devide & radiofault :%x", active.CabNum, Signal, master, TrainInfo)
|
|
|
+ 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
|
|
|
- //lfshook.NewLogger().Logger.Infof("=====cab1 Master")
|
|
|
if !utils.CheckAsterisk() {
|
|
|
- utils.LoggerDebug.Printf("Check asterisk , not running , run cmd /usr/sbin/asterisk !")
|
|
|
- utils.ExecCmdAsync("/usr/sbin/asterisk")
|
|
|
- time.Sleep(3 * time.Second)
|
|
|
+ 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
|
|
|
- //utils.LoggerDebug.Printf("=====cab8 Master")
|
|
|
if !utils.CheckAsterisk() {
|
|
|
- utils.LoggerDebug.Printf("Check asterisk , not running , run cmd /usr/sbin/asterisk !")
|
|
|
- utils.ExecCmdAsync("/usr/sbin/asterisk")
|
|
|
- time.Sleep(3 * time.Second)
|
|
|
+ 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() {
|
|
|
- utils.LoggerDebug.Printf("Master 8, cab1 ; Check asterisk , running , run cmd /etc/init.d/asterisk stop PBX !")
|
|
|
- utils.ExecCmd("/etc/init.d/asterisk", "stop", "PBX")
|
|
|
+ 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
|
|
|
@@ -392,7 +398,14 @@ func Active(data [3]byte) {
|
|
|
|
|
|
active.Master = true //列车断开,设置两边都Master
|
|
|
if !utils.CheckAsterisk() {
|
|
|
- utils.ExecCmd("/usr/sbin/asterisk")
|
|
|
+ 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
|