2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
2023-10-26 17:16:07 +08:00
|
|
|
"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"
|
2023-10-27 14:57:37 +08:00
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
2023-10-26 16:41:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 综合后备盘IBP消息服务
|
|
|
|
type IbpMs struct {
|
|
|
|
vs *memory.VerifySimulation
|
|
|
|
mapId int32
|
|
|
|
}
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
func NewIBPMs(vs *memory.VerifySimulation, mapId int32) *IbpMs {
|
|
|
|
return &IbpMs{vs: vs, mapId: mapId}
|
|
|
|
}
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
func (ms *IbpMs) GetChannel() string {
|
2023-11-17 18:04:38 +08:00
|
|
|
return SimulationIbpTopic
|
2023-11-17 17:01:12 +08:00
|
|
|
// return "simulation-ibp-%s_%d_%s-status"
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *IbpMs) GetInterval() time.Duration {
|
|
|
|
return 200 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *IbpMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
|
|
|
var msgArr []*ms_api.TopicMsg
|
|
|
|
mapData := memory.QueryGiData[*graphicData.RtssGraphicStorage](ms.mapId)
|
|
|
|
for _, station := range mapData.Stations {
|
2023-10-27 14:57:37 +08:00
|
|
|
channel := ms.handlerIBPChannelName(station.Common.Id)
|
2023-10-26 16:41:18 +08:00
|
|
|
stationIbpState, err := ms.collectStationIbpState(station)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(stationIbpState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("IBP设备状态消息服务数据序列化失败: %s", err)
|
|
|
|
}
|
|
|
|
msgArr = append(msgArr, ms_api.NewTopicMsg(channel, b))
|
|
|
|
}
|
|
|
|
return msgArr, nil
|
|
|
|
}
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
// 当发生错误时执行的逻辑
|
|
|
|
func (ms *IbpMs) OnError(err error) {
|
|
|
|
}
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
func (ms *IbpMs) collectStationIbpState(station *graphicData.Station) (*state.PushedDevicesStatus, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
if station.RefIbpMapCode == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
stationUid := memory.QueryUidByMidAndComId(ms.mapId, station.Common.Id, &graphicData.Station{})
|
2023-12-07 10:17:48 +08:00
|
|
|
ibpMapId, ibpStorage := memory.GetStorageIBPMapData(station.RefIbpMapCode)
|
|
|
|
ibpUidsMap := memory.QueryUidStructure[*memory.IBPUidStructure](ibpMapId)
|
|
|
|
buttonStates, err := ms.collectIBPButtonState(stationUid, ibpUidsMap, ibpStorage.IbpButtons)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-07 10:17:48 +08:00
|
|
|
alarmStates, err := ms.collectIBPAlarmState(stationUid, ibpUidsMap, ibpStorage.IbpAlarms)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-07 10:17:48 +08:00
|
|
|
lightStates, err := ms.collectIBPLightState(stationUid, ibpUidsMap, ibpStorage.IbpLights)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-07 10:17:48 +08:00
|
|
|
keyStates, err := ms.collectIBPKeyState(stationUid, ibpUidsMap, ibpStorage.IbpKeys)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &state.PushedDevicesStatus{
|
|
|
|
All: true,
|
|
|
|
AllStatus: &state.AllDevicesStatus{
|
|
|
|
ButtonState: buttonStates,
|
|
|
|
AlarmState: alarmStates,
|
|
|
|
LightState: lightStates,
|
|
|
|
KeyState: keyStates,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集IBP按钮状态
|
2023-12-07 10:17:48 +08:00
|
|
|
func (ms *IbpMs) collectIBPButtonState(stationUid string, uidsMap *memory.IBPUidStructure, ibpButtons []*graphicData.IBPButton) ([]*state.ButtonState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
var states []*state.ButtonState
|
|
|
|
for _, data := range ibpButtons { // 按钮
|
2023-12-07 10:17:48 +08:00
|
|
|
uid := stationUid + "_" + uidsMap.IbpButtonIds[data.Common.Id].Uid
|
|
|
|
entry, ok := entity.GetEntityByUid(ms.vs.World, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:17:48 +08:00
|
|
|
return nil, fmt.Errorf("ibp按钮实体不存在: World id=%d, uid=%s", ms.vs.World.Id(), uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.ButtonTag) {
|
|
|
|
states = append(states, getIBPButtonState(data.Common.Id, entry))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取IBP盘按钮状态
|
|
|
|
func getIBPButtonState(id string, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集报警器状态
|
2023-12-07 10:17:48 +08:00
|
|
|
func (ms *IbpMs) collectIBPAlarmState(stationUid string, uidsMap *memory.IBPUidStructure, ibpAlarms []*graphicData.IbpAlarm) ([]*state.AlarmState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
var states []*state.AlarmState
|
|
|
|
for _, data := range ibpAlarms { // 报警器
|
2023-12-07 10:17:48 +08:00
|
|
|
uid := stationUid + "_" + uidsMap.IbpAlarmIds[data.Common.Id].Uid
|
|
|
|
entry, ok := entity.GetEntityByUid(ms.vs.World, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:17:48 +08:00
|
|
|
return nil, fmt.Errorf("ibp报警器实体不存在: World id=%d, uid=%s", ms.vs.World.Id(), uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.AlarmTag) {
|
|
|
|
states = append(states, &state.AlarmState{Id: data.Common.Id, Active: component.BitStateType.Get(entry).Val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集灯状态信息
|
2023-12-07 10:17:48 +08:00
|
|
|
func (ms *IbpMs) collectIBPLightState(stationUid string, uidsMap *memory.IBPUidStructure, ibpLights []*graphicData.IbpLight) ([]*state.LightState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
var states []*state.LightState
|
|
|
|
for _, data := range ibpLights { // 指示灯
|
2023-12-07 10:17:48 +08:00
|
|
|
uid := stationUid + "_" + uidsMap.IbpLightIds[data.Common.Id].Uid
|
|
|
|
entry, ok := entity.GetEntityByUid(ms.vs.World, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:17:48 +08:00
|
|
|
return nil, fmt.Errorf("ibp指示灯实体不存在: World id=%d, uid=%s", ms.vs.World.Id(), uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.LightTag) {
|
|
|
|
states = append(states, &state.LightState{Id: data.Common.Id, Active: component.BitStateType.Get(entry).Val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集钥匙状态
|
2023-12-07 10:17:48 +08:00
|
|
|
func (ms *IbpMs) collectIBPKeyState(stationUid string, uidsMap *memory.IBPUidStructure, ibpKeys []*graphicData.IbpKey) ([]*state.KeyState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
var states []*state.KeyState
|
|
|
|
for _, data := range ibpKeys { // 钥匙
|
2023-12-07 10:17:48 +08:00
|
|
|
uid := stationUid + "_" + uidsMap.IbpKeyIds[data.Common.Id].Uid
|
|
|
|
entry, ok := entity.GetEntityByUid(ms.vs.World, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:17:48 +08:00
|
|
|
return nil, fmt.Errorf("ibp钥匙实体不存在: World id=%d, uid=%s", ms.vs.World.Id(), uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.KeyTag) {
|
|
|
|
states = append(states, &state.KeyState{Id: data.Common.Id, Gear: component.GearStateType.Get(entry).Val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 处理订阅通道名称
|
2023-10-27 14:57:37 +08:00
|
|
|
func (ms *IbpMs) handlerIBPChannelName(stationId string) string {
|
2023-11-17 18:04:38 +08:00
|
|
|
return fmt.Sprintf(SimulationIbpTopic, ms.vs.SimulationId, ms.mapId, stationId)
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|