From 1130277693bd463db5a7a10a2a006026a03df5aa Mon Sep 17 00:00:00 2001 From: walker Date: Fri, 3 Nov 2023 17:27:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=81=94=E9=94=81=E9=A9=B1?= =?UTF-8?q?=E9=87=87=E6=95=B0=E6=8D=AE=E5=AE=9A=E4=B9=89=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=81=93=E5=B2=94=E6=93=8D=E4=BD=9C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20=E5=AE=8C=E5=96=84=E9=81=93=E5=B2=94=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/simulation.go | 5 +- message_server/sfp_ms.go | 26 ++++++- .../wayside/memory/wayside_memory_psd_test.go | 16 ++-- .../wayside/memory/wayside_memory_turnout.go | 26 ++++--- .../wayside/memory/wayside_simulation.go | 75 ++++++++++++------- ts/test_simulation_manage.go | 3 +- 6 files changed, 99 insertions(+), 52 deletions(-) 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关闭