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-26 16:41:18 +08:00
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 信号平面布置图消息服务
|
|
|
|
type SfpMs struct {
|
|
|
|
vs *memory.VerifySimulation
|
|
|
|
mapId int32
|
|
|
|
channel string
|
|
|
|
}
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
func NewSfpMs(vs *memory.VerifySimulation, mapId int32) *SfpMs {
|
2023-10-26 16:41:18 +08:00
|
|
|
return &SfpMs{
|
|
|
|
vs: vs,
|
|
|
|
mapId: mapId,
|
|
|
|
channel: fmt.Sprintf("simulation-%s_%d-devices-status", vs.SimulationId, mapId),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取通道名
|
|
|
|
func (ms *SfpMs) GetChannel() string {
|
|
|
|
return ms.channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发送消息间隔时间,单位ms
|
|
|
|
func (ms *SfpMs) GetInterval() time.Duration {
|
|
|
|
return 200 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
// 定时发送的消息
|
|
|
|
func (ms *SfpMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
|
|
|
turnoutStates, err := ms.collectTurnoutStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trainStates, err := ms.collectTrainStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signalStates, err := ms.collectSignalStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buttonStates, err := ms.collectStationButtonStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
psdStates, err := ms.collectPsdStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sectionStates, err := ms.collectSectionStates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ststes := &state.PushedDevicesStatus{
|
|
|
|
All: true,
|
|
|
|
AllStatus: &state.AllDevicesStatus{
|
|
|
|
SwitchState: turnoutStates,
|
|
|
|
TrainState: trainStates,
|
|
|
|
SignalState: signalStates,
|
|
|
|
ButtonState: buttonStates,
|
|
|
|
PsdState: psdStates,
|
|
|
|
SectionState: sectionStates,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(ststes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("信号布置图设备状态消息服务数据序列化失败, %s", err)
|
|
|
|
}
|
|
|
|
return []*ms_api.TopicMsg{ms_api.NewTopicMsg(ms.channel, b)}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:48:43 +08:00
|
|
|
func (ms *SfpMs) OnError(err error) {
|
|
|
|
// TODO: 错误处理
|
|
|
|
}
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
// 收集屏蔽门状态
|
|
|
|
func (ms *SfpMs) collectPsdStates() ([]*state.PsdState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
world := ms.vs.World
|
|
|
|
uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](ms.mapId)
|
|
|
|
data := memory.QueryGiData[*graphicData.RtssGraphicStorage](ms.mapId)
|
|
|
|
var psdStateArr []*state.PsdState
|
|
|
|
for _, door := range data.ScreenDoors {
|
|
|
|
uid := uidStructure.PsdIds[door.Common.Id].Uid
|
|
|
|
psdEntry, ok := entity.GetEntityByUid(world, uid)
|
|
|
|
if ok {
|
|
|
|
var amount int32
|
|
|
|
psdState := component.PsdStateType.Get(psdEntry)
|
|
|
|
if psdState.Km8 {
|
|
|
|
amount = 8
|
|
|
|
} else if psdState.Km4 {
|
|
|
|
amount = 4
|
|
|
|
} else {
|
|
|
|
amount = 0
|
|
|
|
}
|
|
|
|
start, end := getStartEndCodeByGroup(door, amount)
|
|
|
|
var openSubDoor []int32
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
openSubDoor = append(openSubDoor, i)
|
|
|
|
}
|
|
|
|
psdStateArr = append(psdStateArr, &state.PsdState{
|
|
|
|
Id: door.Common.Id,
|
|
|
|
OpenDoorCodes: openSubDoor,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return psdStateArr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStartEndCodeByGroup(door *graphicData.ScreenDoor, groupAmount int32) (int32, int32) {
|
|
|
|
for _, group := range door.ScreenDoorGroupList {
|
|
|
|
if group.TrainGroupAmount == groupAmount {
|
|
|
|
return group.StartSmallDoor, group.EndSmallDoor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, -1
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 收集区段状态
|
|
|
|
func (ms *SfpMs) collectSectionStates() ([]*state.SectionState, error) {
|
2023-10-27 14:57:37 +08:00
|
|
|
uidMap := memory.QueryMapUidMapByType(ms.mapId, &graphicData.Section{})
|
|
|
|
var sectionArr []*state.SectionState
|
|
|
|
for _, u := range uidMap {
|
|
|
|
s := handlerSectionState(ms.vs.World, u.Uid)
|
|
|
|
if s == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.Id = u.CommonId
|
|
|
|
sectionArr = append(sectionArr, s)
|
|
|
|
}
|
|
|
|
return sectionArr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSectionState(w ecs.World, uid string) *state.SectionState {
|
|
|
|
entry, ok := entity.GetEntityByUid(w, uid)
|
|
|
|
if !ok {
|
|
|
|
//fmt.Printf("id=%s的信号机不存在", uid)
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-31 17:39:11 +08:00
|
|
|
if entry.HasComponent(component.AxleSectionType) { //计轴区段
|
2023-10-27 14:57:37 +08:00
|
|
|
sectionState := &state.SectionState{}
|
2023-10-31 17:39:11 +08:00
|
|
|
axleState := component.AxleSectionType.Get(entry)
|
2023-11-01 11:18:09 +08:00
|
|
|
sectionFault := component.AxleSectionFaultType.Get(entry)
|
2023-10-27 14:57:37 +08:00
|
|
|
sectionState.Occupied = axleState.Occ
|
2023-11-01 11:18:09 +08:00
|
|
|
sectionState.AxleFault = sectionFault.SectionFault
|
2023-10-27 14:57:37 +08:00
|
|
|
return sectionState
|
|
|
|
}
|
|
|
|
return nil
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 收集车站按钮状态
|
|
|
|
func (ms *SfpMs) collectStationButtonStates() ([]*state.ButtonState, error) {
|
|
|
|
// 获取地图上的按钮状态
|
|
|
|
uidMap := memory.QueryMapUidMapByType(ms.mapId, &graphicData.EsbButton{})
|
|
|
|
var btnStateArr []*state.ButtonState
|
|
|
|
for _, u := range uidMap {
|
|
|
|
entry, ok := entity.GetEntityByUid(ms.vs.World, u.Uid)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("ESB按钮实体不存在: World id=%d, uid=%s", ms.vs.World.Id(), u.Uid)
|
|
|
|
}
|
|
|
|
if entry.HasComponent(component.ButtonTag) { // 按钮
|
|
|
|
bit := component.BitStateType.Get(entry)
|
|
|
|
btnStateArr = append(btnStateArr, &state.ButtonState{Id: u.CommonId, Down: bit.Val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return btnStateArr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集信号机状态
|
|
|
|
func (ms *SfpMs) collectSignalStates() ([]*state.SignalState, error) {
|
|
|
|
uidMap := memory.QueryMapUidMapByType(ms.mapId, &graphicData.Signal{})
|
|
|
|
var signalArr []*state.SignalState
|
|
|
|
for _, u := range uidMap {
|
|
|
|
s, err := handlerSignalState(ms.vs.World, u.Uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.Id = u.CommonId
|
|
|
|
signalArr = append(signalArr, s)
|
|
|
|
}
|
|
|
|
return signalArr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSignalState(w ecs.World, uid string) (*state.SignalState, error) {
|
|
|
|
entry, ok := entity.GetEntityByUid(w, uid)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("信号机不存在: World id=%d, 信号机id=%s", w.Id(), uid)
|
|
|
|
}
|
|
|
|
if !entry.HasComponent(component.SignalLightsType) { //信号机灯列表
|
|
|
|
return nil, fmt.Errorf("信号机没有SignalLights组件: World id=%d, 信号机id=%s", w.Id(), uid)
|
|
|
|
}
|
|
|
|
signalState := &state.SignalState{}
|
|
|
|
lights := component.SignalLightsType.Get(entry)
|
|
|
|
isL := false
|
|
|
|
isH := false
|
|
|
|
isU := false
|
|
|
|
isA := false
|
|
|
|
isB := false
|
|
|
|
for _, light := range lights.Lights {
|
|
|
|
switch {
|
|
|
|
case light.HasComponent(component.LdTag):
|
|
|
|
isL = component.BitStateType.Get(light).Val
|
|
|
|
case light.HasComponent(component.HdTag):
|
|
|
|
isH = component.BitStateType.Get(light).Val
|
|
|
|
case light.HasComponent(component.UdTag):
|
|
|
|
isU = component.BitStateType.Get(light).Val
|
|
|
|
case light.HasComponent(component.BdTag):
|
|
|
|
isB = component.BitStateType.Get(light).Val
|
|
|
|
case light.HasComponent(component.AdTag):
|
|
|
|
isA = component.BitStateType.Get(light).Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isH && isU {
|
|
|
|
signalState.Aspect = state.Signal_HU
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case isL:
|
|
|
|
signalState.Aspect = state.Signal_L
|
|
|
|
case isH:
|
|
|
|
signalState.Aspect = state.Signal_H
|
|
|
|
case isU:
|
|
|
|
signalState.Aspect = state.Signal_U
|
|
|
|
case isB:
|
|
|
|
signalState.Aspect = state.Signal_B
|
|
|
|
case isA:
|
|
|
|
signalState.Aspect = state.Signal_A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signalState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集列车状态
|
|
|
|
func (ms *SfpMs) collectTrainStates() ([]*state.TrainState, error) {
|
|
|
|
allTrainMap := &ms.vs.Memory.Status.TrainStateMap
|
|
|
|
var trainArr []*state.TrainState
|
|
|
|
allTrainMap.Range(func(_, v any) bool {
|
|
|
|
trainArr = append(trainArr, v.(*state.TrainState))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return trainArr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 收集道岔状态
|
|
|
|
func (ms *SfpMs) collectTurnoutStates() ([]*state.SwitchState, error) {
|
|
|
|
s := ms.vs
|
|
|
|
uidMap := memory.QueryMapUidMapByType(ms.mapId, &graphicData.Turnout{})
|
|
|
|
var switchArr []*state.SwitchState
|
|
|
|
for _, u := range uidMap {
|
|
|
|
entry, ok := entity.GetEntityByUid(s.World, u.Uid)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("道岔不存在: World id=%d,道岔id=%s", s.World.Id(), u.Uid)
|
|
|
|
}
|
|
|
|
if !entry.HasComponent(component.TurnoutPositionType) {
|
|
|
|
return nil, fmt.Errorf("道岔没有TurnoutPosition组件: World id=%d,道岔id=%s", s.World.Id(), u.Uid)
|
|
|
|
}
|
|
|
|
pos := component.TurnoutPositionType.Get(entry)
|
|
|
|
s := &state.SwitchState{
|
|
|
|
Id: u.CommonId,
|
|
|
|
Normal: pos.Db,
|
|
|
|
Reverse: pos.Fb,
|
|
|
|
Dw: pos.Dw,
|
|
|
|
Fw: pos.Fw,
|
|
|
|
}
|
|
|
|
switchArr = append(switchArr, s)
|
|
|
|
}
|
|
|
|
return switchArr, nil
|
|
|
|
}
|