2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
2023-11-16 16:54:23 +08:00
|
|
|
"joylink.club/bj-rtsts-server/mqtt"
|
2023-10-27 14:57:37 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ts/protos/state"
|
|
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
2023-10-27 16:51:48 +08:00
|
|
|
"joylink.club/ecs"
|
2023-10-27 14:57:37 +08:00
|
|
|
)
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
// 世界状态变化消息服务
|
|
|
|
type SimulationStateMs struct {
|
2023-10-27 16:51:48 +08:00
|
|
|
vs *memory.VerifySimulation
|
|
|
|
channel string
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimulationStateMs(vs *memory.VerifySimulation) *SimulationStateMs {
|
2023-10-27 16:51:48 +08:00
|
|
|
ms := &SimulationStateMs{
|
2023-10-27 14:57:37 +08:00
|
|
|
vs: vs,
|
|
|
|
channel: fmt.Sprintf("simulation-%s-status", vs.SimulationId),
|
|
|
|
}
|
2023-10-27 16:51:48 +08:00
|
|
|
ecs.WorldStateChangeEvent.Subscribe(ms.vs.World, func(_ ecs.World, e ecs.WorldStateChange) {
|
|
|
|
switch e.NewState {
|
|
|
|
case ecs.WorldClose:
|
|
|
|
doSendState(ms.channel, &state.SimulationStatus{
|
2023-10-27 17:23:44 +08:00
|
|
|
SimulationId: vs.SimulationId,
|
2023-10-27 16:51:48 +08:00
|
|
|
State: state.SimulationStatus_DESTROY,
|
|
|
|
})
|
2023-10-30 11:13:36 +08:00
|
|
|
case ecs.WorldError:
|
|
|
|
doSendState(ms.channel, &state.SimulationStatus{
|
|
|
|
SimulationId: vs.SimulationId,
|
|
|
|
State: state.SimulationStatus_ERROR,
|
|
|
|
})
|
|
|
|
case ecs.WorldPause:
|
|
|
|
doSendState(ms.channel, &state.SimulationStatus{
|
|
|
|
SimulationId: vs.SimulationId,
|
|
|
|
State: state.SimulationStatus_PAUSE,
|
|
|
|
})
|
2023-10-27 16:51:48 +08:00
|
|
|
}
|
|
|
|
})
|
2023-10-27 17:23:44 +08:00
|
|
|
return ms
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
2023-10-27 14:57:37 +08:00
|
|
|
|
2023-10-27 16:51:48 +08:00
|
|
|
func doSendState(channel string, status *state.SimulationStatus) error {
|
|
|
|
b, err := proto.Marshal(status)
|
2023-10-27 14:57:37 +08:00
|
|
|
if err != nil {
|
2023-10-27 16:51:48 +08:00
|
|
|
return fmt.Errorf("仿真状态消息服务数据序列化失败, %s", err)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
2023-11-16 16:54:23 +08:00
|
|
|
mqtt.Publish(channel, b)
|
|
|
|
// apiproto.PublishMsg(channel, b)
|
2023-10-27 16:51:48 +08:00
|
|
|
return nil
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|