rts-sim-testing-service/message_server/psl_ms.go
2023-12-26 13:27:09 +08:00

67 lines
2.2 KiB
Go

package message_server
import (
"fmt"
"time"
"joylink.club/bj-rtsts-server/message_server/ms_api"
"joylink.club/bj-rtsts-server/mqtt"
"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"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
func NewPSLMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
mapData := memory.QueryGiData[*graphicData.RtssGraphicStorage](mapId)
return ms_api.NewScheduleTask(fmt.Sprintf("地图[%d]综合门控箱按钮状态", mapId), func() error {
for _, box := range mapData.GateBoxs {
did := memory.GetMapElementId(box.Common)
state, err := collectGateBoxPSLState(vs.World, mapId, box)
if err != nil {
return err
}
mqtt.GetMsgClient().PubPSLState(vs.SimulationId, mapId, did, state)
}
return nil
}, 200*time.Millisecond)
}
func collectGateBoxPSLState(world ecs.World, mapId int32, box *graphicData.GatedBox) (*state.PushedDevicesStatus, error) {
did := memory.GetMapElementId(box.Common)
uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](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 {
_, pslStorage := memory.QueryGiDataByName[*graphicData.PslGraphicStorage](box.RefGatedBoxMapCode)
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
}