rts-sim-testing-service/message_server/rcc_ms.go

55 lines
1.7 KiB
Go

package message_server
import (
"fmt"
"time"
"joylink.club/bj-rtsts-server/dto/state_proto"
"joylink.club/bj-rtsts-server/dto/data_proto"
"joylink.club/bj-rtsts-server/message_server/ms_api"
"joylink.club/bj-rtsts-server/mqtt"
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
func NewRccMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
return ms_api.NewScheduleTask(fmt.Sprintf("地图[%d]继电器状态", mapId), func() error {
relayStates, err := collectRelayState(vs.World, mapId)
if err != nil {
return err
}
ststes := &state_proto.PushedDevicesStatus{
All: true,
AllStatus: &state_proto.AllDevicesStatus{
ReplyState: relayStates,
},
}
mqtt.GetMsgClient().PubRCCState(vs.SimulationId, mapId, ststes)
return nil
}, 200*time.Millisecond)
}
// 获取仿真地图的继电器状态,前端推送
func collectRelayState(world ecs.World, mapId int32) ([]*state_proto.ReplyState, error) {
// 获取本地图下的继电器信息
uidMap := memory.QueryMapUidMapByType(mapId, &data_proto.Relay{})
var replyStateArr []*state_proto.ReplyState
for _, u := range uidMap {
entry, ok := entity.GetEntityByUid(world, u.Uid)
if !ok {
// 暂时注释,很多继电器都没初始化
//return nil, fmt.Errorf("继电器实体不存在: World id=%d, uid=%s", r.vs.World.BaliseId(), u.Uid)
continue
}
if entry.HasComponent(component.RelayTag) {
bit := component.BitStateType.Get(entry)
force := entry.HasComponent(component.RelayFaultForceType)
replyStateArr = append(replyStateArr, &state_proto.ReplyState{Id: u.CommonId, Xh: bit.Val, Force: force})
}
}
return replyStateArr, nil
}