日志调整及现场测试

This commit is contained in:
tiger_zhou 2024-06-13 10:06:54 +08:00
parent 35920fbfeb
commit a808f8f73e
5 changed files with 53 additions and 40 deletions

View File

@ -8,7 +8,6 @@ import (
"joylink.club/rtsssimulation/fi" "joylink.club/rtsssimulation/fi"
"joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto" "joylink.club/rtsssimulation/repository/model/proto"
"log/slog"
"math" "math"
"sort" "sort"
"sync" "sync"
@ -72,7 +71,7 @@ func (t *BaliseDetector) tryRebind(th *TrainHeadPositionInfo) {
t.clearExpectedBalise() t.clearExpectedBalise()
t.baliseCounter = 0 t.baliseCounter = 0
t.messageCounter = 0 t.messageCounter = 0
slog.Debug(fmt.Sprintf("列车[%s]与CAN-BTM绑定", t.trianId)) //slog.Debug(fmt.Sprintf("列车[%s]与CAN-BTM绑定", t.trianId))
} }
} }

View File

@ -22,11 +22,13 @@ func StartTcpClient(rAddr string, handler func(n int, data []byte), readErr func
} }
conn, err := net.DialTCP("tcp", nil, raddr) conn, err := net.DialTCP("tcp", nil, raddr)
ctx, ctxFun := context.WithCancel(context.Background()) ctx, ctxFun := context.WithCancel(context.Background())
client := &TcpClient{conn: conn, ctx: ctxFun}
if err != nil { if err != nil {
client.ctx = ctxFun
client.conning = false
return nil, err return nil, err
} }
client := &TcpClient{conn: conn, ctx: ctxFun}
go func() { go func() {
for { for {
select { select {
@ -35,18 +37,18 @@ func StartTcpClient(rAddr string, handler func(n int, data []byte), readErr func
default: default:
} }
data := make([]byte, 1024) data := make([]byte, 1024)
l, err := conn.Read(data) l, readDataErr := conn.Read(data)
if err != nil { if readDataErr != nil {
if opErr, ok := err.(*net.OpError); ok { if opErr, ok := readDataErr.(*net.OpError); ok {
slog.Error(fmt.Sprintf("TCP客户端[rAddr:%s]读取数据异常连接可能断开:", rAddr), opErr) slog.Error(fmt.Sprintf("TCP客户端[rAddr:%s]读取数据异常连接可能断开:", rAddr), opErr)
client.conning = false client.conning = false
readErr(err) readErr(readDataErr)
} } else if err == io.EOF {
if err == io.EOF {
slog.Warn(fmt.Sprintf("TCP客户端[rAddr:%s]断开连接:", rAddr)) slog.Warn(fmt.Sprintf("TCP客户端[rAddr:%s]断开连接:", rAddr))
client.conning = false client.conning = false
readErr(err) readErr(err)
} }
return
} }
client.conning = true client.conning = true
handler(l, data) handler(l, data)
@ -72,12 +74,12 @@ func (c *TcpClient) IsConning() bool {
func (c *TcpClient) Send(data []byte) error { func (c *TcpClient) Send(data []byte) error {
if c == nil || c.conn == nil { if c == nil || c.conn == nil {
slog.Error("tcp client send error,conn is nil") //slog.Error("tcp client send error,conn is nil")
return fmt.Errorf("TCP未连接车载PC仿真") return fmt.Errorf("tcp client send error,conn is nil")
} }
_, err := c.conn.Write(data) _, err := c.conn.Write(data)
if err != nil { if err != nil {
slog.Error("tcp client send error", "error", err) //slog.Error("tcp client send error", "error", err)
return err return err
} }
return nil return nil

View File

@ -9,6 +9,7 @@ import (
"log/slog" "log/slog"
"net" "net"
"strconv" "strconv"
"strings"
) )
type TcpConnHandler = func(conn net.Conn) type TcpConnHandler = func(conn net.Conn)
@ -68,12 +69,13 @@ func StartTcpServer(port int, connHandler TcpConnHandler, msgHandler TcpMsgHandl
return listen, err return listen, err
} }
var serConn net.Conn var serConnMap map[int]net.Conn = make(map[int]net.Conn)
func createServer(h TcpMsgHandler) { func createServer(port int, h TcpMsgHandler) {
StartTcpServer(5600, func(conn net.Conn) { StartTcpServer(port, func(conn net.Conn) {
fmt.Println("TCP服务端接收到连接") fmt.Println("TCP服务端接收到连接")
serConn = conn serConnMap[port] = conn
}, h) }, h)
} }
func connTrain() *message.TrainPcSimBaseMessage { func connTrain() *message.TrainPcSimBaseMessage {
@ -162,20 +164,18 @@ func pcSimNumReportOut() *message.TrainPcSimBaseMessage {
}) })
select {} select {}
}*/ }*/
func startService(port int) {
func main() { createServer(port, func(n int, data []byte) {
createServer(func(n int, data []byte) {
msg := &message.TrainPcSimBaseMessage{} msg := &message.TrainPcSimBaseMessage{}
d := data[:n] d := data[:n]
msg.Decode(d) msg.Decode(d)
pd := fmt.Sprintf("%X", d) pd := fmt.Sprintf("%X", d)
if msg.Type == train_pc_sim.SENDER_TRAIN_TC_ACTIVE { if msg.Type == train_pc_sim.SENDER_TRAIN_TC_ACTIVE {
fmt.Println("接收驾驶端激活") fmt.Println("接收驾驶端激活 port:", port)
fmt.Println(pd)
} else if msg.Type == train_pc_sim.SENDER_TRAIN_TC_NOT_ACTIVE { } else if msg.Type == train_pc_sim.SENDER_TRAIN_TC_NOT_ACTIVE {
fmt.Println("接收驾驶端未激活") fmt.Println("接收驾驶端未激活 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.SENDER_TRAIN_OUTR_INFO { } else if msg.Type == train_pc_sim.SENDER_TRAIN_OUTR_INFO {
fmt.Println(pd) fmt.Println(pd)
@ -194,31 +194,31 @@ func main() {
case 4: case 4:
tt = "制动状态" tt = "制动状态"
} }
fmt.Println("接受列车输出数字量", tt, s) fmt.Println("接受列车输出数字量", tt, s, port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE {
state := msg.Data[0] state := msg.Data[0]
if state == 0x01 { if state == 0x01 {
fmt.Println("创建列车") fmt.Println("创建列车 port:", port)
} else if state == 0x00 { } else if state == 0x00 {
fmt.Println("删除列车") fmt.Println("删除列车 port:", port)
} }
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.SENDER_TRAIN_HAND_KEY_FORWARD { } else if msg.Type == train_pc_sim.SENDER_TRAIN_HAND_KEY_FORWARD {
fmt.Println("列车手柄向前") fmt.Println("列车手柄向前 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_CANCLE_FORWARD { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_CANCLE_FORWARD {
fmt.Println("列车手柄取消向前") fmt.Println("列车手柄取消向前 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_BACKWARD { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_BACKWARD {
fmt.Println("列车手柄向后") fmt.Println("列车手柄向后 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_CACLE_BACKWARD { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_HAND_KEY_CACLE_BACKWARD {
fmt.Println("列车手柄取消向后") fmt.Println("列车手柄取消向后 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_BTM_HAS_DATA { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_BTM_HAS_DATA {
fmt.Println("有数据应答") fmt.Println("有数据应答 port:", port)
fmt.Println(pd) fmt.Println(pd)
} else if msg.Type == train_pc_sim.RECIVE_TRAIN_BTM_NOT_DATA { } else if msg.Type == train_pc_sim.RECIVE_TRAIN_BTM_NOT_DATA {
@ -231,18 +231,30 @@ func main() {
}*/ }*/
}) })
}
func main() {
port1 := 5600
port2 := 5601
go startService(port1)
go startService(port2)
//reader := bufio.NewReader(os.Stdin) //reader := bufio.NewReader(os.Stdin)
var command string var command string
for { for {
if serConn == nil {
continue
}
fmt.Scanln(&command) fmt.Scanln(&command)
if command != "" { if command != "" {
fmt.Println(command) fmt.Println(command)
} }
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
if command == "create-train" { if command == "create-train" {
msg := connTrain() msg := connTrain()
serConn.Write(msg.Encode()) serConn.Write(msg.Encode())

View File

@ -64,7 +64,7 @@ type trainPcReciverData struct {
pcSimManage TrainPcSimManage pcSimManage TrainPcSimManage
} }
func (rd *trainPcReciverData) reciverDataHandle(n int, data []byte) { func (rd *trainPcReciverData) receiverDataHandle(n int, data []byte) {
connType := state_proto.TrainConnState_PC_SIM_A connType := state_proto.TrainConnState_PC_SIM_A
if rd.clientKey == "B" { if rd.clientKey == "B" {
@ -248,7 +248,7 @@ func (d *trainPcSimService) initConn(clientKey string) {
cfg, _ := d.findConfig(clientKey) cfg, _ := d.findConfig(clientKey)
addr := fmt.Sprintf("%v:%v", cfg.PcSimIp, cfg.PcSimPort) addr := fmt.Sprintf("%v:%v", cfg.PcSimIp, cfg.PcSimPort)
client2, err := tcp.StartTcpClient(addr, rd.reciverDataHandle, d.readError) client2, err := tcp.StartTcpClient(addr, rd.receiverDataHandle, d.readError)
if err != nil { if err != nil {
slog.Error("车载pc连接失败 clientKey:", clientKey, "error:", err.Error()) slog.Error("车载pc连接失败 clientKey:", clientKey, "error:", err.Error())
d.updateState(tpapi.ThirdPartyState_Broken) d.updateState(tpapi.ThirdPartyState_Broken)

View File

@ -153,7 +153,7 @@ func TrainConnTypeUpdate(vs *VerifySimulation, ct *dto.TrainConnThirdDto) {
connTypeName := "半实物" connTypeName := "半实物"
if tmpTrain.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_A { if tmpTrain.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_A {
connTypeName = "车载pc仿真-A" connTypeName = "车载pc仿真-A"
} else if tmpTrain.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_A { } else if tmpTrain.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_B {
connTypeName = "车载pc仿真-B" connTypeName = "车载pc仿真-B"
} }
panic(sys_error.New(fmt.Sprintf("列车[%s]已经连接 [%v],此列车无法连接", k, connTypeName))) panic(sys_error.New(fmt.Sprintf("列车[%s]已经连接 [%v],此列车无法连接", k, connTypeName)))