rts-sim-testing-service/message_server/psl_ms.go

90 lines
2.4 KiB
Go
Raw Normal View History

package message_server
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"time"
"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 "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 {
channel := p.handlerPSLChannelName(box.Common.Id)
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 string) string {
return fmt.Sprintf(p.GetChannel(), p.vs.SimulationId, p.mapId, gateBoxId)
}
func (p *PslMs) collectGateBoxPSLState(box *graphicData.GatedBox) (*state.PushedDevicesStatus, error) {
world := p.vs.World
uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](p.mapId)
boxUid := uidStructure.GateBoxIds[box.Common.Id].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 {
btnArr := []*ecs.Entry{mkx.PCB, mkx.POB, mkx.PAB}
for _, btn := range btnArr {
if btn == nil {
continue
}
buttonStateArr = append(buttonStateArr, &state.ButtonState{
Id: 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
}