diff --git a/ats/verify/simulation/wayside/memory/wayside_memory_map.go b/ats/verify/simulation/wayside/memory/wayside_memory_map.go index 890c123..05e30b0 100644 --- a/ats/verify/simulation/wayside/memory/wayside_memory_map.go +++ b/ats/verify/simulation/wayside/memory/wayside_memory_map.go @@ -13,9 +13,11 @@ import ( "google.golang.org/protobuf/proto" "joylink.club/bj-rtsts-server/ats/verify/protos/graphicData" + "joylink.club/bj-rtsts-server/ats/verify/protos/state" "joylink.club/bj-rtsts-server/db/dbquery" "joylink.club/bj-rtsts-server/db/model" "joylink.club/bj-rtsts-server/dto" + "joylink.club/bj-rtsts-server/sys_error" ) var ( @@ -95,13 +97,14 @@ func QueryGiId(name string) int32 { } // 根据区段,道岔偏移量返回linkID和link相对偏移量 -func QueryEcsLinkByDeviceInfo(repo *repository.Repository, mapId int32, id string, devicePort string, offset int64, runDirection bool) (int32, int64, bool, bool, int64) { - if devicePort == "" { +func QueryEcsLinkByDeviceInfo(repo *repository.Repository, mapId int32, status *state.TrainState) (int32, int64, bool, bool, int64) { + id := status.HeadDeviceId + if status.DevicePort == "" { uid := QueryUidByMidAndComId(mapId, id, &graphicData.Section{}) - return sectionMapToEcsLink(repo, uid, offset, runDirection) + return sectionMapToEcsLink(repo, uid, status) } else { uid := QueryUidByMidAndComId(mapId, id, &graphicData.Turnout{}) - return turnoutMapToEcsLink(repo, uid, devicePort, offset, runDirection) + return turnoutMapToEcsLink(repo, uid, status) } } @@ -122,11 +125,15 @@ func getStorageIBPMapData(mapCode string) *graphicData.IBPGraphicStorage { } // 根据物理区段上的偏移量(基于区段A端),找到所在link的linkId与偏移量 -func sectionMapToEcsLink(repo *repository.Repository, id string, offset int64, runDirection bool) (int32, int64, bool, bool, int64) { +func sectionMapToEcsLink(repo *repository.Repository, id string, status *state.TrainState) (int32, int64, bool, bool, int64) { + runDirection := status.RunDirection section := repo.FindPhysicalSection(id) if section == nil { - panic(&dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("地图不存在uid:%s缓存", id)}) + panic(sys_error.New(fmt.Sprintf("地图不存在uid:%s缓存", id))) } + link := section.ALinkPosition().Link() + // 检查link偏移 + status.HeadOffset = checkOffsetInLink(status.HeadOffset, status.TrainLength, link) ao, bo := section.ALinkPosition().Offset(), section.BLinkPosition().Offset() // 是否从A到B,统一坐标 ak, bk := convertRepoBaseKm(repo, section.AKilometer()), convertRepoBaseKm(repo, section.BKilometer()) @@ -148,20 +155,21 @@ func sectionMapToEcsLink(repo *repository.Repository, id string, offset int64, r up = ao > bo } } - linkId, _ := strconv.Atoi(section.ALinkPosition().Link().Identity.Id()) - trainKilometer := concertTrainKilometer(akv, offset, up) + linkId, _ := strconv.Atoi(link.Identity.Id()) + trainKilometer := concertTrainKilometer(akv, status.HeadOffset, up) if ao < bo { - return int32(linkId), ao + offset, up, abDirection, trainKilometer + return int32(linkId), ao + status.HeadOffset, up, abDirection, trainKilometer } else { - return int32(linkId), ao - offset, up, abDirection, trainKilometer + return int32(linkId), ao - status.HeadOffset, up, abDirection, trainKilometer } } // 根据道岔上的偏移量(基于岔心位置),找到所在link的linkId与偏移量 -func turnoutMapToEcsLink(repo *repository.Repository, id string, port string, offset int64, runDirection bool) (int32, int64, bool, bool, int64) { +func turnoutMapToEcsLink(repo *repository.Repository, id string, status *state.TrainState) (int32, int64, bool, bool, int64) { + port, runDirection := status.DevicePort, status.RunDirection turnout := repo.FindTurnout(id) if turnout == nil { - panic(dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("不存在道岔【uid:%s】", id)}) + panic(sys_error.New(fmt.Sprintf("不存在道岔【uid:%s】", id))) } var portPosition *repository.LinkPosition var crossKm, portKm *proto2.Kilometer @@ -176,30 +184,32 @@ func turnoutMapToEcsLink(repo *repository.Repository, id string, port string, of portPosition = turnout.FindLinkPositionByPort(proto2.Port_C) portKm = turnout.GetTurnoutKm(proto2.Port_C) default: - panic(dto.ErrorDto{Code: dto.DataNotExist, Message: fmt.Sprintf("无效端口【%s】偏移量", port)}) + panic(sys_error.New(fmt.Sprintf("无效端口【%s】偏移量", port))) } + // 关联link + link := portPosition.Link() + // 检查link偏移 + status.HeadOffset = checkOffsetInLink(status.HeadOffset, status.TrainLength, link) // 岔心公里标 crossKm = turnout.GetTurnoutKm(proto2.Port_None) portKm, err := repo.ConvertKilometer(portKm, crossKm.CoordinateSystem) if err != nil { - panic(err) + panic(sys_error.New("公里标转换出错", err)) } - // 关联link - link := portPosition.Link() isStart := link.ARelation().Device().Id() == id up := runDirection if (portKm.Value > crossKm.Value) != isStart { up = !runDirection } pointTo := (portKm.Value > crossKm.Value) == runDirection - trainKilometer := concertTrainKilometer(crossKm.Value, offset, pointTo) + trainKilometer := concertTrainKilometer(crossKm.Value, status.HeadOffset, pointTo) linkId, _ := strconv.Atoi(link.Identity.Id()) if isStart { - return int32(linkId), offset, up, pointTo, trainKilometer + return int32(linkId), status.HeadOffset, up, pointTo, trainKilometer } else { // 道岔长度 turnoutLen := int64(math.Abs(float64(portKm.Value - crossKm.Value))) - return int32(linkId), portPosition.Offset() + turnoutLen - offset, up, pointTo, trainKilometer + return int32(linkId), portPosition.Offset() + turnoutLen - status.HeadOffset, up, pointTo, trainKilometer } } @@ -355,3 +365,17 @@ func convertRepoBaseKm(r *repository.Repository, km *proto2.Kilometer) *proto2.K } return k } + +// 检查偏移是否在link上,返回最终的offset +func checkOffsetInLink(offset, trainLen int64, link *repository.Link) int64 { + if offset > link.Length() { + panic(sys_error.New(fmt.Sprintf("偏移【%d】超出link范围【%d】", offset, link.Length()))) + } + if trainLen > offset { // 如果列车长度超过设置offset + offset = trainLen + } + if offset > link.Length() { + panic(sys_error.New(fmt.Sprintf("列车长度【%d】超出link范围【%d】", trainLen, link.Length()))) + } + return offset +} diff --git a/ats/verify/simulation/wayside/memory/wayside_memory_train.go b/ats/verify/simulation/wayside/memory/wayside_memory_train.go index 38178c3..4f3572d 100644 --- a/ats/verify/simulation/wayside/memory/wayside_memory_train.go +++ b/ats/verify/simulation/wayside/memory/wayside_memory_train.go @@ -26,7 +26,7 @@ func AddTrainState(vs *VerifySimulation, status *state.TrainState, mapId int32) //向动力学发送初始化请求 trainIndex, _ := strconv.ParseUint(status.Id, 10, 16) // 映射link、偏移量、运行方向 - linkId, loffset, up, pointTo, kilometer := QueryEcsLinkByDeviceInfo(vs.Repo, mapId, status.HeadDeviceId, status.DevicePort, status.HeadOffset, status.RunDirection) + linkId, loffset, up, pointTo, kilometer := QueryEcsLinkByDeviceInfo(vs.Repo, mapId, status) status.Up = up status.PointTo = pointTo status.TrainKilometer = kilometer