rts-sim-testing-service/message_server/ibp_ms.go
2023-12-25 14:15:22 +08:00

147 lines
4.9 KiB
Go

package message_server
import (
"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"
)
// 综合后备盘IBP消息服务
func NewIBPMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
mapData := memory.QueryGiData[*graphicData.RtssGraphicStorage](mapId)
return ms_api.NewScheduleTask("综合后备盘IBP", func() error {
for _, station := range mapData.Stations {
sid := memory.GetMapElementId(station.Common)
stationIbpState, err := collectStationIbpState(mapId, vs.World, station)
if err != nil {
return err
}
mqtt.GetMsgClient().PubIBPState(vs.SimulationId, mapId, sid, stationIbpState)
}
return nil
}, 200*time.Millisecond)
}
func collectStationIbpState(mapId int32, world ecs.World, station *graphicData.Station) (*state.PushedDevicesStatus, error) {
if station.RefIbpMapCode == "" {
return nil, nil
}
sid := memory.GetMapElementId(station.Common)
stationUid := memory.QueryUidByMidAndComId(mapId, sid, &graphicData.Station{})
ibpMapId, ibpStorage := memory.QueryGiDataByName[*graphicData.IBPGraphicStorage](station.RefIbpMapCode)
ibpUidsMap := memory.QueryUidStructure[*memory.IBPUidStructure](ibpMapId)
buttonStates, err := collectIBPButtonState(world, stationUid, ibpUidsMap, ibpStorage.IbpButtons)
if err != nil {
return nil, err
}
alarmStates, err := collectIBPAlarmState(world, stationUid, ibpUidsMap, ibpStorage.IbpAlarms)
if err != nil {
return nil, err
}
lightStates, err := collectIBPLightState(world, stationUid, ibpUidsMap, ibpStorage.IbpLights)
if err != nil {
return nil, err
}
keyStates, err := collectIBPKeyState(world, stationUid, ibpUidsMap, ibpStorage.IbpKeys)
if err != nil {
return nil, err
}
return &state.PushedDevicesStatus{
All: true,
AllStatus: &state.AllDevicesStatus{
ButtonState: buttonStates,
AlarmState: alarmStates,
LightState: lightStates,
KeyState: keyStates,
},
}, nil
}
// 收集IBP按钮状态
func collectIBPButtonState(world ecs.World, stationUid string, uidsMap *memory.IBPUidStructure, ibpButtons []*graphicData.IBPButton) ([]*state.ButtonState, error) {
var states []*state.ButtonState
for _, data := range ibpButtons { // 按钮
did := memory.GetMapElementId(data.Common)
uid := stationUid + "_" + uidsMap.IbpButtonIds[did].Uid
entry, ok := entity.GetEntityByUid(world, uid)
if !ok {
continue
}
if entry.HasComponent(component.ButtonTag) {
states = append(states, getIBPButtonState(did, entry))
}
}
return states, nil
}
// 获取IBP盘按钮状态
func getIBPButtonState(id uint32, entry *ecs.Entry) *state.ButtonState {
status := &state.ButtonState{Id: id}
bit := component.BitStateType.Get(entry)
status.Down = bit.Val
// 如果按钮包含灯
if entry.HasComponent(component.SingleLightType) {
lightComponent := component.SingleLightType.Get(entry)
status.Active = component.BitStateType.Get(lightComponent.Light).Val
}
return status
}
// 收集报警器状态
func collectIBPAlarmState(world ecs.World, stationUid string, uidsMap *memory.IBPUidStructure, ibpAlarms []*graphicData.IbpAlarm) ([]*state.AlarmState, error) {
var states []*state.AlarmState
for _, data := range ibpAlarms { // 报警器
did := memory.GetMapElementId(data.Common)
uid := stationUid + "_" + uidsMap.IbpAlarmIds[did].Uid
entry, ok := entity.GetEntityByUid(world, uid)
if !ok {
continue
}
if entry.HasComponent(component.AlarmTag) {
states = append(states, &state.AlarmState{Id: did, Active: component.BitStateType.Get(entry).Val})
}
}
return states, nil
}
// 收集灯状态信息
func collectIBPLightState(world ecs.World, stationUid string, uidsMap *memory.IBPUidStructure, ibpLights []*graphicData.IbpLight) ([]*state.LightState, error) {
var states []*state.LightState
for _, data := range ibpLights { // 指示灯
did := memory.GetMapElementId(data.Common)
uid := stationUid + "_" + uidsMap.IbpLightIds[did].Uid
entry, ok := entity.GetEntityByUid(world, uid)
if !ok {
continue
}
if entry.HasComponent(component.LightTag) {
states = append(states, &state.LightState{Id: did, Active: component.BitStateType.Get(entry).Val})
}
}
return states, nil
}
// 收集钥匙状态
func collectIBPKeyState(world ecs.World, stationUid string, uidsMap *memory.IBPUidStructure, ibpKeys []*graphicData.IbpKey) ([]*state.KeyState, error) {
var states []*state.KeyState
for _, data := range ibpKeys { // 钥匙
did := memory.GetMapElementId(data.Common)
uid := stationUid + "_" + uidsMap.IbpKeyIds[did].Uid
entry, ok := entity.GetEntityByUid(world, uid)
if !ok {
continue
}
if entry.HasComponent(component.KeyTag) {
states = append(states, &state.KeyState{Id: did, Gear: component.GearStateType.Get(entry).Val})
}
}
return states, nil
}