rts-sim-testing-service/ats/verify/simulation/simulation_manage.go
2023-08-01 14:54:11 +08:00

67 lines
1.5 KiB
Go

package simulation
import (
"strconv"
"sync"
"joylink.club/bj-rtsts-server/ats/verify/simulation/wayside/memory"
"joylink.club/bj-rtsts-server/dto"
)
// 仿真存储集合
var simulationMap sync.Map
// 创建仿真对象
func CreateSimulation(mapId int32) string {
simulationId := createSimulationId(mapId)
_, e := simulationMap.Load(simulationId)
if !e {
verifySimulation := memory.CreateSimulation(mapId, simulationId)
simulationMap.Store(simulationId, verifySimulation)
}
return simulationId
}
// 删除仿真对象
func DestroySimulation(simulationId string) {
simulationMap.Delete(simulationId)
}
// 创建时生成仿真Id
func createSimulationId(mapId int32) string {
return strconv.Itoa(int(mapId))
}
// 获取仿真列表
func ListAllSimulations() []*dto.SimulationInfoRepDto {
simArr := []*dto.SimulationInfoRepDto{}
simulationMap.Range(func(k, v any) bool {
s := v.(*memory.VerifySimulation)
simArr = append(simArr, &dto.SimulationInfoRepDto{
SimulationId: s.SimulationId,
MapId: strconv.Itoa(int(s.MapId)),
})
return true
})
return simArr
}
// 根据仿真id查找仿真实例
func FindSimulation(simulationId string) *memory.VerifySimulation {
m, e := simulationMap.Load(simulationId)
if e {
return m.(*memory.VerifySimulation)
}
return nil
}
// 获取普通仿真数组
func GetSimulationArr() []*memory.VerifySimulation {
result := []*memory.VerifySimulation{}
simulationMap.Range(func(k, v any) bool {
result = append(result, v.(*memory.VerifySimulation))
return true
})
return result
}