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

59 lines
1.8 KiB
Go
Raw Normal View History

package message_server
import (
"log/slog"
"sync"
"joylink.club/bj-rtsts-server/dto/data_proto"
"joylink.club/bj-rtsts-server/message_server/ms_api"
2023-10-26 17:16:07 +08:00
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
)
var _default = &publishTaskManage{tasks: make(map[string][]ms_api.MsgTask)}
// 仿真消息任务管理
type publishTaskManage struct {
mu sync.Mutex
tasks map[string][]ms_api.MsgTask
}
func (m *publishTaskManage) AddTasks(simulationId string, tasks ...ms_api.MsgTask) {
m.mu.Lock()
defer m.mu.Unlock()
m.tasks[simulationId] = append(m.tasks[simulationId], tasks...)
}
func (m *publishTaskManage) RemoveTasks(simulationId string) {
m.mu.Lock()
defer m.mu.Unlock()
slog.Info("停止并移除仿真消息服务", "simulationId", simulationId, "tasks", m.tasks[simulationId])
for _, task := range m.tasks[simulationId] {
task.Stop()
}
delete(m.tasks, simulationId)
}
// 启动仿真所需的消息服务
func Start(vs *memory.VerifySimulation) {
// 添加仿真状态消息服务
_default.AddTasks(vs.SimulationId, NewStateMs(vs))
_default.AddTasks(vs.SimulationId, NewTpapiServiceMs(vs))
2023-12-25 14:15:22 +08:00
for _, mapId := range vs.MapIds {
t := memory.QueryGiType(mapId)
switch t {
case data_proto.PictureType_StationLayout: // 平面布置图
2023-12-25 14:15:22 +08:00
// 添加车站关联的平面布置图、IBP、PSL信息
_default.AddTasks(vs.SimulationId, NewSfpMs(vs, mapId), NewIBPMs(vs, mapId), NewPSLMs(vs, mapId))
case data_proto.PictureType_RelayCabinetLayout: // 继电器柜
_default.AddTasks(vs.SimulationId, NewRccMs(vs, mapId))
case data_proto.PictureType_TrainControlCab:
_default.AddTasks(vs.SimulationId, NewTrainControlMs(vs, mapId))
}
}
}
// 关闭仿真消息服务
func Close(vs *memory.VerifySimulation) {
_default.RemoveTasks(vs.SimulationId)
2023-10-26 17:48:43 +08:00
}