package message_server import ( "fmt" "time" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" "google.golang.org/protobuf/proto" "joylink.club/bj-rtsts-server/message_server/ms_api" "joylink.club/bj-rtsts-server/ts/protos/graphicData" "joylink.club/bj-rtsts-server/ts/protos/state" "joylink.club/bj-rtsts-server/ts/simulation/wayside/memory" ) // PSL或门控箱消息服务 type PslMs struct { vs *memory.VerifySimulation mapId int32 } func NewPSLMs(vs *memory.VerifySimulation, mapId int32) *PslMs { return &PslMs{vs: vs, mapId: mapId} } func (p *PslMs) GetChannel() string { return SimulationPslTopic // return "simulation-psl-%s_%d_%s-status" } func (p *PslMs) GetInterval() time.Duration { return 200 * time.Millisecond } func (p *PslMs) OnTick() ([]*ms_api.TopicMsg, error) { var msgArr []*ms_api.TopicMsg mapData := memory.QueryGiData[*graphicData.RtssGraphicStorage](p.mapId) for _, box := range mapData.GateBoxs { did := memory.GetMapElementId(box.Common) channel := p.handlerPSLChannelName(did) state, err := p.collectGateBoxPSLState(box) if err != nil { return nil, err } b, err := proto.Marshal(state) if err != nil { return nil, fmt.Errorf("PSL设备状态消息服务数据序列化失败: %s", err) } msgArr = append(msgArr, ms_api.NewTopicMsg(channel, b)) } return msgArr, nil } func (p *PslMs) OnError(err error) {} // 处理订阅通道名称 func (p *PslMs) handlerPSLChannelName(gateBoxId uint32) string { return fmt.Sprintf(SimulationPslTopic, p.vs.SimulationId, p.mapId, gateBoxId) } func (p *PslMs) collectGateBoxPSLState(box *graphicData.GatedBox) (*state.PushedDevicesStatus, error) { world := p.vs.World did := memory.GetMapElementId(box.Common) uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](p.mapId) boxUid := uidStructure.GateBoxIds[did].Uid mkxEntry, ok := entity.GetEntityByUid(world, boxUid) if !ok { return nil, fmt.Errorf("[id:%s]的门控箱实体找不到", boxUid) } mkx := component.MkxType.Get(mkxEntry) var buttonStateArr []*state.ButtonState if ok { pslMapId := memory.QueryGiId(box.RefGatedBoxMapCode) pslStorage := memory.QueryGiData[*graphicData.PslGraphicStorage](pslMapId) btnUidMap := make(map[string]uint32, len(pslStorage.PslButtons)) for _, button := range pslStorage.PslButtons { btnUidMap[boxUid+"_"+button.Code] = memory.GetMapElementId(button.Common) } btnArr := []*ecs.Entry{mkx.PCB, mkx.POB, mkx.PAB} for _, btn := range btnArr { if btn == nil { continue } buttonStateArr = append(buttonStateArr, &state.ButtonState{ Id: btnUidMap[component.UidType.Get(btn).Id], Down: component.BitStateType.Get(btn).Val, Active: component.BitStateType.Get(btn).Val, }) } } return &state.PushedDevicesStatus{ All: true, AllStatus: &state.AllDevicesStatus{ ButtonState: buttonStateArr, }, }, nil }