2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-17 17:01:12 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
2023-10-27 14:57:37 +08:00
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
// PSL或门控箱消息服务
|
|
|
|
type PslMs struct {
|
2023-10-27 14:57:37 +08:00
|
|
|
vs *memory.VerifySimulation
|
|
|
|
mapId int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPSLMs(vs *memory.VerifySimulation, mapId int32) *PslMs {
|
|
|
|
return &PslMs{vs: vs, mapId: mapId}
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
func (p *PslMs) GetChannel() string {
|
2023-11-17 18:04:38 +08:00
|
|
|
return SimulationPslTopic
|
2023-11-17 17:01:12 +08:00
|
|
|
// return "simulation-psl-%s_%d_%s-status"
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
func (p *PslMs) GetInterval() time.Duration {
|
2023-10-27 14:57:37 +08:00
|
|
|
return 200 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
func (p *PslMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
var msgArr []*ms_api.TopicMsg
|
2023-11-01 16:52:08 +08:00
|
|
|
mapData := memory.QueryGiData[*graphicData.RtssGraphicStorage](p.mapId)
|
2023-10-27 14:57:37 +08:00
|
|
|
for _, box := range mapData.GateBoxs {
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(box.Common)
|
|
|
|
channel := p.handlerPSLChannelName(did)
|
2023-11-01 16:52:08 +08:00
|
|
|
state, err := p.collectGateBoxPSLState(box)
|
2023-10-27 14:57:37 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
func (p *PslMs) OnError(err error) {}
|
2023-10-27 14:57:37 +08:00
|
|
|
|
|
|
|
// 处理订阅通道名称
|
2023-12-14 13:04:48 +08:00
|
|
|
func (p *PslMs) handlerPSLChannelName(gateBoxId uint32) string {
|
2023-11-17 18:04:38 +08:00
|
|
|
return fmt.Sprintf(SimulationPslTopic, p.vs.SimulationId, p.mapId, gateBoxId)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
|
2023-11-01 16:52:08 +08:00
|
|
|
func (p *PslMs) collectGateBoxPSLState(box *graphicData.GatedBox) (*state.PushedDevicesStatus, error) {
|
|
|
|
world := p.vs.World
|
2023-12-13 15:05:42 +08:00
|
|
|
did := memory.GetMapElementId(box.Common)
|
2023-11-01 16:52:08 +08:00
|
|
|
uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](p.mapId)
|
2023-12-13 15:05:42 +08:00
|
|
|
boxUid := uidStructure.GateBoxIds[did].Uid
|
2023-11-01 16:52:08 +08:00
|
|
|
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 {
|
2023-12-14 13:04:48 +08:00
|
|
|
pslMapId := memory.QueryGiId(box.RefGatedBoxMapCode)
|
|
|
|
pslStorage := memory.QueryGiData[*graphicData.PslGraphicStorage](pslMapId)
|
|
|
|
btnUidMap := make(map[string]uint32, len(pslStorage.PslButtons))
|
|
|
|
for _, button := range pslStorage.PslButtons {
|
|
|
|
btnUidMap[boxUid+"_"+button.Code] = memory.GetMapElementId(button.Common)
|
|
|
|
}
|
2023-11-01 16:52:08 +08:00
|
|
|
btnArr := []*ecs.Entry{mkx.PCB, mkx.POB, mkx.PAB}
|
|
|
|
for _, btn := range btnArr {
|
|
|
|
if btn == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buttonStateArr = append(buttonStateArr, &state.ButtonState{
|
2023-12-14 13:04:48 +08:00
|
|
|
Id: btnUidMap[component.UidType.Get(btn).Id],
|
2023-11-01 16:52:08 +08:00
|
|
|
Down: component.BitStateType.Get(btn).Val,
|
|
|
|
Active: component.BitStateType.Get(btn).Val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &state.PushedDevicesStatus{
|
|
|
|
All: true,
|
|
|
|
AllStatus: &state.AllDevicesStatus{
|
|
|
|
ButtonState: buttonStateArr,
|
|
|
|
},
|
|
|
|
}, nil
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|