【增加车头车尾寻找物理区段方法】

This commit is contained in:
weizhihong 2023-11-09 14:19:48 +08:00
parent e102a28bb2
commit feb497b5b5
2 changed files with 82 additions and 13 deletions

View File

@ -176,7 +176,7 @@ func findCalcLinkIdAndOffset(sim *VerifySimulation, link *repository.Link, offse
if tp == nil {
panic(sys_error.New("列车偏移超出link位置"))
}
nextPort := getTurnoutNextPort(sim, tp)
nextPort := getTurnoutNextPort(sim, tp.Device().Id(), tp.Port().String())
if nextPort == proto2.Port_None {
panic(sys_error.New("列车偏移超出link位置"))
}
@ -209,37 +209,37 @@ func findCalcLinkIdAndOffset(sim *VerifySimulation, link *repository.Link, offse
// 根据当前link端寻找下一个link连接端端口
// 入参:仿真、道岔端口
// 输出:道岔端口
func getTurnoutNextPort(sim *VerifySimulation, tp *repository.TurnoutPort) proto2.Port {
entry, ok := entity.GetEntityByUid(sim.World, tp.Device().Id())
func getTurnoutNextPort(sim *VerifySimulation, turnoutId string, port string) proto2.Port {
entry, ok := entity.GetEntityByUid(sim.World, turnoutId)
if !ok {
panic(sys_error.New(fmt.Sprintf("道岔不存在: World id=%d,道岔id=%s", sim.World.Id(), tp.Device().Id())))
panic(sys_error.New(fmt.Sprintf("道岔不存在: World id=%d,道岔id=%s", sim.World.Id(), turnoutId)))
}
if !entry.HasComponent(component.TurnoutPositionType) {
panic(sys_error.New(fmt.Sprintf("道岔没有TurnoutPosition组件: World id=%d,道岔id=%s", sim.World.Id(), tp.Device().Id())))
panic(sys_error.New(fmt.Sprintf("道岔没有TurnoutPosition组件: World id=%d,道岔id=%s", sim.World.Id(), turnoutId)))
}
// 获取定反数据
pos := component.TurnoutPositionType.Get(entry)
switch tp.Port() {
case proto2.Port_A:
switch port {
case "A":
if pos.Dw {
return proto2.Port_B
} else {
return proto2.Port_C
}
case proto2.Port_B:
case "B":
if pos.Dw {
return proto2.Port_A
} else {
return proto2.Port_None
}
case proto2.Port_C:
case "C":
if pos.Dw {
return proto2.Port_None
} else {
return proto2.Port_A
}
}
panic(sys_error.New(fmt.Sprintf("非法端口:端口=%s", tp.Port().String())))
panic(sys_error.New(fmt.Sprintf("非法端口:端口=%s", port)))
}
// 计算link offset 在道岔上的位置
@ -542,3 +542,60 @@ func turnoutOffsetToKilometer(repo *repository.Repository, uid string, runDirect
}
return
}
// 根据起始与末尾端查找经过的设备ID列表
// 入参起始设备uid、起始设备端口、终点设备uid、在区段a->b或道岔->岔心)上的找寻方向
// 输出占用物理区段的ID
func QueryDeviceRoutePath(sim *VerifySimulation, startUid, endUid, startPort string, pointTo bool) []string {
suid, euid, port, direction := startUid, endUid, startPort, pointTo
var paths []string
for {
device := sim.Repo.FindById(suid)
var nextRelation repository.DevicePort
switch device.Type() {
case proto2.DeviceType_DeviceType_PhysicalSection: // 区段
d := device.(*repository.PhysicalSection)
paths = append(paths, d.Id())
nextRelation = d.ARelation()
if direction {
nextRelation = d.BRelation()
}
case proto2.DeviceType_DeviceType_Turnout: // 道岔
d := device.(*repository.Turnout)
if d.GetPhysicalSection() == nil {
panic(sys_error.New(fmt.Sprintf("道岔%s没有绑定物理区段", device.Id())))
}
paths = append(paths, d.GetPhysicalSection().Id())
var nextPort proto2.Port
switch port {
case "A":
nextPort = proto2.Port_A
case "B":
nextPort = proto2.Port_B
case "C":
nextPort = proto2.Port_C
}
if pointTo { // -> 岔心
nextPort = getTurnoutNextPort(sim, d.Id(), port)
}
nextRelation = d.GindDevicePortByPort(nextPort)
default:
panic(sys_error.New(fmt.Sprintf("查找路径设备类型%s无处理处理", device.Type().String())))
}
if nextRelation == nil {
break
}
if suid == euid {
break
}
suid = nextRelation.Device().Id() // 下一个设备ID
port = nextRelation.Port().String() // 下一个道岔端
switch nextRelation.Device().Type() { // 查找方向
case proto2.DeviceType_DeviceType_PhysicalSection:
direction = nextRelation.Port() == proto2.Port_A
case proto2.DeviceType_DeviceType_Turnout:
direction = true
}
}
return paths
}

View File

@ -282,9 +282,19 @@ func convert(info *message.DynamicsTrainInfo, sta *state.TrainState, simulation
delayTime := time.Now().UnixMilli() - sta.VobcState.UpdateTime
sta.ControlDelayTime = (int64(sta.VobcState.LifeSignal)-int64(info.VobcLifeSignal))*20 + delayTime
slog.Debug("收到动力学原始消息", "Number", info.Number, "Link", info.Link, "LinkOffset", info.LinkOffset)
_, id, port, _, offset, kilometer := CalcInitializeLink(simulation, strconv.Itoa(int(info.Link)), int64(info.LinkOffset), info.Up)
inLinkId, inLinkOffset := strconv.Itoa(int(info.Link)), int64(info.LinkOffset)
outLinkId, id, port, outLinkOffset, offset, kilometer := CalcInitializeLink(simulation, inLinkId, inLinkOffset, info.Up)
runDirection, pointTo := QueryDirectionAndABByDevice(simulation.Repo, id, port, info.Up)
slog.Debug("处理动力学转换后的消息", "number", info.Number, "车头位置", id, "偏移", offset, "是否上行", runDirection, "是否ab", pointTo)
// 车尾相对车头link的偏移量
var calctailOffset int64
if info.Up {
calctailOffset = outLinkOffset - sta.TrainLength
} else {
calctailOffset = outLinkOffset + sta.TrainLength
}
tailLinkId, tailDeviceId, tailDevicePort, tailLinkOffset, tailDeviceOffset, _ := CalcInitializeLink(simulation, outLinkId, calctailOffset, info.Up)
slog.Debug("车未位置", tailDeviceId, "偏移", tailDeviceOffset, "所在设备端", tailDevicePort)
sta.HeadDeviceId = simulation.GetComIdByUid(id)
sta.DevicePort = port
sta.HeadOffset = offset
@ -305,8 +315,10 @@ func convert(info *message.DynamicsTrainInfo, sta *state.TrainState, simulation
}
// 赋值动力学信息
sta.DynamicState.Heartbeat = int32(info.LifeSignal)
sta.DynamicState.HeadLinkId = strconv.Itoa(int(info.Link))
sta.DynamicState.HeadLinkOffset = int64(info.LinkOffset)
sta.DynamicState.HeadLinkId = outLinkId
sta.DynamicState.HeadLinkOffset = outLinkOffset
sta.DynamicState.TailLinkId = tailLinkId
sta.DynamicState.TailLinkOffset = tailLinkOffset
sta.DynamicState.Slope = int32(info.Slope)
sta.DynamicState.Upslope = info.UpSlope
sta.DynamicState.RunningUp = info.Up