【仿真状态服务】

This commit is contained in:
weizhihong 2023-10-27 16:51:48 +08:00
parent 821aa88a9e
commit cc99be478f
2 changed files with 25 additions and 32 deletions

View File

@ -25,7 +25,7 @@ func Start(vs *memory.VerifySimulation) {
if !ok {
sms := &SimulationMs{
vs: vs,
mss: []ms_api.IMsgServer{NewSimulationStateMs(vs)},
mss: []ms_api.IMsgServer{},
}
for _, mapId := range vs.MapIds {
t := memory.QueryOnlyGiType(mapId)
@ -37,6 +37,8 @@ func Start(vs *memory.VerifySimulation) {
sms.mss = append(sms.mss, NewRccMs(vs, mapId))
}
}
// 启动仿真状态服务
NewSimulationStateMs(vs)
ms_manage.Register(sms)
smsMap.Store(vs.SimulationId, sms)
}

View File

@ -2,55 +2,46 @@ package message_server
import (
"fmt"
"time"
"google.golang.org/protobuf/proto"
"joylink.club/bj-rtsts-server/message_server/ms_api"
apiproto "joylink.club/bj-rtsts-server/grpcproto"
"joylink.club/bj-rtsts-server/ts/protos/state"
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
"joylink.club/ecs"
)
// 世界状态变化消息服务
type SimulationStateMs struct {
vs *memory.VerifySimulation
channel string
currentState state.SimulationStatus_SimulationState
vs *memory.VerifySimulation
channel string
}
func NewSimulationStateMs(vs *memory.VerifySimulation) *SimulationStateMs {
return &SimulationStateMs{
ms := &SimulationStateMs{
vs: vs,
channel: fmt.Sprintf("simulation-%s-status", vs.SimulationId),
}
go stateListen(ms)
return ms
}
// 获取消息服务名
func (s *SimulationStateMs) GetChannel() string {
return s.channel
func stateListen(ms *SimulationStateMs) {
ecs.WorldStateChangeEvent.Subscribe(ms.vs.World, func(_ ecs.World, e ecs.WorldStateChange) {
switch e.NewState {
case ecs.WorldClose:
doSendState(ms.channel, &state.SimulationStatus{
SimulationId: ms.vs.SimulationId,
State: state.SimulationStatus_DESTROY,
})
}
})
}
// 发送消息间隔时间,单位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)
func doSendState(channel string, status *state.SimulationStatus) error {
b, err := proto.Marshal(status)
if err != nil {
return nil, fmt.Errorf("仿真状态消息服务数据序列化失败, %s", err)
return fmt.Errorf("仿真状态消息服务数据序列化失败, %s", err)
}
return []*ms_api.TopicMsg{ms_api.NewTopicMsg(s.channel, b)}, nil
apiproto.PublishMsg(channel, b)
return nil
}
// 当发生错误时执行的逻辑
func (s *SimulationStateMs) OnError(err error) {}