diff --git a/api/simulation.go b/api/simulation.go index 388dffc..5904f8f 100644 --- a/api/simulation.go +++ b/api/simulation.go @@ -256,7 +256,10 @@ func turnoutOperation(c *gin.Context) { } simulation := checkDeviceDataAndReturn(req.SimulationId) slog.Info("传入状态参数", "request", req) - memory.HandleTurnoutOperation(simulation, req) + err := memory.HandleTurnoutOperation(simulation, req) + if err != nil { + panic(sys_error.New(fmt.Sprintf("道岔操作失败, %s", err), err)) + } c.JSON(http.StatusOK, "ok") } diff --git a/message_server/sfp_ms.go b/message_server/sfp_ms.go index 6251001..d3e560e 100644 --- a/message_server/sfp_ms.go +++ b/message_server/sfp_ms.go @@ -241,6 +241,7 @@ func (ms *SfpMs) collectTrainStates() ([]*state.TrainState, error) { func (ms *SfpMs) collectTurnoutStates() ([]*state.SwitchState, error) { s := ms.vs uidMap := memory.QueryMapUidMapByType(ms.mapId, &graphicData.Turnout{}) + wd := entity.GetWorldData(s.World) var switchArr []*state.SwitchState for _, u := range uidMap { entry, ok := entity.GetEntityByUid(s.World, u.Uid) @@ -269,10 +270,27 @@ func (ms *SfpMs) collectTurnoutStates() ([]*state.SwitchState, error) { // 挤岔 s.Jc = entry.HasComponent(component.TurnoutFaultJcType) - // if entry.HasComponent(component.Zdj9TwoElectronicType) { - // elec := component.Zdj9TwoElectronicType.Get(entry) - - // } + if entry.HasComponent(component.Zdj9TwoElectronicType) { + elec := component.Zdj9TwoElectronicType.Get(entry) + dcj := component.BitStateType.Get(elec.TDC_DCJ) + s.Dc = dcj.Val + qdc, err := wd.QueryQdBit(component.UidType.Get(elec.TDC_DCJ).Id) + if err == nil { + s.Qdc = qdc + } + fcj := component.BitStateType.Get(elec.TDC_FCJ) + s.Fc = fcj.Val + qfc, err := wd.QueryQdBit(component.UidType.Get(elec.TDC_FCJ).Id) + if err == nil { + s.Qfc = qfc + } + ycj := component.BitStateType.Get(elec.TDC_YCJ) + s.Yc = ycj.Val + qyc, err := wd.QueryQdBit(component.UidType.Get(elec.TDC_YCJ).Id) + if err == nil { + s.Qyc = qyc + } + } switchArr = append(switchArr, s) } return switchArr, nil diff --git a/ts/simulation/wayside/memory/wayside_memory_psd_test.go b/ts/simulation/wayside/memory/wayside_memory_psd_test.go index f7b0679..e274918 100644 --- a/ts/simulation/wayside/memory/wayside_memory_psd_test.go +++ b/ts/simulation/wayside/memory/wayside_memory_psd_test.go @@ -1,15 +1,17 @@ -package memory +package memory_test import ( "fmt" + "testing" + "time" + "joylink.club/bj-rtsts-server/dto/request_proto" "joylink.club/bj-rtsts-server/ts" "joylink.club/bj-rtsts-server/ts/protos/graphicData" + "joylink.club/bj-rtsts-server/ts/simulation/wayside/memory" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/consts" "joylink.club/rtsssimulation/entity" - "testing" - "time" ) // 先创建一个仿真再调 @@ -18,7 +20,7 @@ func TestHandlePsdOperation(t *testing.T) { simId := simInfo.SimulationId var mapId int32 for _, id := range simInfo.MapIds { - if QueryGiType(id) == graphicData.PictureType_StationLayout { + if memory.QueryGiType(id) == graphicData.PictureType_StationLayout { mapId = id } } @@ -26,12 +28,12 @@ func TestHandlePsdOperation(t *testing.T) { fmt.Println("无信号布置图") return } - data := QueryGiData[*graphicData.RtssGraphicStorage](mapId) + data := memory.QueryGiData[*graphicData.RtssGraphicStorage](mapId) deviceId := data.ScreenDoors[0].Common.Id simulation := ts.FindSimulation(simId) wantErr := false - uid := QueryUidByMidAndComId(mapId, deviceId, &graphicData.Turnout{}) + uid := memory.QueryUidByMidAndComId(mapId, deviceId, &graphicData.Turnout{}) entry, _ := entity.GetEntityByUid(simulation.World, uid) asdList := component.AsdListType.Get(entry) psdModel := entity.GetWorldData(simulation.World).Repo.FindPsd(uid) @@ -45,7 +47,7 @@ func TestHandlePsdOperation(t *testing.T) { Operation: request_proto.Psd_Operation(value), } t.Run(name, func(t *testing.T) { - if err := HandlePsdOperation(simulation, req); (err != nil) != wantErr { + if err := memory.HandlePsdOperation(simulation, req); (err != nil) != wantErr { t.Errorf("HandlePsdOperation() error = %v, wantErr %v", err, wantErr) } tick := time.Tick(5 * time.Second) diff --git a/ts/simulation/wayside/memory/wayside_memory_turnout.go b/ts/simulation/wayside/memory/wayside_memory_turnout.go index 37a1e72..0f62fd6 100644 --- a/ts/simulation/wayside/memory/wayside_memory_turnout.go +++ b/ts/simulation/wayside/memory/wayside_memory_turnout.go @@ -15,34 +15,36 @@ import ( ) // 处理道岔操作 -func HandleTurnoutOperation(simulation *VerifySimulation, req *request_proto.TurnoutOperationReq) { +func HandleTurnoutOperation(simulation *VerifySimulation, req *request_proto.TurnoutOperationReq) error { uid := QueryUidByMidAndComId(req.MapId, req.DeviceId, &graphicData.Turnout{}) + var err error switch req.Operation { case request_proto.Turnout_DC: - fi.DriveTurnoutDCOn(simulation.World, uid) + err = fi.DriveTurnoutDCOn(simulation.World, uid) case request_proto.Turnout_CancelDC: - fi.DriveTurnoutDCOff(simulation.World, uid) + err = fi.DriveTurnoutDCOff(simulation.World, uid) case request_proto.Turnout_FC: - fi.DriveTurnoutFCOn(simulation.World, uid) + err = fi.DriveTurnoutFCOn(simulation.World, uid) case request_proto.Turnout_CancelFC: - fi.DriveTurnoutFCOff(simulation.World, uid) + err = fi.DriveTurnoutFCOff(simulation.World, uid) case request_proto.Turnout_SetSB: - fi.SetTurnoutFault(simulation.World, uid, component_proto.Turnout_SB) + err = fi.SetTurnoutFault(simulation.World, uid, component_proto.Turnout_SB) case request_proto.Turnout_CancelSB: - fi.CancelTurnoutFault(simulation.World, uid, component_proto.Turnout_SB) + err = fi.CancelTurnoutFault(simulation.World, uid, component_proto.Turnout_SB) case request_proto.Turnout_SetJC: - fi.SetTurnoutFault(simulation.World, uid, component_proto.Turnout_JC) + err = fi.SetTurnoutFault(simulation.World, uid, component_proto.Turnout_JC) case request_proto.Turnout_CancelJC: - fi.CancelTurnoutFault(simulation.World, uid, component_proto.Turnout_JC) + err = fi.CancelTurnoutFault(simulation.World, uid, component_proto.Turnout_JC) case request_proto.Turnout_ForceDw: - fi.ForceTurnoutDw(simulation.World, uid) + err = fi.ForceTurnoutDw(simulation.World, uid) case request_proto.Turnout_ForceFw: - fi.ForceTurnoutFw(simulation.World, uid) + err = fi.ForceTurnoutFw(simulation.World, uid) case request_proto.Turnout_CancelForce: - fi.CancelTurnoutForce(simulation.World, uid) + err = fi.CancelTurnoutForce(simulation.World, uid) default: panic(fmt.Sprintf("未知的道岔操作:%s", req.Operation)) } + return err } // 获取全部的道岔状态,动力学使用 diff --git a/ts/simulation/wayside/memory/wayside_simulation.go b/ts/simulation/wayside/memory/wayside_simulation.go index bac0681..4a771ab 100644 --- a/ts/simulation/wayside/memory/wayside_simulation.go +++ b/ts/simulation/wayside/memory/wayside_simulation.go @@ -351,7 +351,7 @@ func (s *VerifySimulation) GetInterlockCodes() []*config.InterlockConfig { // 处理接到的联锁消息 func (s *VerifySimulation) HandleInterlockDriverInfo(code string, b []byte) { - for _, m := range s.Repo.CentralizedList() { // 获取继电器地图信息 + for _, m := range s.Repo.CiQcList() { // 获取继电器地图信息 if m.StationId != code { continue } @@ -370,7 +370,7 @@ func (s *VerifySimulation) HandleInterlockDriverInfo(code string, b []byte) { // 采集联锁中的继电器消息 func (s *VerifySimulation) CollectInterlockRelayInfo(code string) *message.InterlockSendMsgPkg { - for _, m := range s.Repo.CentralizedList() { // 获取继电器地图信息 + for _, m := range s.Repo.CiQcList() { // 获取继电器地图信息 if m.StationId != code { continue } @@ -384,7 +384,7 @@ func (s *VerifySimulation) CollectInterlockRelayInfo(code string) *message.Inter } rs := true for _, j := range l.RefRelays { - if j.Position == proto.CjDataItem_Q { + if j.Q { rs = rs && fi.CollectXQCircuitState(s.World, j.RelayId) } else { rs = rs && fi.CollectLXCircuitState(s.World, j.RelayId) @@ -635,7 +635,32 @@ func buildAndRelateElectronicComponent(repo *proto.Repository, relayGi *graphicD centralizedStationId := GenerateElementUid(city, lineId, nil, station) ref := queryCentralizedStationRef(centralizedStationId, repo) ref.CjList = append(ref.CjList, handlerRelayGiCj(uidsMap, centralizedStationId, relayGi.CiCjList)...) + sortQcTable(ref.CjList) ref.QdList = append(ref.QdList, handlerRelayGiQd(uidsMap, centralizedStationId, relayGi.CiQdList)...) + sortQcTable(ref.QdList) +} + +type IQcTable interface { + GetRow() int32 + GetCol() int32 +} + +func sortQcTable[T IQcTable](s []T) { + sort.SliceStable(s, func(i, j int) bool { + a := s[i] + b := s[j] + if a.GetCol() < b.GetCol() { + return true + } else if a.GetCol() > b.GetCol() { + return false + } else { + if a.GetRow() < b.GetRow() { + return true + } else { + return false + } + } + }) } // 查询集中站配置信息 @@ -668,23 +693,21 @@ func handlerRelayGiCj(uidsMap *RelayUidStructure, stationId string, ciCj *graphi index := 0 for ci, col := range ciCj.CjList { for ri, row := range col.BitList { - if len(row.RefRelays) == 0 { - index++ - continue - } r := &proto.CjData{Row: int32(ri), Col: int32(ci)} - for _, j := range row.RefRelays { - u := uidsMap.RelayIds[j.RelayId] - if u == nil { - panic(sys_error.New(fmt.Sprintf("集中站【id:%s】不存在继电器的【comId:%s】UID映射关系", stationId, j.RelayId))) + if len(row.RefRelays) > 0 { + for _, j := range row.RefRelays { + u := uidsMap.RelayIds[j.RelayId] + if u == nil { + panic(sys_error.New(fmt.Sprintf("集中站【id:%s】不存在继电器的【comId:%s】UID映射关系", stationId, j.RelayId))) + } + d := &proto.CjDataItem{RelayId: u.Uid} + if j.Position == graphicData.CjDataItem_H { + d.Q = false + } else { + d.Q = true + } + r.RefRelays = append(r.RefRelays, d) } - d := &proto.CjDataItem{RelayId: u.Uid} - if j.Position == graphicData.CjDataItem_H { - d.Position = proto.CjDataItem_H - } else { - d.Position = proto.CjDataItem_Q - } - r.RefRelays = append(r.RefRelays, d) } cjList[index] = r index++ @@ -707,17 +730,15 @@ func handlerRelayGiQd(uidsMap *RelayUidStructure, stationId string, ciQd *graphi index := 0 for ci, col := range ciQd.QdList { for ri, row := range col.BitList { - if len(row.RefRelays) == 0 { - index++ - continue - } r := &proto.QdData{Row: int32(ri), Col: int32(ci)} - for _, j := range row.RefRelays { - u := uidsMap.RelayIds[j] - if u == nil { - panic(sys_error.New(fmt.Sprintf("集中站【id:%s】不存在继电器的【comId:%s】UID映射关系", stationId, j))) + if len(row.RefRelays) > 0 { + for _, j := range row.RefRelays { + u := uidsMap.RelayIds[j] + if u == nil { + panic(sys_error.New(fmt.Sprintf("集中站【id:%s】不存在继电器的【comId:%s】UID映射关系", stationId, j))) + } + r.RefRelays = append(r.RefRelays, u.Uid) } - r.RefRelays = append(r.RefRelays, u.Uid) } qdList[index] = r index++ diff --git a/ts/test_simulation_manage.go b/ts/test_simulation_manage.go index 9a30c9a..7733aec 100644 --- a/ts/test_simulation_manage.go +++ b/ts/test_simulation_manage.go @@ -1,6 +1,7 @@ package ts import ( + "fmt" "log/slog" "runtime" "strconv" @@ -85,7 +86,7 @@ func initWorld(s *memory.VerifySimulation) error { //创建仿真 w, err := rtss_simulation.NewSimulation(s.Repo) if err != nil { - return sys_error.New("仿真创建失败", err) + return sys_error.New(fmt.Sprintf("仿真创建失败: %s", err), err) } s.World = w // 保证World关闭