2023-07-31 08:41:42 +08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
2023-08-03 10:41:43 +08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-07 14:08:02 +08:00
|
|
|
"fmt"
|
2023-08-10 14:18:55 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2023-08-02 15:47:48 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"joylink.club/bj-rtsts-server/ats/verify/protos/state"
|
2023-08-03 10:41:43 +08:00
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-08-02 15:47:48 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-08-03 10:41:43 +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]
|
|
|
|
})()
|
|
|
|
|
2023-08-02 15:47:48 +08:00
|
|
|
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 {
|
2023-08-10 14:18:55 +08:00
|
|
|
memory.UpdateTrainState(simulation, convert(info, sta.(*state.TrainState), memory.QueryMapVerifyStructure(simulation.MapId)))
|
2023-08-02 15:47:48 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
2023-08-09 15:34:19 +08:00
|
|
|
//通知动力学
|
|
|
|
httpCode, _, err := dynamics.SendSimulationStartReq(buildLineBaseInfo(memory.QueryMapVerifyStructure(verifySimulation.MapId)))
|
|
|
|
if httpCode != http.StatusOK || err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.LogicError, Message: fmt.Sprintf("动力学接口调用失败:[%d][%s]", httpCode, err)})
|
|
|
|
}
|
2023-08-01 14:54:11 +08:00
|
|
|
simulationMap.Store(simulationId, verifySimulation)
|
2023-08-09 15:34:19 +08:00
|
|
|
dynamicsRun(verifySimulation)
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
return simulationId
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除仿真对象
|
|
|
|
func DestroySimulation(simulationId string) {
|
2023-08-02 15:47:48 +08:00
|
|
|
//移除道岔状态发送
|
2023-08-09 15:34:19 +08:00
|
|
|
dynamics.Stop()
|
2023-08-07 14:08:02 +08:00
|
|
|
//通知动力学
|
|
|
|
httpCode, _, err := dynamics.SendSimulationEndReq()
|
|
|
|
if httpCode != http.StatusOK || err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.LogicError, Message: fmt.Sprintf("动力学接口调用失败:[%d][%s]", httpCode, err)})
|
|
|
|
}
|
|
|
|
simulationMap.Delete(simulationId)
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建时生成仿真Id
|
2023-08-01 14:54:11 +08:00
|
|
|
func createSimulationId(mapId int32) string {
|
2023-08-03 10:41:43 +08:00
|
|
|
// 当前服务器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
|
|
|
}
|
2023-08-07 14:08:02 +08:00
|
|
|
|
2023-08-10 14:18:55 +08:00
|
|
|
func convert(info *dynamics.TrainInfo, sta *state.TrainState, vs *memory.VerifyStructure) *state.TrainState {
|
2023-08-09 15:34:19 +08:00
|
|
|
zap.S().Debugf("原始消息:[%d-%d-%d]", info.Number, info.Link, info.LinkOffset)
|
2023-08-10 14:18:55 +08:00
|
|
|
sta.HeadLinkId = strconv.Itoa(int(info.Link))
|
|
|
|
sta.HeadLinkOffset = int64(info.LinkOffset)
|
2023-08-15 10:18:39 +08:00
|
|
|
id, port, offset, runDirection, pointTo := memory.QueryDeviceByCalcLink(vs, int32(info.Link), sta.HeadLinkOffset, sta.Up, sta.RunDirection)
|
|
|
|
zap.S().Debugf("转换后的消息:[%d-车头:%s-偏移:%d-上行:%v-是否ab:%v]", info.Number, id, offset, runDirection, pointTo)
|
2023-08-10 14:18:55 +08:00
|
|
|
sta.HeadDeviceId = id
|
|
|
|
sta.DevicePort = port
|
|
|
|
sta.HeadOffset = offset
|
2023-08-15 10:18:39 +08:00
|
|
|
sta.PointTo = pointTo
|
2023-08-14 16:27:03 +08:00
|
|
|
sta.RunDirection = runDirection
|
2023-08-10 14:23:18 +08:00
|
|
|
/*
|
|
|
|
modeller := vs.LinkModelMap[int32(info.Link)]
|
|
|
|
model := modeller.(*device.LinkModel)
|
|
|
|
for i, dp := range model.DevicePositions {
|
|
|
|
if uint32(dp.Offset) >= info.LinkOffset {
|
|
|
|
var minLinkRef *graphicData.RelatedRef
|
|
|
|
var minOffset int32
|
|
|
|
var maxOffset int32
|
|
|
|
if i == 0 {
|
|
|
|
minLinkRef = model.SectionLinkMap[dp.Device.GetGraphicId()]
|
|
|
|
minOffset = dp.Offset
|
|
|
|
maxOffset = model.DevicePositions[i+1].Offset
|
|
|
|
} else {
|
|
|
|
minLinkRef = model.SectionLinkMap[model.DevicePositions[i-1].Device.GetGraphicId()]
|
|
|
|
minOffset = model.DevicePositions[i-1].Offset
|
|
|
|
maxOffset = dp.Offset
|
|
|
|
}
|
|
|
|
switch minLinkRef.DevicePort {
|
|
|
|
case 0:
|
|
|
|
sta.HeadLinkId = minLinkRef.GetId()
|
|
|
|
sta.HeadLinkOffset = int64(info.LinkOffset - uint32(minOffset))
|
|
|
|
case 1:
|
|
|
|
sta.HeadLinkId = minLinkRef.GetId()
|
|
|
|
sta.HeadLinkOffset = int64(uint32(maxOffset) - info.LinkOffset)
|
|
|
|
}
|
|
|
|
break
|
2023-08-09 15:34:19 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-10 14:23:18 +08:00
|
|
|
zap.S().Debugf("转换后的消息:[%d-%s-%d]", info.Number, sta.HeadLinkId, sta.HeadLinkOffset)
|
|
|
|
*/
|
2023-08-07 14:08:02 +08:00
|
|
|
sta.Slope = int32(info.Slope)
|
|
|
|
sta.Upslope = info.UpSlope
|
|
|
|
sta.RunningUp = info.Up
|
|
|
|
sta.RunningResistanceSum = float32(info.TotalResistance) / 1000
|
|
|
|
sta.AirResistance = float32(info.AirResistance) / 1000
|
|
|
|
sta.RampResistance = float32(info.SlopeResistance) / 1000
|
|
|
|
sta.CurveResistance = float32(info.CurveResistance) / 1000
|
|
|
|
sta.Speed = info.Speed * 3.6
|
|
|
|
sta.HeadSensorSpeed1 = info.HeadSpeed1 * 3.6
|
|
|
|
sta.HeadSensorSpeed2 = info.HeadSpeed2 * 3.6
|
|
|
|
sta.TailSensorSpeed1 = info.TailSpeed1 * 3.6
|
|
|
|
sta.TailSensorSpeed2 = info.TailSpeed2 * 3.6
|
|
|
|
sta.HeadRadarSpeed = info.HeadRadarSpeed * 3.6
|
|
|
|
sta.TailRadarSpeed = info.TailRadarSpeed * 3.6
|
2023-08-10 14:18:55 +08:00
|
|
|
return sta
|
2023-08-07 14:08:02 +08:00
|
|
|
}
|
|
|
|
|
2023-08-09 15:34:19 +08:00
|
|
|
func dynamicsRun(verifySimulation *memory.VerifySimulation) {
|
|
|
|
_ = dynamics.Run(func() []*dynamics.TurnoutInfo {
|
2023-08-07 14:08:02 +08:00
|
|
|
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-08-09 15:34:19 +08:00
|
|
|
|
|
|
|
func buildLineBaseInfo(vs *memory.VerifyStructure) *dynamics.LineBaseInfo {
|
|
|
|
var links []*dynamics.Link
|
2023-08-11 17:24:30 +08:00
|
|
|
var slopes []*dynamics.Slope
|
|
|
|
var curves []*dynamics.Curve
|
|
|
|
for _, link := range vs.LinkModelMap {
|
2023-08-09 15:34:19 +08:00
|
|
|
id, _ := strconv.Atoi(link.Index)
|
|
|
|
var aTurnoutId int
|
|
|
|
if link.ARelatedSwitchRef.SwitchDevice != nil {
|
|
|
|
aTurnoutId, _ = strconv.Atoi(link.ARelatedSwitchRef.SwitchDevice.GetIndex())
|
|
|
|
}
|
|
|
|
var bTurnoutId int
|
|
|
|
if link.BRelatedSwitchRef.SwitchDevice != nil {
|
|
|
|
bTurnoutId, _ = strconv.Atoi(link.BRelatedSwitchRef.SwitchDevice.GetIndex())
|
|
|
|
}
|
|
|
|
links = append(links, &dynamics.Link{
|
|
|
|
ID: int32(id),
|
|
|
|
Len: link.Length,
|
|
|
|
ARelTurnoutId: int32(aTurnoutId),
|
|
|
|
ARelTurnoutPoint: link.ARelatedSwitchRef.Port.Name(),
|
|
|
|
BRelTurnoutId: int32(bTurnoutId),
|
|
|
|
BRelTurnoutPoint: link.BRelatedSwitchRef.Port.Name(),
|
|
|
|
})
|
|
|
|
}
|
2023-08-11 17:24:30 +08:00
|
|
|
for _, slope := range vs.SlopeModelMap {
|
|
|
|
id, _ := strconv.Atoi(slope.Index)
|
|
|
|
slopes = append(slopes, &dynamics.Slope{
|
|
|
|
ID: int32(id),
|
|
|
|
StartLinkId: slope.StartLinkIndex,
|
|
|
|
StartLinkOffset: slope.StartLinkOffset,
|
|
|
|
EndLinkId: slope.EndLinkIndex,
|
|
|
|
EndLinkOffset: slope.EndLinkOffset,
|
|
|
|
DegreeTrig: slope.DegreeTrig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, curve := range vs.CurveModelMap {
|
|
|
|
id, _ := strconv.Atoi(curve.Index)
|
|
|
|
curves = append(curves, &dynamics.Curve{
|
|
|
|
ID: int32(id),
|
|
|
|
StartLinkId: curve.StartLinkIndex,
|
|
|
|
StartLinkOffset: curve.StartLinkOffset,
|
|
|
|
EndLinkId: curve.EndLinkIndex,
|
|
|
|
EndLinkOffset: curve.EndLinkOffset,
|
|
|
|
Curvature: curve.Curvature,
|
|
|
|
})
|
|
|
|
}
|
2023-08-09 15:34:19 +08:00
|
|
|
return &dynamics.LineBaseInfo{
|
|
|
|
LinkList: links,
|
2023-08-11 17:24:30 +08:00
|
|
|
SlopeList: slopes,
|
|
|
|
CurveList: curves,
|
2023-08-09 15:34:19 +08:00
|
|
|
}
|
|
|
|
}
|