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 3aded83..586d0ec 100644 --- a/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java +++ b/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java @@ -167,21 +167,42 @@ public class LineGraphicDataRepository { initConvertorCoordinate(lineId, dataMap, storage); Map sectionMap = dataMap.getOrDefault(DeviceType.Section.name(), Map.of()); - storage - .getAxleCountingsList() + Map turnoutMap = + dataMap.getOrDefault(DeviceType.Turnout.name(), Map.of()); + storage.getAxleCountingsList().stream() + .filter(ac -> StringUtils.isNotEmpty(ac.getKilometerSystem().getCoordinateSystem())) .forEach( - ac -> - ac.getAxleCountingRefList().stream() - .filter( - ref -> - DeviceType.Section.equals(ref.getDeviceType()) - && sectionMap.containsKey(ref.getId())) - .forEach( - ref -> { - DeviceInfoProto.Section.Builder sectionBuilder = - (DeviceInfoProto.Section.Builder) sectionMap.get(ref.getId()); - sectionBuilder.addKilometerSystem(ac.getKilometerSystem()); - })); + ac -> { + long mainLineCoordinate = doConvertorCoordinate(lineId, ac.getKilometerSystem()); + ac.getAxleCountingRefList() + .forEach( + ref -> { + if (Objects.equals(DeviceType.Section, ref.getDeviceType())) { + if (sectionMap.containsKey(ref.getId())) { + DeviceInfoProto.Section.Builder sectionBuilder = + (DeviceInfoProto.Section.Builder) sectionMap.get(ref.getId()); + sectionBuilder.addKilometerSystem(ac.getKilometerSystem()); + sectionBuilder.addConvertKilometer(mainLineCoordinate); + } + } else if (Objects.equals(DeviceType.Turnout, ref.getDeviceType())) { + if (turnoutMap.containsKey(ref.getId())) { + DeviceInfoProto.Turnout.Builder turnoutBuilder = + (DeviceInfoProto.Turnout.Builder) turnoutMap.get(ref.getId()); + // 如果已经转换过则不再做转换 + if (turnoutBuilder.getConvertKilometerCount() == 0) { + turnoutBuilder.getKilometerSystemList().stream() + .filter(s -> StringUtils.isNotEmpty(s.getCoordinateSystem())) + .forEach( + s -> + turnoutBuilder.addConvertKilometer( + doConvertorCoordinate(lineId, s))); + } + // 放入当前计轴转换信息 + turnoutBuilder.addConvertKilometer(mainLineCoordinate); + } + } + }); + }); } /** @@ -255,13 +276,34 @@ public class LineGraphicDataRepository { }); } + /** + * 公里标转换 + * + * @param kilometer 公里标 + * @return 转换后的值 + */ + private static long doConvertorCoordinate(Integer lineId, KilometerSystem kilometer) { + if (CoordinateEnum.MAIN_LINE.matchEnum(kilometer.getCoordinateSystem())) { + return kilometer.getKilometer(); + } + Map convertorMap = lineCoordinateMain.get(lineId); + String convertorKey = + CoordinateConvertor.generateConvertorKey( + kilometer.getCoordinateSystem(), CoordinateEnum.MAIN_LINE); + if (CollectionUtils.isEmpty(convertorMap) || !convertorMap.containsKey(convertorKey)) { + log.error("无坐标转换规则" + convertorKey); + return kilometer.getKilometer(); + } + return convertorMap.get(convertorKey).convertorKilometer(kilometer, CoordinateEnum.MAIN_LINE); + } + /** 坐标系枚举 */ @Getter private enum CoordinateEnum { - MAIN_LINE("正线", List.of("YDK", "ZDK", "XDK", "SDK")), - PARK_LINE("停车场", List.of("CDK", "RDK")), - DEPOT_LINE("车辆段", List.of("CDK", "RDK")), - CHANGE_LINE("换线", List.of("L")), + MAIN_LINE("MAIN_LINE", List.of("YDK", "ZDK", "XDK", "SDK")), + PARK_LINE("PARKING_LOT", List.of("CDK", "RDK")), + DEPOT_LINE("DEPOT", List.of("CDK", "RDK")), + CHANGE_LINE("TRANSFER", List.of("L")), UN_KNOW("未知", List.of()); private final String title; @@ -341,6 +383,17 @@ public class LineGraphicDataRepository { return generateConvertorKey(CoordinateEnum.match(type1), CoordinateEnum.match(type2)); } + /** + * 生成转换规则key + * + * @param type1 坐标轴类型 + * @param type2 坐标轴类型 + * @return 规则KEY + */ + public static String generateConvertorKey(String type1, CoordinateEnum type2) { + return generateConvertorKey(CoordinateEnum.match(type1), type2); + } + /** * 是否可以转换这个坐标系 * @@ -361,16 +414,16 @@ public class LineGraphicDataRepository { * @return 正线公里标数字 */ public long convertorKilometer(KilometerSystem basisKilometer, CoordinateEnum targetType) { - if (canConvertor(basisKilometer.getCoordinateSystem(), targetType)) { + if (!canConvertor(basisKilometer.getCoordinateSystem(), targetType)) { throw new IllegalArgumentException( String.format( "非【%s<>%s】转换方法", basisKilometer.getCoordinateSystem(), targetType.getTitle())); } // x-y = w-z => w = x - y + z, yz代表原点位置,x代表传入坐标 if (targetType.equals(configType)) { - return basisKilometer.getKilometer() - configCoordinate + convertorCoordinate; + return configCoordinate + basisKilometer.getKilometer() - convertorCoordinate; } else { - return basisKilometer.getKilometer() - convertorCoordinate + configCoordinate; + return convertorCoordinate + basisKilometer.getKilometer() - configCoordinate; } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRecordConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRecordConvertor.java index 6cdcf0b..3050e36 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRecordConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRecordConvertor.java @@ -48,7 +48,15 @@ public class LineNetTrainRecordConvertor extends DeviceStatusConvertor { LineGraphicDataRepository.getSectionDefaultKmCodeList( response.getLineId(), response.getTrackName()); if (CollectionUtils.isNotEmpty(kmCodeList)) { - offset.setKilometerCode(kmCodeList.get(0)); + long kilometer; + if (offset.getDir() == 1) { // 下行取大值 + kilometer = kmCodeList.stream().max(Long::compareTo).get(); + } else if (offset.getDir() == 2) { // 上行取小值 + kilometer = kmCodeList.stream().min(Long::compareTo).get(); + } else { // 无方向获取第一个 + kilometer = kmCodeList.get(0); + } + offset.setKilometerCode(kilometer); } buildList.add(offset); } diff --git a/src/test/java/club/joylink/xiannccda/WebSocketTest.java b/src/test/java/club/joylink/xiannccda/WebSocketTest.java index 226f559..21f374a 100644 --- a/src/test/java/club/joylink/xiannccda/WebSocketTest.java +++ b/src/test/java/club/joylink/xiannccda/WebSocketTest.java @@ -2,7 +2,6 @@ package club.joylink.xiannccda; import static org.junit.jupiter.api.Assertions.assertTrue; -import club.joylink.xiannccda.dto.protos.AlertInfoProto.AlertInfoMessage; import com.google.protobuf.InvalidProtocolBufferException; import java.lang.reflect.Type; import java.util.concurrent.CountDownLatch; @@ -29,42 +28,51 @@ public class WebSocketTest { CountDownLatch latch = new CountDownLatch(1); WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); - headers.set("Authorization", + headers.set( + "Authorization", "Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiMTQiLCJleHAiOjE2ODgzNDY3ODgsImlhdCI6MTY4ODA4NzU4OH0.CCAHMqzzVXDYlvIBYhOBT92_MaYTSUDMmPwkzFDMRXR_LSidyVkm3o5SmqaiORjTgx_B0kxgwgpDGdXs3A_tmDrHolIUCvSaiez-hK3v30Z3Z4DhxtE8073tAqGdM-uU1eXTGC7zm20tM5riDe_Qy5PF5lf9qe9ty0Y06s62FNRNcyCBw-DuhkQcSkWfcMiUHKYVge2weO5Mzm1nD-1yViI359-smYU5eYJInVBdOaBvOECQ4OF8GJQ9rfgktPJAUsrXT0bf8cHp-fUUSvuNv5QUVmg2j-tZDIjhlN3OkXpHDiXuMPnx_lHVS95CO0metFozU2OT7uYukV5Z6RN4JA"); StompHeaders stompHeaders = new StompHeaders(); - stompHeaders.set("Authorization", + stompHeaders.set( + "Authorization", "Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzZWxmIiwic3ViIjoiMTQiLCJleHAiOjE2ODgzNDY3ODgsImlhdCI6MTY4ODA4NzU4OH0.CCAHMqzzVXDYlvIBYhOBT92_MaYTSUDMmPwkzFDMRXR_LSidyVkm3o5SmqaiORjTgx_B0kxgwgpDGdXs3A_tmDrHolIUCvSaiez-hK3v30Z3Z4DhxtE8073tAqGdM-uU1eXTGC7zm20tM5riDe_Qy5PF5lf9qe9ty0Y06s62FNRNcyCBw-DuhkQcSkWfcMiUHKYVge2weO5Mzm1nD-1yViI359-smYU5eYJInVBdOaBvOECQ4OF8GJQ9rfgktPJAUsrXT0bf8cHp-fUUSvuNv5QUVmg2j-tZDIjhlN3OkXpHDiXuMPnx_lHVS95CO0metFozU2OT7uYukV5Z6RN4JA"); - StompSessionHandlerAdapter sessionHandler = new StompSessionHandlerAdapter() { - @Override - public void handleException(StompSession session, StompCommand command, StompHeaders headers, - byte[] payload, Throwable exception) { - exception.printStackTrace(); - } - - @Override - public void afterConnected(StompSession session, StompHeaders connectedHeaders) { - super.afterConnected(session, connectedHeaders); - session.subscribe("/queue/xian/ncc/alert", new StompFrameHandler() { + StompSessionHandlerAdapter sessionHandler = + new StompSessionHandlerAdapter() { @Override - public Type getPayloadType(StompHeaders headers) { - return byte[].class; + public void handleException( + StompSession session, + StompCommand command, + StompHeaders headers, + byte[] payload, + Throwable exception) { + exception.printStackTrace(); } @Override - public void handleFrame(StompHeaders headers, Object payload) { - AlertInfoMessage message = null; - try { - message = AlertInfoMessage.parseFrom((byte[]) payload); - System.out.println("Received message: " + message); - } catch (InvalidProtocolBufferException e) { - e.printStackTrace(); - } + public void afterConnected(StompSession session, StompHeaders connectedHeaders) { + super.afterConnected(session, connectedHeaders); + session.subscribe( + "/queue/xian/ncc/alert", + new StompFrameHandler() { + @Override + public Type getPayloadType(StompHeaders headers) { + return byte[].class; + } + + @Override + public void handleFrame(StompHeaders headers, Object payload) { + // AlertInfoMessage message = null; + // try { + // message = AlertInfoMessage.parseFrom((byte[]) payload); + // System.out.println("Received message: " + message); + // } catch (InvalidProtocolBufferException e) { + // e.printStackTrace(); + // } + } + }); } - }); - } - }; + }; stompClient.connectAsync(webSocketUrl, headers, stompHeaders, sessionHandler);