rts-sim-testing-service/ats/verify/simulation/simulation_manage.go

154 lines
4.2 KiB
Go
Raw Normal View History

2023-07-31 08:41:42 +08:00
package simulation
import (
"net"
"strconv"
"strings"
"sync"
"go.uber.org/zap"
"joylink.club/bj-rtsts-server/ats/verify/protos/state"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/dynamics"
2023-07-31 08:41:42 +08:00
2023-08-01 14:54:11 +08:00
"joylink.club/bj-rtsts-server/ats/verify/simulation/wayside/memory"
"joylink.club/bj-rtsts-server/dto"
2023-07-31 08:41:42 +08:00
)
var simulationId_prefix = (func() string {
ip := "127.0.0.1"
addrList, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, address := range addrList {
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
ip = ipNet.IP.String()
}
}
}
ipArr := strings.Split(ip, ".")
return ipArr[2] + "_" + ipArr[3]
})()
func init() {
dynamics.RegisterTrainInfoHandler(func(info *dynamics.TrainInfo) {
for _, simulation := range GetSimulationArr() {
sta, ok := simulation.Memory.Status.TrainStateMap.Load(strconv.Itoa(int(info.Number)))
if ok {
memory.UpdateTrainState(simulation, convert(info, *sta.(*state.TrainState)))
break
}
}
//simulationMap.Range(func(key, value any) bool {
// simulation := value.(*memory.VerifySimulation)
// sta, ok := simulation.Memory.Status.TrainStateMap.Load(strconv.Itoa(int(info.Number)))
// if ok {
// memory.UpdateTrainState(simulation, convert(info, *sta.(*state.TrainState)))
// return false
// }
// return true
//})
})
}
func convert(info *dynamics.TrainInfo, sta state.TrainState) *state.TrainState {
sta.HeadLinkId = strconv.Itoa(int(info.Link))
sta.HeadLinkOffset = int64(info.LinkOffset * 10)
sta.Slope = int32(info.Slope)
sta.Upslope = info.UpSlope
sta.RunningUp = info.Up
sta.RunningResistanceSum = info.TotalResistance
sta.AirResistance = info.AirResistance
sta.RampResistance = info.SlopeResistance
sta.CurveResistance = info.CurveResistance
sta.Speed = info.Speed
sta.HeadSensorSpeed1 = info.HeadSpeed1
sta.HeadSensorSpeed2 = info.HeadSpeed2
sta.TailSensorSpeed1 = info.TailSpeed1
sta.TailSensorSpeed2 = info.TailSpeed2
sta.HeadRadarSpeed = info.HeadRadarSpeed
sta.TailRadarSpeed = info.TailRadarSpeed
return &sta
}
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-01 14:54:11 +08:00
func CreateSimulation(mapId int32) string {
2023-07-31 08:41:42 +08:00
simulationId := createSimulationId(mapId)
2023-08-01 14:54:11 +08:00
_, e := simulationMap.Load(simulationId)
if !e {
verifySimulation := memory.CreateSimulation(mapId, simulationId)
simulationMap.Store(simulationId, verifySimulation)
//道岔状态发送
dynamics.AddTurnoutInfoFunc(simulationId, func() []*dynamics.TurnoutInfo {
stateSlice := memory.GetAllTurnoutState(verifySimulation)
var turnoutInfoSlice []*dynamics.TurnoutInfo
for _, sta := range stateSlice {
code64, err := strconv.ParseUint(sta.Id, 10, 16)
if err != nil {
zap.S().Error("id转uint16报错", err)
}
info := dynamics.TurnoutInfo{
Code: uint16(code64),
NPosition: sta.Normal,
RPosition: sta.Reverse,
}
turnoutInfoSlice = append(turnoutInfoSlice, &info)
}
return turnoutInfoSlice
})
//
2023-07-31 08:41:42 +08:00
}
return simulationId
}
// 删除仿真对象
func DestroySimulation(simulationId string) {
2023-08-01 14:54:11 +08:00
simulationMap.Delete(simulationId)
//移除道岔状态发送
dynamics.RemoveTurnoutInfoFunc(simulationId)
2023-07-31 08:41:42 +08:00
}
// 创建时生成仿真Id
2023-08-01 14:54:11 +08:00
func createSimulationId(mapId int32) string {
// 当前服务器IP + 端口 + 地图
return simulationId_prefix + "_" + strconv.Itoa(config.Config.Server.Port) + "_" + strconv.Itoa(int(mapId))
2023-07-31 08:41:42 +08:00
}
// 获取仿真列表
2023-08-01 14:54:11 +08:00
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
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 {
result := []*memory.VerifySimulation{}
simulationMap.Range(func(k, v any) bool {
result = append(result, v.(*memory.VerifySimulation))
return true
})
return result
2023-07-31 08:41:42 +08:00
}