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/state"
|
|
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
|
|
|
)
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
// 世界状态变化消息服务
|
|
|
|
type SimulationStateMs struct {
|
2023-10-27 15:45:43 +08:00
|
|
|
vs *memory.VerifySimulation
|
|
|
|
channel string
|
|
|
|
currentState state.SimulationStatus_SimulationState
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
2023-10-27 14:57:37 +08:00
|
|
|
|
|
|
|
// 发送消息间隔时间,单位ms
|
|
|
|
func (s *SimulationStateMs) GetInterval() time.Duration {
|
|
|
|
return 200 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
// 构造定时发送的消息
|
|
|
|
func (s *SimulationStateMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
2023-10-27 15:45:43 +08:00
|
|
|
if s.currentState == s.vs.GetRunState() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
s.currentState = s.vs.GetRunState()
|
2023-10-27 14:57:37 +08:00
|
|
|
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) {}
|