2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
|
|
|
import (
|
2023-12-26 13:27:09 +08:00
|
|
|
"fmt"
|
2024-01-11 10:24:56 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto/data_proto"
|
2024-02-20 14:18:42 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-10-26 16:41:18 +08:00
|
|
|
"time"
|
|
|
|
|
2024-01-11 10:24:56 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto/state_proto"
|
2023-10-26 16:41:18 +08:00
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
2023-12-20 10:37:54 +08:00
|
|
|
"joylink.club/bj-rtsts-server/mqtt"
|
2023-10-26 17:16:07 +08:00
|
|
|
"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消息服务
|
2023-12-20 10:37:54 +08:00
|
|
|
func NewIBPMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
|
2024-01-11 10:24:56 +08:00
|
|
|
mapData := memory.QueryGiData[*data_proto.RtssGraphicStorage](mapId)
|
2023-12-26 13:27:09 +08:00
|
|
|
return ms_api.NewScheduleTask(fmt.Sprintf("地图[%d]综合后备盘IBP", mapId), func() error {
|
2024-02-20 14:18:42 +08:00
|
|
|
for _, ibpBox := range mapData.IbpBoxs {
|
|
|
|
ibpState, err := collectIbpState(mapId, vs.World, ibpBox)
|
2023-12-20 10:37:54 +08:00
|
|
|
if err != nil {
|
2024-02-20 14:18:42 +08:00
|
|
|
return sys_error.New("IBP状态发送异常", err)
|
2023-12-20 10:37:54 +08:00
|
|
|
}
|
2024-02-20 14:18:42 +08:00
|
|
|
mqtt.GetMsgClient().PubIBPState(vs.SimulationId, mapId, ibpBox.Common.Id, ibpState)
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
2023-12-20 10:37:54 +08:00
|
|
|
return nil
|
|
|
|
}, 200*time.Millisecond)
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
|
|
|
|
2024-02-20 14:18:42 +08:00
|
|
|
func collectIbpState(mapId int32, world ecs.World, ibpBox *data_proto.IbpBox) (*state_proto.PushedDevicesStatus, error) {
|
|
|
|
ibpBoxUid := memory.QueryUidByMidAndComId(mapId, ibpBox.Common.Id, &data_proto.IbpBox{})
|
|
|
|
ibpMapId, ibpStorage := memory.QueryGiDataByName[*data_proto.IBPGraphicStorage](ibpBox.RefIbpMapCode)
|
2023-12-07 10:17:48 +08:00
|
|
|
ibpUidsMap := memory.QueryUidStructure[*memory.IBPUidStructure](ibpMapId)
|
2024-02-20 14:18:42 +08:00
|
|
|
buttonStates, err := collectIBPButtonState(world, ibpBoxUid, ibpUidsMap, ibpStorage.IbpButtons)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-20 14:18:42 +08:00
|
|
|
alarmStates, err := collectIBPAlarmState(world, ibpBoxUid, ibpUidsMap, ibpStorage.IbpAlarms)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-20 14:18:42 +08:00
|
|
|
lightStates, err := collectIBPLightState(world, ibpBoxUid, ibpUidsMap, ibpStorage.IbpLights)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-20 14:18:42 +08:00
|
|
|
keyStates, err := collectIBPKeyState(world, ibpBoxUid, ibpUidsMap, ibpStorage.IbpKeys)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-11 10:24:56 +08:00
|
|
|
return &state_proto.PushedDevicesStatus{
|
2023-10-27 14:57:37 +08:00
|
|
|
All: true,
|
2024-01-11 10:24:56 +08:00
|
|
|
AllStatus: &state_proto.AllDevicesStatus{
|
2023-10-27 14:57:37 +08:00
|
|
|
ButtonState: buttonStates,
|
|
|
|
AlarmState: alarmStates,
|
|
|
|
LightState: lightStates,
|
|
|
|
KeyState: keyStates,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集IBP按钮状态
|
2024-02-20 14:18:42 +08:00
|
|
|
func collectIBPButtonState(world ecs.World, ibpBoxUid string, uidsMap *memory.IBPUidStructure, ibpButtons []*data_proto.IBPButton) ([]*state_proto.ButtonState, error) {
|
2024-01-11 10:24:56 +08:00
|
|
|
var states []*state_proto.ButtonState
|
2023-10-27 14:57:37 +08:00
|
|
|
for _, data := range ibpButtons { // 按钮
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(data.Common)
|
2024-02-20 14:18:42 +08:00
|
|
|
uid := ibpBoxUid + "_" + uidsMap.IbpButtonIds[did].Uid
|
2023-12-20 10:37:54 +08:00
|
|
|
entry, ok := entity.GetEntityByUid(world, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:43:20 +08:00
|
|
|
continue
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.ButtonTag) {
|
2023-12-13 15:05:42 +08:00
|
|
|
states = append(states, getIBPButtonState(did, entry))
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取IBP盘按钮状态
|
2024-01-11 10:24:56 +08:00
|
|
|
func getIBPButtonState(id uint32, entry *ecs.Entry) *state_proto.ButtonState {
|
|
|
|
status := &state_proto.ButtonState{Id: id}
|
2023-10-27 14:57:37 +08:00
|
|
|
bit := component.BitStateType.Get(entry)
|
|
|
|
status.Down = bit.Val
|
2024-02-06 10:49:46 +08:00
|
|
|
//status.Bypass = bit.BypassEnable
|
2023-10-27 14:57:37 +08:00
|
|
|
// 如果按钮包含灯
|
|
|
|
if entry.HasComponent(component.SingleLightType) {
|
|
|
|
lightComponent := component.SingleLightType.Get(entry)
|
|
|
|
status.Active = component.BitStateType.Get(lightComponent.Light).Val
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集报警器状态
|
2024-02-20 14:18:42 +08:00
|
|
|
func collectIBPAlarmState(world ecs.World, ibpBoxUid string, uidsMap *memory.IBPUidStructure, ibpAlarms []*data_proto.IbpAlarm) ([]*state_proto.AlarmState, error) {
|
2024-01-11 10:24:56 +08:00
|
|
|
var states []*state_proto.AlarmState
|
2023-10-27 14:57:37 +08:00
|
|
|
for _, data := range ibpAlarms { // 报警器
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(data.Common)
|
2024-02-20 14:18:42 +08:00
|
|
|
uid := ibpBoxUid + "_" + uidsMap.IbpAlarmIds[did].Uid
|
2023-12-20 10:37:54 +08:00
|
|
|
entry, ok := entity.GetEntityByUid(world, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:43:20 +08:00
|
|
|
continue
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.AlarmTag) {
|
2024-01-11 10:24:56 +08:00
|
|
|
states = append(states, &state_proto.AlarmState{Id: did, Active: component.BitStateType.Get(entry).Val})
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集灯状态信息
|
2024-02-20 14:18:42 +08:00
|
|
|
func collectIBPLightState(world ecs.World, ibpBoxUid string, uidsMap *memory.IBPUidStructure, ibpLights []*data_proto.IbpLight) ([]*state_proto.LightState, error) {
|
2024-01-11 10:24:56 +08:00
|
|
|
var states []*state_proto.LightState
|
2023-10-27 14:57:37 +08:00
|
|
|
for _, data := range ibpLights { // 指示灯
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(data.Common)
|
2024-02-20 14:18:42 +08:00
|
|
|
uid := ibpBoxUid + "_" + uidsMap.IbpLightIds[did].Uid
|
2023-12-20 10:37:54 +08:00
|
|
|
entry, ok := entity.GetEntityByUid(world, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:43:20 +08:00
|
|
|
continue
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.LightTag) {
|
2024-01-11 10:24:56 +08:00
|
|
|
states = append(states, &state_proto.LightState{Id: did, Active: component.BitStateType.Get(entry).Val})
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集钥匙状态
|
2024-02-20 14:18:42 +08:00
|
|
|
func collectIBPKeyState(world ecs.World, ibpBoxUid string, uidsMap *memory.IBPUidStructure, ibpKeys []*data_proto.IbpKey) ([]*state_proto.KeyState, error) {
|
2024-01-11 10:24:56 +08:00
|
|
|
var states []*state_proto.KeyState
|
2023-10-27 14:57:37 +08:00
|
|
|
for _, data := range ibpKeys { // 钥匙
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(data.Common)
|
2024-02-20 14:18:42 +08:00
|
|
|
uid := ibpBoxUid + "_" + uidsMap.IbpKeyIds[did].Uid
|
2023-12-20 10:37:54 +08:00
|
|
|
entry, ok := entity.GetEntityByUid(world, uid)
|
2023-10-27 14:57:37 +08:00
|
|
|
if !ok {
|
2023-12-07 10:43:20 +08:00
|
|
|
continue
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.KeyTag) {
|
2024-02-04 18:22:52 +08:00
|
|
|
gearState := component.GearStateType.Get(entry)
|
2024-02-06 10:49:46 +08:00
|
|
|
states = append(states, &state_proto.KeyState{ /*Bypass: gearState.BypassEnable,*/ Id: did, Gear: gearState.Val})
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return states, nil
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|