2023-10-26 16:41:18 +08:00
|
|
|
|
package message_server
|
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-01-11 10:24:56 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/dto/state_proto"
|
2024-03-26 13:12:17 +08:00
|
|
|
|
"log/slog"
|
2023-11-17 17:01:12 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2024-01-11 10:24:56 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/dto/data_proto"
|
2023-10-27 14:57:37 +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-27 14:57:37 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
2023-12-20 10:37:54 +08:00
|
|
|
|
"joylink.club/ecs"
|
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
2023-10-27 14:57:37 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-12-20 10:37:54 +08:00
|
|
|
|
func NewPSLMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
|
2024-01-11 10:24:56 +08:00
|
|
|
|
mapData := memory.QueryGiData[*data_proto.RtssGraphicStorage](mapId)
|
2024-04-08 09:25:06 +08:00
|
|
|
|
uidStructure := memory.QueryUidStructure[*memory.StationUidStructure](mapId)
|
2024-03-26 13:12:17 +08:00
|
|
|
|
return ms_api.NewScheduleTask(fmt.Sprintf("地图[%d]PSL按钮状态", mapId), func() error {
|
2024-04-08 09:25:06 +08:00
|
|
|
|
for _, psl := range mapData.PslBoxs {
|
|
|
|
|
data, err := collectPslStates(vs.World, psl.RefPslMapCode, uidStructure.PslIds[psl.Common.Id].Uid)
|
2023-12-20 10:37:54 +08:00
|
|
|
|
if err != nil {
|
2024-04-08 09:25:06 +08:00
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
continue
|
2023-12-20 10:37:54 +08:00
|
|
|
|
}
|
2024-04-08 09:25:06 +08:00
|
|
|
|
sendMsg(vs, mapId, psl.Common.Id, data)
|
2024-03-26 13:12:17 +08:00
|
|
|
|
}
|
2024-04-08 09:25:06 +08:00
|
|
|
|
for _, psl := range mapData.GarageDoors {
|
|
|
|
|
data, err := collectPslStates(vs.World, psl.RefPslMapCode, uidStructure.PslIds[psl.Common.Id].Uid)
|
2024-03-26 13:12:17 +08:00
|
|
|
|
if err != nil {
|
2024-04-08 09:25:06 +08:00
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
sendMsg(vs, mapId, psl.Common.Id, data)
|
|
|
|
|
}
|
|
|
|
|
for _, psl := range mapData.FloodGates {
|
|
|
|
|
data, err := collectPslStates(vs.World, psl.RefPslMapCode, uidStructure.PslIds[psl.Common.Id].Uid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
continue
|
2024-03-26 13:12:17 +08:00
|
|
|
|
}
|
2024-04-08 09:25:06 +08:00
|
|
|
|
sendMsg(vs, mapId, psl.Common.Id, data)
|
2023-10-27 14:57:37 +08:00
|
|
|
|
}
|
2023-12-20 10:37:54 +08:00
|
|
|
|
return nil
|
|
|
|
|
}, 200*time.Millisecond)
|
2023-10-27 14:57:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 13:12:17 +08:00
|
|
|
|
func sendMsg(vs *memory.VerifySimulation, mapId int32, pslId uint32, data *state_proto.PushedDevicesStatus) {
|
|
|
|
|
err := mqtt.GetMsgClient().PubPSLState(vs.SimulationId, mapId, pslId, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(fmt.Sprintf("发送PSL状态出错:%s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 09:25:06 +08:00
|
|
|
|
func collectPslStates(world ecs.World, pslMapCode string, pslUid string) (*state_proto.PushedDevicesStatus, error) {
|
|
|
|
|
var btnStates []*state_proto.ButtonState
|
|
|
|
|
_, pslStorage := memory.QueryGiDataByName[*data_proto.PslGraphicStorage](pslMapCode)
|
|
|
|
|
for _, button := range pslStorage.PslButtons {
|
|
|
|
|
btnEntry, ok := entity.GetEntityByUid(world, memory.BuildUid(pslUid, button.Code))
|
|
|
|
|
if !ok {
|
|
|
|
|
slog.Error(fmt.Sprintf("[id:%s]的PSL的按钮[code:%s]对应的实体找不到", pslUid, button.Code))
|
|
|
|
|
continue
|
2023-11-01 16:52:08 +08:00
|
|
|
|
}
|
2024-04-08 09:25:06 +08:00
|
|
|
|
btnStates = append(btnStates, &state_proto.ButtonState{
|
|
|
|
|
Id: button.Common.Id,
|
|
|
|
|
Down: component.BitStateType.Get(btnEntry).Val,
|
|
|
|
|
Active: component.BitStateType.Get(btnEntry).Val,
|
|
|
|
|
})
|
2023-11-01 16:52:08 +08:00
|
|
|
|
}
|
2024-01-11 10:24:56 +08:00
|
|
|
|
return &state_proto.PushedDevicesStatus{
|
2023-11-01 16:52:08 +08:00
|
|
|
|
All: true,
|
2024-01-11 10:24:56 +08:00
|
|
|
|
AllStatus: &state_proto.AllDevicesStatus{
|
2024-04-08 09:25:06 +08:00
|
|
|
|
ButtonState: btnStates,
|
2023-11-01 16:52:08 +08:00
|
|
|
|
},
|
|
|
|
|
}, nil
|
2023-10-26 16:41:18 +08:00
|
|
|
|
}
|