diff --git a/third_party/tcp/tcp_client.go b/third_party/tcp/tcp_client.go index 55a92d9..ef55d4a 100644 --- a/third_party/tcp/tcp_client.go +++ b/third_party/tcp/tcp_client.go @@ -50,7 +50,7 @@ func StartTcpClient(rAddr string, handler func(n int, data []byte), readErr func return &TcpClient{conn: conn, ctx: ctxFun}, nil } func (c *TcpClient) Close() { - if c.conn != nil { + if c != nil && c.conn != nil { slog.Info(fmt.Sprintf("TCP客户端[rAddr:%s]关闭连接", c.conn.RemoteAddr().String())) c.ctx() c.conn.Close() diff --git a/third_party/train_pc_sim/train_pc_sim.go b/third_party/train_pc_sim/train_pc_sim.go index 370841b..e630e6e 100644 --- a/third_party/train_pc_sim/train_pc_sim.go +++ b/third_party/train_pc_sim/train_pc_sim.go @@ -23,39 +23,39 @@ type TrainControlEvent struct { var FireTrainControlEventType = ecs.NewEventType[TrainControlEvent]() type TrainPcSim interface { - Start(wd ecs.World, pcSimManage TrainPcSimManage) + tpapi.ThirdPartyApiService + Start(pcSimManage TrainPcSimManage) Stop() - //发送驾驶端激活 + // SendDriverActive 发送驾驶端激活 SendDriverActive(tc *state_proto.TrainConnState, vobc *state_proto.TrainVobcState) - //发送牵引制动手柄 + // SendHandleSwitch 发送牵引制动手柄 SendHandleSwitch(oldTraction, oldBrakeForce int64, tractionState bool, tc *state_proto.TrainConnState, vobc *state_proto.TrainVobcState) - //列车运行方向 + // SendTrainDirection 列车运行方向 //因文档说明不清楚,在调用的时候目前是注释状态,现场调试可能会用到 SendTrainDirection(trainForward, trainBackward bool) //发送应答器信息数据 SendBaliseData(msgType uint16, data []byte) //发布列车控制的相关事件 - PublishTrainControlEvent(world ecs.World, events []TrainControlEvent) - + PublishTrainControlEvent(events []TrainControlEvent) + // CreateOrRemoveSpeedPLace 创建或删除速度位置信息 CreateOrRemoveSpeedPLace(train *state_proto.TrainState) - + // CreateOrRemoveTrain 创建或删除列车 CreateOrRemoveTrain(msgType byte, data []byte) error - tpapi.ThirdPartyApiService } type TrainPcSimManage interface { GetTrainPcSimConfig() config.VehiclePCSimConfig GetConnTrain() *state_proto.TrainState - //4.4.1. 车载输出数字量信息报文内容 + // TrainPcSimDigitalOutInfoHandle 4.4.1. 车载输出数字量信息报文内容 TrainPcSimDigitalOutInfoHandle(data []byte) - //4.4.2. 车载输出数字反馈量信息报文内容 + // TrainPcSimDigitalReportHandle 4.4.2. 车载输出数字反馈量信息报文内容 TrainPcSimDigitalReportHandle(data []byte) - //门模式 + // TrainPcSimMockInfo 门模式 //TrainDoorModeHandle(state byte) //处理列车pc仿真模拟量数据 TrainPcSimMockInfo(data []byte) - //处理列车btm查询 + // TrainBtmQuery 处理列车btm查询 TrainBtmQuery(data []byte) } @@ -111,6 +111,7 @@ func (d *trainPcSimService) connTrainPcSim() { return default: } + d.pcSimClient.Close() client, err := tcp.StartTcpClient(fmt.Sprintf("%v:%v", d.config.PcSimIp, d.config.PcSimPort), d.reivceData, d.readError) if err != nil { reconnIndex++ @@ -128,7 +129,7 @@ func (d *trainPcSimService) connTrainPcSim() { }() } -func (d *trainPcSimService) Start(wd ecs.World, pcSimManage TrainPcSimManage) { +func (d *trainPcSimService) Start(pcSimManage TrainPcSimManage) { config := pcSimManage.GetTrainPcSimConfig() if config.PcSimIp == "" || !config.Open { slog.Info("车载pc仿真配置未开启") @@ -266,14 +267,16 @@ func (d *trainPcSimService) SendBaliseData(msgType uint16, data []byte) { d.pcSimClient.Send(msg.Encode()) } -func (d *trainPcSimService) trainControlEventHandle(w ecs.World, event TrainControlEvent) { - msg := &message.TrainPcSimBaseMessage{} - msg.Type = SENDER_TRAIN_OUTR_INFO - data := []byte{event.Command, event.Status} - msg.Data = data - d.pcSimClient.Send(msg.Encode()) -} -func (d *trainPcSimService) PublishTrainControlEvent(world ecs.World, events []TrainControlEvent) { +/* + func (d *trainPcSimService) trainControlEventHandle( event TrainControlEvent) { + msg := &message.TrainPcSimBaseMessage{} + msg.Type = SENDER_TRAIN_OUTR_INFO + data := []byte{event.Command, event.Status} + msg.Data = data + d.pcSimClient.Send(msg.Encode()) + } +*/ +func (d *trainPcSimService) PublishTrainControlEvent(events []TrainControlEvent) { if len(events) <= 0 { slog.Warn("发布事件数量为空") return diff --git a/ts/simulation/wayside/memory/wayside_memory_train.go b/ts/simulation/wayside/memory/wayside_memory_train.go index 67ba4bd..6b6b970 100644 --- a/ts/simulation/wayside/memory/wayside_memory_train.go +++ b/ts/simulation/wayside/memory/wayside_memory_train.go @@ -150,15 +150,18 @@ func TrainConnTypeUpdate(vs *VerifySimulation, ct *dto.TrainConnThirdDto) { return true }) } + train.ConnState.Conn = true + train.ConnState.ConnType = ct.ConnType if ct.ConnType == state_proto.TrainConnState_PC_SIM { err := TrainPcSimConnOrRemoveHandle(train) if err != nil { - panic(sys_error.New("连接车载PC仿真失败")) + train.ConnState.Conn = false + train.ConnState.ConnType = state_proto.TrainConnState_NONE + panic(sys_error.New(err.Error())) } train_pc_sim.Default().SendDriverActive(train.ConnState, train.VobcState) } - train.ConnState.Conn = true - train.ConnState.ConnType = ct.ConnType + } // 列车断开三方连接 @@ -405,12 +408,6 @@ func RemoveAllTrain(vs *VerifySimulation) { allTrainMap.Range(func(k any, t any) bool { id := k.(string) RemoveTrainState(vs, id) - //train := t.(*state_proto.TrainState) - //err := removeTrain(vs, train.Id, train) - //if err != nil { - // slog.Error("列车id:", train.Id, "移除失败,原因:", err.Error()) - //} - //allTrainMap.Store(train.Id, t) return true }) } @@ -423,10 +420,17 @@ func removeTrain(vs *VerifySimulation, trainId string, train *state_proto.TrainS if err != nil { return err } - train.Show = false if train.ConnState.Conn { - train.ConnState.ConnType = state_proto.TrainConnState_NONE + train.ConnState.Conn = false + err = TrainPcSimConnOrRemoveHandle(train) + if err != nil { + train.ConnState.Conn = true + return err + } } + + train.Show = false + train.ConnState.ConnType = state_proto.TrainConnState_NONE return fi.RemoveTrainFromWorld(vs.World, trainId) } diff --git a/ts/simulation/wayside/memory/wayside_simulation_train_pc.go b/ts/simulation/wayside/memory/wayside_simulation_train_pc.go index 2a86dd8..68528e9 100644 --- a/ts/simulation/wayside/memory/wayside_simulation_train_pc.go +++ b/ts/simulation/wayside/memory/wayside_simulation_train_pc.go @@ -54,7 +54,7 @@ func ControlTrainUpdate(s *VerifySimulation, ct *request_proto.TrainControl) { } if sta.ConnState.Conn && sta.ConnState.ConnType == state_proto.TrainConnState_PC_SIM && tce != nil { - train_pc_sim.Default().PublishTrainControlEvent(s.World, tce) + train_pc_sim.Default().PublishTrainControlEvent(tce) } } @@ -386,15 +386,18 @@ func (s *VerifySimulation) TrainPcSimDigitalReportHandle(data []byte) { // 创建/删除列车 func TrainPcSimConnOrRemoveHandle(train *state_proto.TrainState) error { + var data byte = 0x01 if train.ConnState.Conn == false { data = 0x00 } - crErr := train_pc_sim.Default().CreateOrRemoveTrain(train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE, []byte{data}) - if crErr != nil { - return crErr + if train.ConnState.ConnType == state_proto.TrainConnState_PC_SIM { + crErr := train_pc_sim.Default().CreateOrRemoveTrain(train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE, []byte{data}) + if crErr != nil { + return crErr + } + train_pc_sim.Default().CreateOrRemoveSpeedPLace(train) } - train_pc_sim.Default().CreateOrRemoveSpeedPLace(train) return nil } diff --git a/ts/test_simulation_manage.go b/ts/test_simulation_manage.go index e4361bc..9815469 100644 --- a/ts/test_simulation_manage.go +++ b/ts/test_simulation_manage.go @@ -137,7 +137,7 @@ func runThirdParty(s *memory.VerifySimulation) error { radar.Default().Start(s) //列车加速计发送vobc acc.Default().Start(s) - train_pc_sim.Default().Start(s.World, s) + train_pc_sim.Default().Start(s) return nil }