package message_server import ( "fmt" "time" "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" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" ) // 继电器组合柜布置图消息服务 type RccMs struct { vs *memory.VerifySimulation mapId int32 channel string } func NewRccMs(vs *memory.VerifySimulation, mapId int32) *RccMs { return &RccMs{ vs: vs, mapId: mapId, channel: fmt.Sprintf("simulation-%s_%d-devices-status", vs.SimulationId, mapId), } } // 获取消息服务名 func (r *RccMs) GetChannel() string { return r.channel } // 发送消息间隔时间,单位ms func (r *RccMs) GetInterval() time.Duration { return 200 * time.Millisecond } // 构造定时发送的消息 func (r *RccMs) OnTick() ([]*ms_api.TopicMsg, error) { relayStates, err := r.collectRelayState() if err != nil { return nil, err } ststes := &state.PushedDevicesStatus{ All: true, AllStatus: &state.AllDevicesStatus{ ReplyState: relayStates, }, } b, err := proto.Marshal(ststes) if err != nil { return nil, fmt.Errorf("信号布置图设备状态消息服务数据序列化失败, %s", err) } return []*ms_api.TopicMsg{ms_api.NewTopicMsg(r.channel, b)}, nil } // 当发生错误时执行的逻辑 func (r *RccMs) OnError(err error) { } // 获取仿真地图的继电器状态,前端推送 func (r *RccMs) collectRelayState() ([]*state.ReplyState, error) { // 获取本地图下的继电器信息 uidMap := memory.QueryMapUidMapByType(r.mapId, &graphicData.Relay{}) var replyStateArr []*state.ReplyState for _, u := range uidMap { entry, ok := entity.GetEntityByUid(r.vs.World, u.Uid) if !ok { return nil, fmt.Errorf("继电器实体不存在: World id=%d, uid=%s", r.vs.World.Id(), u.Uid) } if entry.HasComponent(component.RelayTag) { bit := component.BitStateType.Get(entry) replyStateArr = append(replyStateArr, &state.ReplyState{Id: u.CommonId, Xh: bit.Val}) } } return replyStateArr, nil }