diff --git a/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java b/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java index 0ef6379..b7e4391 100644 --- a/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java +++ b/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java @@ -1,5 +1,6 @@ package club.joylink.xiannccda.ats.cache; +import club.joylink.xiannccda.ats.message.line3.DeviceNameChanger; import club.joylink.xiannccda.dto.protos.DeviceInfoProto; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.KilometerSystem; @@ -18,15 +19,21 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -/** 发布时缓存在内存中的线路数据 */ +/** + * 发布时缓存在内存中的线路数据 + */ @Slf4j public class LineGraphicDataRepository { - /** 线路数据信息 */ + /** + * 线路数据信息 + */ private static final Map>> lineGraphMap = new ConcurrentHashMap<>(); - /** 线路各坐标系转换关系 */ + /** + * 线路各坐标系转换关系 + */ private static final Map> lineCoordinateMain = new HashMap<>(); @@ -40,6 +47,7 @@ public class LineGraphicDataRepository { if (publishGi.getLineId() != null) { LayoutGraphicsProto.RtssGraphicStorage storage = LayoutGraphicsProto.RtssGraphicStorage.parseFrom(publishGi.getProto()); + DeviceNameChanger.init(storage); Map> lineDataMap = new HashMap<>(); // 构建区段 sectionInitLineGraph(publishGi.getLineId(), lineDataMap, storage); @@ -57,7 +65,7 @@ public class LineGraphicDataRepository { /** * 获取线路上区段名为【sectionName】的公里标 * - * @param lineId 线路ID + * @param lineId 线路ID * @param sectionName 区段名称 * @return 公里标 */ @@ -83,7 +91,9 @@ public class LineGraphicDataRepository { return List.of(); } - /** 删除线路绘图数据 */ + /** + * 删除线路绘图数据 + */ public static void removeLineGraph(Integer id) { lineGraphMap.remove(id); } @@ -91,7 +101,7 @@ public class LineGraphicDataRepository { /** * 构建程序中的区段信息 * - * @param lineId 线路ID + * @param lineId 线路ID * @param dataMap 缓存数据 * @param storage 地图构建数据 */ @@ -120,7 +130,7 @@ public class LineGraphicDataRepository { /** * 构建道岔信息 * - * @param lineId 线路ID + * @param lineId 线路ID * @param dataMap 缓存数据 * @param storage 地图构建数据 */ @@ -171,7 +181,7 @@ public class LineGraphicDataRepository { /** * 设置公里标 * - * @param lineId 线路ID + * @param lineId 线路ID * @param dataMap 缓存数据 * @param storage 地图构建数据 */ @@ -222,7 +232,7 @@ public class LineGraphicDataRepository { /** * 初始化坐标转换对象 * - * @param lineId 线路ID + * @param lineId 线路ID * @param dataMap 设备集合 * @param storage 地图信息 */ @@ -314,7 +324,9 @@ public class LineGraphicDataRepository { return convertorMap.get(convertorKey).convertorKilometer(kilometer, CoordinateEnum.MAIN_LINE); } - /** 坐标系枚举 */ + /** + * 坐标系枚举 + */ @Getter private enum CoordinateEnum { MAIN_LINE("MAIN_LINE", List.of("YDK", "ZDK", "XDK", "SDK")), @@ -359,7 +371,9 @@ public class LineGraphicDataRepository { } } - /** 坐标转换对象 */ + /** + * 坐标转换对象 + */ @Getter private static class CoordinateConvertor { @@ -374,8 +388,8 @@ public class LineGraphicDataRepository { /** * 生成坐标转换对象 * - * @param configSystem 原配置坐标系信息 - * @param convertorSystem 转换坐标系类型 + * @param configSystem 原配置坐标系信息 + * @param convertorSystem 转换坐标系类型 * @param convertorCoordinate 转换坐标 * @return 转换对象 */ @@ -427,7 +441,7 @@ public class LineGraphicDataRepository { * 将出入库公里标转换为正线标 * * @param basisKilometer 出入库基准公里标 - * @param targetType 目标坐标系 + * @param targetType 目标坐标系 * @return 正线公里标数字 */ public long convertorKilometer(KilometerSystem basisKilometer, CoordinateEnum targetType) { diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java index e299995..22107e2 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java @@ -1,14 +1,28 @@ package club.joylink.xiannccda.ats.message.line3; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.RtssGraphicStorage; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Signal; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Turnout; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.protobuf.GeneratedMessageV3; +import com.google.protobuf.Message; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; public abstract class DeviceNameChanger { -// abstract List findAllDeviceName(JSONArray dataArray); + abstract void collectNames(LayoutGraphicsProto.RtssGraphicStorage graphicStorage); - abstract String changeDeviceName(String nccDeviceName); + abstract String changeDeviceName(String nccDeviceName, List graphicNames); static Map NCC_DEVICE_TYPE_NAME_MAPPER = Maps.newHashMap(); @@ -19,17 +33,36 @@ public abstract class DeviceNameChanger { // NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_PLATFORM, "stationStandList"); } + private final static Map> DEVICE_NAMES = Maps.newHashMap(); + + + public static void init(LayoutGraphicsProto.RtssGraphicStorage gs) { + for (DeviceNameChanger value : NCC_DEVICE_TYPE_NAME_MAPPER.values()) { + value.collectNames(gs); + } + } + + + public static String nameMatch(DeviceType dt, String name) { + DeviceNameChanger nameChanger = NCC_DEVICE_TYPE_NAME_MAPPER.get(dt); + if (Objects.nonNull(nameChanger)) { + List deviceNames = DEVICE_NAMES.get(dt); + nameChanger.changeDeviceName(name, deviceNames); + } + return name; + } + public static class SwitchDevice extends DeviceNameChanger { - /* @Override - public List findAllDeviceName(JSONArray dataArray) { - List mapDeviceNames = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).map(d -> d.getString("name")).toList(); - return mapDeviceNames; - }*/ + @Override + void collectNames(RtssGraphicStorage graphicStorage) { + List names = graphicStorage.getTurnoutsList().stream().map(Turnout::getCode).toList(); + DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SWITCH, names); + } @Override - public String changeDeviceName(String nccDeviceName) { + public String changeDeviceName(String nccDeviceName, List graphicNames) { if (nccDeviceName.length() < 3) { return nccDeviceName; } @@ -43,89 +76,105 @@ public abstract class DeviceNameChanger { public static class TrackDevice extends DeviceNameChanger { -/* @Override - List findAllDeviceName(JSONArray dataArray) { - Map jsonMap = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).collect(Collectors.toMap((d) -> d.getString("code"), Function.identity())); - List newNames = Lists.newArrayList(); - for (JSONObject value : jsonMap.values()) { -// JSONArray hasLogic = value.getJSONArray("logicSectionList"); - String physicsTrackCode = value.getString("parentCode"); - String name = value.getString("name"); - if (StringUtils.isEmpty(physicsTrackCode)) { - newNames.add(name); - } else { - JSONObject physicsTrack = jsonMap.get(physicsTrackCode); - String pn = physicsTrack.getString("name"); - newNames.add(pn + name); - - } - } - return newNames; - }*/ @Override - String changeDeviceName(String nccDeviceName) { + void collectNames(RtssGraphicStorage graphicStorage) { + List names = graphicStorage.getSectionList().stream().map(Section::getCode).toList(); + DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_TRACK, names); + } + + @Override + String changeDeviceName(String nccDeviceName, List graphicNames) { if (nccDeviceName.length() < 4) { return nccDeviceName; } - if (nccDeviceName.startsWith("T") && nccDeviceName.contains("_")) { - return nccDeviceName.substring(1); - } else if (nccDeviceName.matches("^[A-Za-z]{2}.*?")) { - String head = nccDeviceName.substring(2, 4); - String tail = nccDeviceName.substring(5); - String tailEnd = ""; - if (tail.matches(".*?[A-Z]$")) { - tail = nccDeviceName.substring(4, nccDeviceName.length() - 1); - tailEnd = nccDeviceName.substring(nccDeviceName.length() - 1); + Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); + Matcher matcher = regex.matcher(nccDeviceName); + String newName = ""; + String code = ""; + if (matcher.find()) { + code = matcher.group(2); + if (StringUtils.contains(code, "_")) { + newName = code; + } else { + String head = code.substring(0, 2); + String end = code.substring(3); + String tail = ""; + if (code.matches(".*?[A-Z]$")) { + tail = end.substring(end.length() - 1); + } + newName = end + head + tail; } - return "G" + tail + head + tailEnd; - } else if (nccDeviceName.matches(".*?[A-Z]$")) { - String head = nccDeviceName.substring(1, 3); - String tail = nccDeviceName.substring(4); - String tailEnd = ""; - if (tail.matches(".*?[A-Z]$")) { - tail = nccDeviceName.substring(4, nccDeviceName.length() - 1); - tailEnd = nccDeviceName.substring(nccDeviceName.length() - 1); - } - return "G" + tail + head + tailEnd; - } else { - return "S" + nccDeviceName; } + final String finalName = newName; + final String finalCode = code; + String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null); + if (Objects.nonNull(finder)) { + return finder; + } + return nccDeviceName; } } public static class SignalDevice extends DeviceNameChanger { -/* @Override - List findAllDeviceName(JSONArray dataArray) { - List mapDeviceNames = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).map(d -> d.getString("name")).toList(); - return mapDeviceNames; - }*/ + @Override + void collectNames(RtssGraphicStorage graphicStorage) { + List names = graphicStorage.getSignalsList().stream().map(Signal::getCode).toList(); + DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SIGNAL, names); + } @Override - String changeDeviceName(String nccDeviceName) { + String changeDeviceName(String nccDeviceName, List graphicNames) { if (nccDeviceName.length() <= 3) { return nccDeviceName; } - String started = nccDeviceName.substring(0, 1); - String head = nccDeviceName.substring(1, 3); - String tail = nccDeviceName.substring(4); - return started + tail + head; + Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); + Matcher matcher = regex.matcher(nccDeviceName); + String newName = ""; + String code = ""; + if (matcher.find()) { + code = matcher.group(2); + if (StringUtils.contains(code, "_")) { + newName = code; + } else { + String head = code.substring(0, 2); + String end = code.substring(3); + String tail = ""; + if (code.matches(".*?[A-Z]$")) { + tail = end.substring(end.length() - 1); + } + newName = end + head + tail; + } + } + final String finalName = newName.replaceAll("_", "-"); + final String finalCode = code.replaceAll("_", "-"); + String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null); + if (Objects.nonNull(finder)) { + return finder; + } + return nccDeviceName; } } -/* public static class PlatformDevice extends Device { - - @Override - List findAllDeviceName(JSONArray dataArray) { - return null; + public static void main(String[] args) { + String nccDeviceName = "TP_2DG"; + Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); + Matcher matcher = regex.matcher(nccDeviceName); + String newName = ""; + String code = ""; + if (matcher.find()) { + code = matcher.group(2); + String head = code.substring(0, 2); + String end = code.substring(3); + String tail = ""; + if (code.matches(".*?[A-Z]$")) { + tail = end.substring(end.length() - 1); + } + newName = end + head + tail; + System.out.println(newName); } - - @Override - String changeDeviceName(String nccDeviceName) { - return null; - } - }*/ + } }