rts-sim-testing-service/third_party/train_pc_sim/example/main.go

294 lines
8.0 KiB
Go
Raw Normal View History

2024-04-09 08:55:33 +08:00
package main
import (
2024-04-18 11:14:05 +08:00
"encoding/binary"
2024-06-27 10:37:52 +08:00
"encoding/hex"
2024-04-09 08:55:33 +08:00
"fmt"
"io"
"joylink.club/bj-rtsts-server/third_party/message"
"log/slog"
"net"
2024-04-18 11:14:05 +08:00
"strconv"
2024-06-13 10:06:54 +08:00
"strings"
2024-04-09 08:55:33 +08:00
)
type TcpConnHandler = func(conn net.Conn)
type TcpMsgHandler = func(n int, data []byte)
func tcpRunAcceptTask(listen net.Listener, port int, connHandler TcpConnHandler, msgHandler TcpMsgHandler) {
go func() {
defer func() {
if err := recover(); err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]接收连接任务异常:", port), err)
tcpRunAcceptTask(listen, port, connHandler, msgHandler)
}
}()
for {
conn, err := listen.Accept()
if err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]接收连接出错:", port), err)
}
connHandler(conn)
tcpRunReadTask(conn, port, msgHandler)
}
}()
}
func tcpRunReadTask(conn net.Conn, port int, msgHandler TcpMsgHandler) {
go func() {
2024-04-18 11:14:05 +08:00
/* defer func() {
2024-04-09 08:55:33 +08:00
if err := recover(); err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]读数据任务异常:", port), err)
2024-04-13 09:40:25 +08:00
serConn.Close()
serConn = nil
2024-04-09 08:55:33 +08:00
}
2024-04-18 11:14:05 +08:00
}()*/
2024-04-09 08:55:33 +08:00
for {
data := make([]byte, 1024)
l, err := conn.Read(data)
if err != nil {
if err == io.EOF {
slog.Warn(fmt.Sprintf("TCP服务端[port:%d]断开[%s]连接:", port, conn.RemoteAddr().String()))
break
}
slog.Error(fmt.Sprintf("TCP服务端[port:%d]读取[%s]数据出错:", port, conn.RemoteAddr().String()), err)
2024-04-13 09:40:25 +08:00
2024-04-09 08:55:33 +08:00
}
msgHandler(l, data)
}
}()
}
func StartTcpServer(port int, connHandler TcpConnHandler, msgHandler TcpMsgHandler) (net.Listener, error) {
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
tcpRunAcceptTask(listen, port, connHandler, msgHandler)
return listen, err
}
2024-06-13 10:06:54 +08:00
var serConnMap map[int]net.Conn = make(map[int]net.Conn)
2024-04-09 08:55:33 +08:00
2024-06-13 10:06:54 +08:00
func createServer(port int, h TcpMsgHandler) {
StartTcpServer(port, func(conn net.Conn) {
2024-04-09 08:55:33 +08:00
fmt.Println("TCP服务端接收到连接")
2024-06-13 10:06:54 +08:00
serConnMap[port] = conn
2024-04-09 08:55:33 +08:00
}, h)
}
func connTrain() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_CREATE_REMOVE
2024-04-09 08:55:33 +08:00
msg.Data = []byte{0x01}
return msg
}
func changeDoorMode() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_DOOR_MODE
2024-04-09 08:55:33 +08:00
msg.Data = []byte{0x02}
return msg
}
2024-04-18 11:14:05 +08:00
func boolsToByte(flags [8]bool) byte {
var result uint8
for index, b := range flags {
if b {
result = result + (1 << index)
}
}
return result
}
func pcSimInfoOut() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_INTERFACE_CABINET_OUTR
2024-04-18 11:14:05 +08:00
data := make([]byte, 0)
data = append(data, boolsToByte([8]bool{false, false, false, false, false, false, false, false}))
data = append(data, boolsToByte([8]bool{false, false, false, false, false, false, false, false}))
data = append(data, boolsToByte([8]bool{false, false, false, false, false, false, false, false}))
data = append(data, boolsToByte([8]bool{false, false, false, false, false, false, false, false}))
data = append(data, boolsToByte([8]bool{true, false, false, false, false, false, false, false}))
msg.Data = data
return msg
}
func pcSimInfoOutReport() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_INTERFACE_CABINET_OUTR_BACK
2024-04-18 11:14:05 +08:00
data := make([]byte, 0)
data = append(data, boolsToByte([8]bool{true, true, false, false, false, false, false, false}))
msg.Data = data
return msg
}
var autoIncNo = 0
func queryBtm() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_QUERY_STATUS
2024-04-18 11:14:05 +08:00
autoIncNo = autoIncNo + 1
data := make([]byte, 0)
data = append(data, 0x62)
data = append(data, 0x81)
data = append(data, 0x02)
data = append(data, byte(autoIncNo)<<3)
data = append(data, boolsToByte([8]bool{true, true, false, false, false, false, false, false}))
data = append(data, 0) //速度低位
data = append(data, 0) //当前时间高位
data = append(data, 0) //当前时间
data = append(data, 0) //当前时间
data = append(data, 0) //当前时间低位
data = append(data, 0) //当前时间低位
data = append(data, 0) //CRC16-H(MSB
data = append(data, 0) //CRC16-L(LSB)
msg.Data = data
return msg
}
func pcSimNumReportOut() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
2024-06-27 10:37:52 +08:00
msg.Type = message.RECIVE_TRAIN_MOCK_DATA
2024-04-18 11:14:05 +08:00
sd := uint16(1234)
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, sd)
msg.Data = data
return msg
}
2024-04-09 08:55:33 +08:00
// 测试创建连接
/*func TestConn(t *testing.T) {
createServer(func(n int, data []byte) {
})
select {}
}*/
2024-06-13 10:06:54 +08:00
func startService(port int) {
createServer(port, func(n int, data []byte) {
2024-04-09 08:55:33 +08:00
msg := &message.TrainPcSimBaseMessage{}
2024-04-18 11:14:05 +08:00
d := data[:n]
//fmt.Println(fmt.Sprintf("接受数据:%v", hex.EncodeToString(d)))
2024-04-18 11:14:05 +08:00
msg.Decode(d)
pd := fmt.Sprintf("%X", d)
2024-06-27 10:37:52 +08:00
if msg.Type == message.SENDER_TRAIN_TC_ACTIVE {
2024-06-13 10:06:54 +08:00
fmt.Println("接收驾驶端激活 port:", port)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.SENDER_TRAIN_TC_NOT_ACTIVE {
2024-06-13 10:06:54 +08:00
fmt.Println("接收驾驶端未激活 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.SENDER_TRAIN_OUTR_INFO {
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
t := msg.Data[0]
s := msg.Data[1]
tt := strconv.Itoa(int(t))
switch t {
case 0:
tt = "驾驶台"
case 1:
tt = "手柄向前"
case 2:
tt = "手柄向后"
case 14:
tt = "手柄归零"
case 4:
tt = "制动状态"
}
2024-06-13 10:06:54 +08:00
fmt.Println("接受列车输出数字量", tt, s, port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_CREATE_REMOVE {
2024-04-18 11:14:05 +08:00
state := msg.Data[0]
if state == 0x01 {
2024-06-13 10:06:54 +08:00
fmt.Println("创建列车 port:", port)
2024-04-18 11:14:05 +08:00
} else if state == 0x00 {
2024-06-13 10:06:54 +08:00
fmt.Println("删除列车 port:", port)
2024-04-18 11:14:05 +08:00
}
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.SENDER_TRAIN_HAND_KEY_FORWARD {
2024-06-13 10:06:54 +08:00
fmt.Println("列车手柄向前 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_HAND_KEY_CANCLE_FORWARD {
2024-06-13 10:06:54 +08:00
fmt.Println("列车手柄取消向前 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_HAND_KEY_BACKWARD {
2024-06-13 10:06:54 +08:00
fmt.Println("列车手柄向后 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_HAND_KEY_CACLE_BACKWARD {
2024-06-13 10:06:54 +08:00
fmt.Println("列车手柄取消向后 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_BTM_HAS_DATA {
2024-06-13 10:06:54 +08:00
fmt.Println("有数据应答 port:", port)
2024-04-18 11:14:05 +08:00
fmt.Println(pd)
2024-06-27 10:37:52 +08:00
} else if msg.Type == message.RECIVE_TRAIN_BTM_NOT_DATA {
2024-04-18 11:14:05 +08:00
} /*else if msg.Type == train_pc_sim.SENDER_TRAIN_LOCATION_INFO {
fmt.Println("列车速度位置报告")
fmt.Println(pd)
mp := message.TrainSpeedPlaceReportMsg{}
mp.Decode(msg.Data)
}*/
2024-04-09 08:55:33 +08:00
})
2024-06-13 10:06:54 +08:00
}
func main() {
port1 := 5600
port2 := 5601
go startService(port1)
go startService(port2)
2024-04-09 08:55:33 +08:00
//reader := bufio.NewReader(os.Stdin)
var command string
for {
2024-06-13 10:06:54 +08:00
2024-04-09 08:55:33 +08:00
fmt.Scanln(&command)
if command != "" {
fmt.Println(command)
}
2024-06-13 10:06:54 +08:00
strs := strings.Split(command, ",")
if len(strs) < 2 {
fmt.Println("eeeeeeee")
command = ""
continue
}
p1s, comm := strs[0], strs[1]
portInt, _ := strconv.Atoi(p1s)
serConn := serConnMap[portInt]
command = comm
2024-04-09 08:55:33 +08:00
if command == "create-train" {
msg := connTrain()
serConn.Write(msg.Encode())
} else if command == "door-mode" {
msg := changeDoorMode()
serConn.Write(msg.Encode())
2024-04-18 11:14:05 +08:00
} else if command == "info-out" {
2024-06-27 10:37:52 +08:00
hexStr := "eb010a8670400000ffff"
data, _ := hex.DecodeString(hexStr)
//msg := pcSimInfoOut()
//serConn.Write(msg.Encode())
serConn.Write(data)
2024-04-18 11:14:05 +08:00
} else if command == "info-out-report" {
2024-06-27 10:37:52 +08:00
2024-04-18 11:14:05 +08:00
msg := pcSimInfoOutReport()
serConn.Write(msg.Encode())
} else if command == "query-btm" {
msg := queryBtm()
serConn.Write(msg.Encode())
} else if command == "num-out" {
msg := pcSimNumReportOut()
serConn.Write(msg.Encode())
2024-06-27 10:37:52 +08:00
} else if command == "query" {
hexStr := "eb04110c50204b900000172065fc160000"
data, _ := hex.DecodeString(hexStr)
2024-04-18 11:14:05 +08:00
2024-06-27 10:37:52 +08:00
serConn.Write(data)
2024-04-09 08:55:33 +08:00
}
command = ""
/*content, _ := reader.ReadString('\n')
if content == "create-train" {
}*/
}
select {}
}