rts-sim-testing-service/ts/test_simulation_manage.go

243 lines
6.3 KiB
Go

package ts
import (
"fmt"
"joylink.club/bj-rtsts-server/service"
"joylink.club/bj-rtsts-server/third_party/acc"
axleBeijing12 "joylink.club/bj-rtsts-server/third_party/axle_device/beijing12"
"joylink.club/bj-rtsts-server/third_party/interlock/beijing11"
"joylink.club/bj-rtsts-server/third_party/interlock/beijing12"
"joylink.club/bj-rtsts-server/third_party/radar"
"joylink.club/bj-rtsts-server/third_party/train_pc_sim"
"joylink.club/rtsssimulation/fi"
"log/slog"
"runtime"
"strconv"
"sync"
"time"
"joylink.club/bj-rtsts-server/third_party/can_btm"
cidcmodbus "joylink.club/bj-rtsts-server/third_party/cidc_modbus"
"joylink.club/bj-rtsts-server/third_party/electrical_machinery"
"joylink.club/bj-rtsts-server/message_server"
"joylink.club/bj-rtsts-server/mqtt"
"joylink.club/bj-rtsts-server/sys_error"
"joylink.club/bj-rtsts-server/third_party/dynamics"
"joylink.club/bj-rtsts-server/third_party/semi_physical_train"
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
"joylink.club/bj-rtsts-server/dto"
rtss_simulation "joylink.club/rtsssimulation"
)
// 仿真存储集合
var simulationMap sync.Map
// 创建前检查
func IsExistSimulation() bool {
i := 0
simulationMap.Range(func(_, _ any) bool {
i++
return true
})
return i > 0
}
// 创建仿真对象
func CreateSimulation(projectId int32, mapIds []int32, runConfig *dto.ProjectRunConfigDto) (string, error) {
simulationId := createSimulationId(projectId)
_, e := simulationMap.Load(simulationId)
if !e && IsExistSimulation() {
return "", sys_error.New("一套环境同时只能运行一个仿真")
}
if !e {
project := service.QueryProject(projectId)
verifySimulation, err := memory.CreateSimulation(projectId, mapIds, runConfig)
if err != nil {
return "", err
}
verifySimulation.SimulationId = simulationId
verifySimulation.ProjectCode = project.Code
// world构建
err = initWorld(verifySimulation)
if err != nil {
return "", err
}
// verifySimulation.Start()
// 全部成功,启动仿真
verifySimulation.World.StartUp()
// 初始化设备状态
initDeviceStatus(verifySimulation)
// 启动仿真消息服务
message_server.Start(verifySimulation)
// 第三方服务处理
err = runThirdParty(verifySimulation)
if err != nil {
verifySimulation.World.Close()
return "", err
}
simulationMap.Store(simulationId, verifySimulation)
}
return simulationId, nil
}
// 删除仿真对象
func DestroySimulation(simulationId string) {
s, e := simulationMap.Load(simulationId)
if !e {
return
}
simulationInfo := s.(*memory.VerifySimulation)
memory.RemoveAllTrain(simulationInfo, true)
simulationMap.Delete(simulationId)
// simulationInfo.Destroy()
// 停止ecs world
simulationInfo.World.Close()
message_server.Close(simulationInfo)
// 确保发布销毁消息
message_server.PubSimulationDestroyMsg(simulationId)
// 停止第三方
stopThirdParty(simulationInfo)
}
func initDeviceStatus(simulation *memory.VerifySimulation) {
for _, turnout := range simulation.Repo.TurnoutList() {
err := fi.DriveTurnoutDCOn(simulation.World, turnout.Id())
if err != nil {
slog.Error("初始驱动道岔到定位失败", "error", err)
}
}
time.Sleep(200 * time.Millisecond)
for _, turnout := range simulation.Repo.TurnoutList() {
err := fi.DriveTurnoutDCOff(simulation.World, turnout.Id())
if err != nil {
slog.Error("取消驱动道岔到定位失败", "error", err)
}
}
}
// 创建world
func initWorld(s *memory.VerifySimulation) error {
//创建仿真
w, err := rtss_simulation.NewSimulation(s.Repo)
if err != nil {
return sys_error.New(fmt.Sprintf("仿真创建失败: %s", err), err)
}
s.World = w
// 保证World关闭
runtime.SetFinalizer(s, func(verifySimulation *memory.VerifySimulation) {
slog.Info("---关闭仿真World---")
verifySimulation.World.Close()
})
return nil
}
// 运行仿真第三方模块
func runThirdParty(s *memory.VerifySimulation) error {
// 动力学启动
err := dynamics.Default().Start(s)
if err != nil {
return err
}
// 半实物启动
semi_physical_train.Default().Start(s)
// 联锁启动
for _, c := range s.GetRunConfig().Interlocks {
switch c.Line {
case "11":
beijing11.Start(c, s)
default:
beijing12.Start(c, s, s.Repo.TransponderList())
}
}
// 计轴RSSP启动
axleBeijing12.Start(s)
//obsolete.StartLineAllRsspAxleServices(s)
// 电机UDP启动
electrical_machinery.Default().Start(s)
// 车载BTM启动
can_btm.Default().Start(s)
// 联锁驱采Modbus服务启动
err = cidcmodbus.Start(s)
if err != nil {
return err
}
//列车雷达发送vobc
radar.Default().Start(s)
//列车加速计发送vobc
acc.Default().Start(s)
train_pc_sim.Default().Start(s)
//btm vobc
semi_physical_train.BtmDefault().Start(s)
return nil
}
// 停止仿真
func stopThirdParty(s *memory.VerifySimulation) {
// 停止半实物
semi_physical_train.Default().Stop()
// 车载BTM停止
can_btm.Default().Stop()
// 联锁驱采Modbus服务停止
cidcmodbus.Stop()
// 雷达服务停止
radar.Default().Stop()
// 加速计服务停止
acc.Default().Stop()
//列车PC仿真停止
train_pc_sim.Default().Stop()
semi_physical_train.BtmDefault().Stop()
// 停止动力学接口功能
dynamics.Default().Stop()
// 联锁通信服务停止
for _, c := range s.GetRunConfig().Interlocks {
switch c.Line {
case "11":
beijing11.Stop(c.Code)
default:
beijing12.Stop(c)
}
}
//计轴RSSP启动销毁
axleBeijing12.Stop(s)
//obsolete.StopLineAllRsspAxleServices()
// 电机UDP停止
electrical_machinery.Default().Stop()
}
func createSimulationId(projectId int32) string {
// MQTT客户端id+项目
return mqtt.GetClientId() + "_" + strconv.Itoa(int(projectId))
}
// 获取仿真列表
func ListAllSimulations() []*dto.SimulationInfoRspDto {
var simArr []*dto.SimulationInfoRspDto
simulationMap.Range(func(_, v any) bool {
s := v.(*memory.VerifySimulation)
simArr = append(simArr, &dto.SimulationInfoRspDto{
SimulationId: s.SimulationId,
MapId: s.MapIds[0],
MapIds: s.MapIds,
ProjectId: s.ProjectId,
ProjectRunConfigId: s.GetRunConfigId(),
})
return true
})
return simArr
}
// 根据仿真id查找仿真实例
func FindSimulation(simulationId string) *memory.VerifySimulation {
m, e := simulationMap.Load(simulationId)
if e {
return m.(*memory.VerifySimulation)
}
return nil
}