Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
thesai 2024-06-13 10:25:13 +08:00
commit 1afa01bffb
5 changed files with 53 additions and 40 deletions

View File

@ -8,7 +8,6 @@ import (
"joylink.club/rtsssimulation/fi"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
"log/slog"
"math"
"sort"
"sync"
@ -72,7 +71,7 @@ func (t *BaliseDetector) tryRebind(th *TrainHeadPositionInfo) {
t.clearExpectedBalise()
t.baliseCounter = 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)
ctx, ctxFun := context.WithCancel(context.Background())
client := &TcpClient{conn: conn, ctx: ctxFun}
if err != nil {
client.ctx = ctxFun
client.conning = false
return nil, err
}
client := &TcpClient{conn: conn, ctx: ctxFun}
go func() {
for {
select {
@ -35,18 +37,18 @@ func StartTcpClient(rAddr string, handler func(n int, data []byte), readErr func
default:
}
data := make([]byte, 1024)
l, err := conn.Read(data)
if err != nil {
if opErr, ok := err.(*net.OpError); ok {
l, readDataErr := conn.Read(data)
if readDataErr != nil {
if opErr, ok := readDataErr.(*net.OpError); ok {
slog.Error(fmt.Sprintf("TCP客户端[rAddr:%s]读取数据异常连接可能断开:", rAddr), opErr)
client.conning = false
readErr(err)
}
if err == io.EOF {
readErr(readDataErr)
} else if err == io.EOF {
slog.Warn(fmt.Sprintf("TCP客户端[rAddr:%s]断开连接:", rAddr))
client.conning = false
readErr(err)
}
return
}
client.conning = true
handler(l, data)
@ -72,12 +74,12 @@ func (c *TcpClient) IsConning() bool {
func (c *TcpClient) Send(data []byte) error {
if c == nil || c.conn == nil {
slog.Error("tcp client send error,conn is nil")
return fmt.Errorf("TCP未连接车载PC仿真")
//slog.Error("tcp client send error,conn is nil")
return fmt.Errorf("tcp client send error,conn is nil")
}
_, err := c.conn.Write(data)
if err != nil {
slog.Error("tcp client send error", "error", err)
//slog.Error("tcp client send error", "error", err)
return err
}
return nil

View File

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

View File

@ -64,7 +64,7 @@ type trainPcReciverData struct {
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
if rd.clientKey == "B" {
@ -248,7 +248,7 @@ func (d *trainPcSimService) initConn(clientKey string) {
cfg, _ := d.findConfig(clientKey)
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 {
slog.Error("车载pc连接失败 clientKey:", clientKey, "error:", err.Error())
d.updateState(tpapi.ThirdPartyState_Broken)

View File

@ -153,7 +153,7 @@ func TrainConnTypeUpdate(vs *VerifySimulation, ct *dto.TrainConnThirdDto) {
connTypeName := "半实物"
if tmpTrain.ConnState.ConnType == state_proto.TrainConnState_PC_SIM_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"
}
panic(sys_error.New(fmt.Sprintf("列车[%s]已经连接 [%v],此列车无法连接", k, connTypeName)))