2023-07-31 08:41:42 +08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
2023-08-10 14:18:55 +08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2023-09-12 10:00:13 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ats/verify/simulation/wayside/memory"
|
2023-08-03 10:41:43 +08:00
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-10-20 15:08:47 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-10-19 09:33:40 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/dynamics"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/semi_physical_train"
|
2023-07-31 08:41:42 +08:00
|
|
|
|
2023-08-01 14:54:11 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-07-31 08:41:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 仿真存储集合
|
2023-08-01 14:54:11 +08:00
|
|
|
var simulationMap sync.Map
|
2023-07-31 08:41:42 +08:00
|
|
|
|
2023-08-30 18:05:11 +08:00
|
|
|
// 创建前检查
|
|
|
|
func IsExistSimulation() bool {
|
|
|
|
i := 0
|
|
|
|
simulationMap.Range(func(_, _ any) bool {
|
|
|
|
i++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return i > 0
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:41:42 +08:00
|
|
|
// 创建仿真对象
|
2023-10-20 15:08:47 +08:00
|
|
|
func CreateSimulation(projectId int32, mapIds []int32) (string, error) {
|
2023-09-19 11:02:50 +08:00
|
|
|
simulationId := createSimulationId(projectId)
|
2023-08-01 14:54:11 +08:00
|
|
|
_, e := simulationMap.Load(simulationId)
|
2023-09-01 10:25:27 +08:00
|
|
|
if !e && IsExistSimulation() {
|
2023-10-20 15:08:47 +08:00
|
|
|
return "", sys_error.New("一套环境同时只能运行一个仿真")
|
2023-09-01 10:25:27 +08:00
|
|
|
}
|
2023-08-01 14:54:11 +08:00
|
|
|
if !e {
|
2023-09-20 15:14:40 +08:00
|
|
|
verifySimulation, err := memory.CreateSimulation(projectId, mapIds)
|
|
|
|
if err != nil {
|
2023-10-20 15:08:47 +08:00
|
|
|
return "", err
|
2023-09-19 11:02:50 +08:00
|
|
|
}
|
2023-09-20 15:14:40 +08:00
|
|
|
verifySimulation.SimulationId = simulationId
|
2023-10-19 09:33:40 +08:00
|
|
|
if config.Config.Dynamics.Open {
|
|
|
|
// 动力学接口调用
|
|
|
|
lineBaseInfo := verifySimulation.BuildLineBaseInfo()
|
|
|
|
err := dynamics.Default().RequestStartSimulation(lineBaseInfo)
|
|
|
|
if err != nil {
|
2023-10-20 15:08:47 +08:00
|
|
|
return "", err
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
dynamics.Default().Start(verifySimulation)
|
|
|
|
}
|
|
|
|
if config.Config.Vobc.Open {
|
|
|
|
// 半实物系统接口功能启动
|
|
|
|
semi_physical_train.Default().Start(verifySimulation)
|
2023-10-07 15:59:22 +08:00
|
|
|
}
|
2023-10-20 15:08:47 +08:00
|
|
|
simulationMap.Store(simulationId, verifySimulation)
|
|
|
|
// 全部成功,启动仿真
|
|
|
|
verifySimulation.World.StartUp()
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
2023-10-20 15:08:47 +08:00
|
|
|
return simulationId, nil
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 删除仿真对象
|
|
|
|
func DestroySimulation(simulationId string) {
|
2023-09-21 14:54:27 +08:00
|
|
|
s, e := simulationMap.Load(simulationId)
|
|
|
|
if !e {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
simulationInfo := s.(*memory.VerifySimulation)
|
2023-08-25 17:49:09 +08:00
|
|
|
simulationMap.Delete(simulationId)
|
2023-09-21 14:54:27 +08:00
|
|
|
// 停止ecs world
|
2023-09-28 17:28:20 +08:00
|
|
|
simulationInfo.World.Close()
|
2023-10-19 09:33:40 +08:00
|
|
|
if config.Config.Dynamics.Open {
|
|
|
|
// 停止动力学接口功能
|
|
|
|
dynamics.Default().Stop()
|
|
|
|
dynamics.Default().RequestStopSimulation()
|
2023-08-07 14:08:02 +08:00
|
|
|
}
|
2023-10-25 13:14:19 +08:00
|
|
|
if config.Config.Vobc.Open {
|
|
|
|
semi_physical_train.Default().Stop()
|
|
|
|
}
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 11:02:50 +08:00
|
|
|
func createSimulationId(projectId int32) string {
|
|
|
|
// 当前服务器IP + 端口 + 项目
|
|
|
|
return config.SimulationId_prefix + "_" + strconv.Itoa(config.Config.Server.Port) + "_" + strconv.Itoa(int(projectId))
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 获取仿真列表
|
2023-08-30 13:25:57 +08:00
|
|
|
func ListAllSimulations() []*dto.SimulationInfoRspDto {
|
2023-08-31 16:16:18 +08:00
|
|
|
var simArr []*dto.SimulationInfoRspDto
|
2023-08-25 15:35:00 +08:00
|
|
|
simulationMap.Range(func(_, v any) bool {
|
2023-08-01 14:54:11 +08:00
|
|
|
s := v.(*memory.VerifySimulation)
|
2023-08-30 13:25:57 +08:00
|
|
|
simArr = append(simArr, &dto.SimulationInfoRspDto{
|
2023-08-01 14:54:11 +08:00
|
|
|
SimulationId: s.SimulationId,
|
2023-09-19 11:02:50 +08:00
|
|
|
MapId: s.MapIds[0],
|
|
|
|
MapIds: s.MapIds,
|
2023-08-30 13:25:57 +08:00
|
|
|
ProjectId: s.ProjectId,
|
2023-08-01 14:54:11 +08:00
|
|
|
})
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return simArr
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 根据仿真id查找仿真实例
|
2023-08-01 14:54:11 +08:00
|
|
|
func FindSimulation(simulationId string) *memory.VerifySimulation {
|
|
|
|
m, e := simulationMap.Load(simulationId)
|
|
|
|
if e {
|
|
|
|
return m.(*memory.VerifySimulation)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取普通仿真数组
|
|
|
|
func GetSimulationArr() []*memory.VerifySimulation {
|
2023-08-31 16:16:18 +08:00
|
|
|
var result []*memory.VerifySimulation
|
2023-08-25 15:35:00 +08:00
|
|
|
simulationMap.Range(func(_, v any) bool {
|
2023-08-01 14:54:11 +08:00
|
|
|
result = append(result, v.(*memory.VerifySimulation))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return result
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|