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

57 lines
1.4 KiB
Go
Raw Normal View History

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/state"
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
)
// 世界状态变化消息服务
type SimulationStateMs struct {
vs *memory.VerifySimulation
channel string
currentState state.SimulationStatus_SimulationState
}
func NewSimulationStateMs(vs *memory.VerifySimulation) *SimulationStateMs {
return &SimulationStateMs{
vs: vs,
channel: fmt.Sprintf("simulation-%s-status", vs.SimulationId),
}
}
// 获取消息服务名
func (s *SimulationStateMs) GetChannel() string {
return s.channel
}
// 发送消息间隔时间,单位ms
func (s *SimulationStateMs) GetInterval() time.Duration {
return 200 * time.Millisecond
}
// 构造定时发送的消息
func (s *SimulationStateMs) OnTick() ([]*ms_api.TopicMsg, error) {
if s.currentState == s.vs.GetRunState() {
return nil, nil
}
s.currentState = s.vs.GetRunState()
ststes := &state.SimulationStatus{
SimulationId: s.vs.SimulationId,
State: s.vs.GetRunState(),
}
b, err := proto.Marshal(ststes)
if err != nil {
return nil, fmt.Errorf("仿真状态消息服务数据序列化失败, %s", err)
}
return []*ms_api.TopicMsg{ms_api.NewTopicMsg(s.channel, b)}, nil
}
// 当发生错误时执行的逻辑
func (s *SimulationStateMs) OnError(err error) {}