2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
// 继电器组合柜布置图消息服务
|
|
|
|
type RccMs struct {
|
2023-10-27 14:57:37 +08:00
|
|
|
vs *memory.VerifySimulation
|
|
|
|
mapId int32
|
|
|
|
channel string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRccMs(vs *memory.VerifySimulation, mapId int32) *RccMs {
|
|
|
|
return &RccMs{
|
|
|
|
vs: vs,
|
|
|
|
mapId: mapId,
|
2023-11-17 18:04:38 +08:00
|
|
|
channel: fmt.Sprintf(SimulationRccTopic, vs.SimulationId, mapId),
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取消息服务名
|
|
|
|
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 {
|
2023-10-27 15:45:43 +08:00
|
|
|
// 暂时注释,很多继电器都没初始化
|
|
|
|
//return nil, fmt.Errorf("继电器实体不存在: World id=%d, uid=%s", r.vs.World.Id(), u.Uid)
|
|
|
|
continue
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
if entry.HasComponent(component.RelayTag) {
|
|
|
|
bit := component.BitStateType.Get(entry)
|
|
|
|
replyStateArr = append(replyStateArr, &state.ReplyState{Id: u.CommonId, Xh: bit.Val})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return replyStateArr, nil
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|