2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
2023-10-27 14:57:37 +08:00
|
|
|
import (
|
2023-12-26 13:27:09 +08:00
|
|
|
"fmt"
|
2024-01-23 09:37:31 +08:00
|
|
|
"time"
|
|
|
|
|
2024-01-11 10:24:56 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto/state_proto"
|
2023-12-26 13:27:09 +08:00
|
|
|
|
2023-12-20 10:37:54 +08:00
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
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/simulation/wayside/memory"
|
2023-10-27 16:51:48 +08:00
|
|
|
"joylink.club/ecs"
|
2023-10-27 14:57:37 +08:00
|
|
|
)
|
|
|
|
|
2023-12-20 10:37:54 +08:00
|
|
|
func NewStateMs(vs *memory.VerifySimulation) ms_api.MsgTask {
|
2024-01-23 09:37:31 +08:00
|
|
|
return ms_api.NewScheduleTask(fmt.Sprintf("仿真[%s]状态", vs.SimulationId), func() error {
|
|
|
|
s := &state_proto.SimulationStatus{
|
|
|
|
SimulationId: vs.SimulationId,
|
|
|
|
State: convertWorldState(vs.World.State()),
|
|
|
|
}
|
|
|
|
mqtt.GetMsgClient().PubSimulationState(vs.SimulationId, s)
|
|
|
|
return nil
|
|
|
|
}, 1000*time.Millisecond)
|
|
|
|
// return ms_api.NewMonitorTask(fmt.Sprintf("仿真[%s]状态", vs.SimulationId), func() {
|
|
|
|
// ecs.WorldStateChangeEvent.Subscribe(vs.World, func(_ ecs.World, e ecs.WorldStateChange) {
|
|
|
|
// s := &state_proto.SimulationStatus{SimulationId: vs.SimulationId}
|
|
|
|
// switch e.NewState {
|
|
|
|
// case ecs.WorldClosed:
|
|
|
|
// s.State = state_proto.SimulationStatus_DESTROY
|
|
|
|
// case ecs.WorldError:
|
|
|
|
// s.State = state_proto.SimulationStatus_ERROR
|
|
|
|
// case ecs.WorldPause:
|
|
|
|
// s.State = state_proto.SimulationStatus_PAUSE
|
|
|
|
// default:
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// mqtt.GetMsgClient().PubSimulationState(vs.SimulationId, s)
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertWorldState(state ecs.WorldState) state_proto.SimulationStatus_SimulationState {
|
|
|
|
switch state {
|
|
|
|
case ecs.WorldInit:
|
|
|
|
return state_proto.SimulationStatus_Init
|
|
|
|
case ecs.WorldRunning:
|
|
|
|
return state_proto.SimulationStatus_Running
|
|
|
|
case ecs.WorldPause:
|
|
|
|
return state_proto.SimulationStatus_Pause
|
|
|
|
case ecs.WorldError:
|
|
|
|
return state_proto.SimulationStatus_Error
|
|
|
|
case ecs.WorldClosed:
|
|
|
|
return state_proto.SimulationStatus_Destroy
|
|
|
|
default:
|
|
|
|
panic("unknown world state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发布仿真销毁消息
|
|
|
|
func PubSimulationDestroyMsg(simulationId string) {
|
|
|
|
s := &state_proto.SimulationStatus{
|
|
|
|
SimulationId: simulationId,
|
|
|
|
State: state_proto.SimulationStatus_Destroy,
|
|
|
|
}
|
|
|
|
mqtt.GetMsgClient().PubSimulationState(simulationId, s)
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|