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

53 lines
1.6 KiB
Go
Raw Normal View History

package message_server
import (
2023-12-26 13:27:09 +08:00
"fmt"
"time"
"joylink.club/bj-rtsts-server/message_server/ms_api"
2023-12-20 10:37:54 +08:00
"joylink.club/bj-rtsts-server/mqtt"
"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-12-20 10:37:54 +08:00
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
2023-12-20 10:37:54 +08:00
func NewRccMs(vs *memory.VerifySimulation, mapId int32) ms_api.MsgTask {
2023-12-26 13:27:09 +08:00
return ms_api.NewScheduleTask(fmt.Sprintf("地图[%d]继电器状态", mapId), func() error {
2023-12-20 10:37:54 +08:00
relayStates, err := collectRelayState(vs.World, mapId)
if err != nil {
return err
}
ststes := &state.PushedDevicesStatus{
All: true,
AllStatus: &state.AllDevicesStatus{
ReplyState: relayStates,
},
}
2023-12-25 14:15:22 +08:00
mqtt.GetMsgClient().PubRCCState(vs.SimulationId, mapId, ststes)
2023-12-20 10:37:54 +08:00
return nil
}, 200*time.Millisecond)
}
// 获取仿真地图的继电器状态,前端推送
2023-12-20 10:37:54 +08:00
func collectRelayState(world ecs.World, mapId int32) ([]*state.ReplyState, error) {
// 获取本地图下的继电器信息
2023-12-20 10:37:54 +08:00
uidMap := memory.QueryMapUidMapByType(mapId, &graphicData.Relay{})
var replyStateArr []*state.ReplyState
for _, u := range uidMap {
2023-12-20 10:37:54 +08:00
entry, ok := entity.GetEntityByUid(world, u.Uid)
if !ok {
// 暂时注释,很多继电器都没初始化
//return nil, fmt.Errorf("继电器实体不存在: World id=%d, uid=%s", r.vs.World.Id(), u.Uid)
continue
}
if entry.HasComponent(component.RelayTag) {
bit := component.BitStateType.Get(entry)
replyStateArr = append(replyStateArr, &state.ReplyState{Id: u.CommonId, Xh: bit.Val})
}
}
return replyStateArr, nil
}