diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java b/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java index b8d96db..d1f3e20 100644 --- a/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java +++ b/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java @@ -1,5 +1,6 @@ package club.joylink.xiannccda.alert; +import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Platform; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Station; import club.joylink.xiannccda.entity.AlertTip; @@ -30,17 +31,18 @@ public class AlertDetailFactory { BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue( messageOrBuilder instanceof Station); Station rtu = (Station) messageOrBuilder; - AlertTipLocationType locationType = AlertTipLocationType.getByLineIdAndRtuId(lineId, - Short.parseShort(rtu.getCode())); + BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertTrue(rtu.getConcentrationStations(), + "参数错误:非集中站"); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(AlertTip.class) .eq(AlertTip::getAlertType, alertType) - .eq(AlertTip::getLocationType, locationType); + .eq(AlertTip::getLocationType, rtu.getCode()); AlertTip alertTip = alertTipRepository.getOne(queryWrapper); alertTipId = alertTip == null ? null : alertTip.getId(); - info = String.format("[%s号线]%s蓝显", lineId, locationType.getDesc()); + String interlockName = getInterlockName(rtu); + info = String.format("[%s号线][%s]蓝显", lineId, interlockName); } case TRAIN_DELAY -> { - return null; + throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("尚未实现"); } case PLATFORM_DOOR_WITHOUT_LOCKED_SIGNAL -> { BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue( @@ -50,7 +52,8 @@ public class AlertDetailFactory { .eq(AlertTip::getAlertType, alertType); AlertTip alertTip = alertTipRepository.getOne(queryWrapper); alertTipId = alertTip == null ? null : alertTip.getId(); - info = String.format("[%s号线]%s站台门无关闭且锁紧信号", lineId, platform.getCode()); + String platformDesc = getPlatformDesc(lineId, platform); + info = String.format("[%s号线]%s站台门无关闭且锁紧信号", lineId, platformDesc); } case PLATFORM_DOOR_CANNOT_OPEN -> { BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue( @@ -60,7 +63,8 @@ public class AlertDetailFactory { .eq(AlertTip::getAlertType, alertType); AlertTip alertTip = alertTipRepository.getOne(queryWrapper); alertTipId = alertTip == null ? null : alertTip.getId(); - info = String.format("[%s号线]%s站台门无法打开", lineId, platform.getCode()); + String platformDesc = getPlatformDesc(lineId, platform); + info = String.format("[%s号线]%s站台门无法打开", lineId, platformDesc); } case PLATFORM_DOOR_CANNOT_CLOSE -> { BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue( @@ -70,10 +74,28 @@ public class AlertDetailFactory { .eq(AlertTip::getAlertType, alertType); AlertTip alertTip = alertTipRepository.getOne(queryWrapper); alertTipId = alertTip == null ? null : alertTip.getId(); - info = String.format("[%s号线]%s站台门无法关闭", lineId, platform.getCode()); + String platformDesc = getPlatformDesc(lineId, platform); + info = String.format("[%s号线]%s站台门无法关闭", lineId, platformDesc); } default -> throw new IllegalStateException("Unexpected value: " + alertType); } return new AlertDetailImpl(alertType, now, alertTipId, info, mock); } + + private String getPlatformDesc(short lineId, Platform platform) { + Station station = LineGraphicDataRepository.getDeviceByCode(lineId, + platform.getRefStation(), Station.class); + return String.format("[%s][%s行站台]", station.getName(), platform.getUp() ? "上" : "下"); + } + + /** + * 获取集中站的联锁区描述。 + * 例如,鱼化寨站的联锁区叫“鱼化寨联锁区” + */ + private String getInterlockName(Station rtu) { + int index = rtu.getName().length() - 1; + char c = rtu.getName().charAt(index); + String interlockName = c == '站' ? rtu.getName().substring(0, index) : rtu.getName(); + return interlockName + "联锁区"; + } } 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 10ea9df..bbf23fc 100644 --- a/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java +++ b/src/main/java/club/joylink/xiannccda/ats/cache/LineGraphicDataRepository.java @@ -4,14 +4,17 @@ import club.joylink.xiannccda.dto.protos.DeviceInfoProto; import club.joylink.xiannccda.dto.protos.DeviceInfoProto.DeviceKilometer; import club.joylink.xiannccda.dto.protos.DeviceInfoProto.Turnout; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.CommonInfo; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.KilometerSystem; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.RelatedRef.DeviceType; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.RtssGraphicStorage; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section.SectionType; import club.joylink.xiannccda.entity.PublishedGi; +import club.joylink.xiannccda.exception.BusinessExceptionAssertEnum; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.google.common.collect.HashBasedTable; +import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.GeneratedMessageV3.Builder; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.MessageOrBuilder; @@ -20,7 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -41,8 +43,10 @@ public class LineGraphicDataRepository { /** 线路-设备code-设备 */ private static final HashBasedTable line_code_table = HashBasedTable.create(); - /** 线路-设备。由于现在许多设备code都是"",使用line_code_table会覆盖,临时加此map,后续删除 */ - private static final HashMap> line_map = new HashMap<>(); + + /** 线路-设备id-设备 */ + private static final HashBasedTable line_id_table = + HashBasedTable.create(); /** * 缓存线路数据信息 @@ -112,38 +116,52 @@ public class LineGraphicDataRepository { lineGraphMap.remove(id); } - public static T getDevice(short lineId, String code, Class cls) { - return (T) line_code_table.get(lineId, code); + public static T getDeviceByCode(short lineId, String code, Class cls) { + MessageOrBuilder mob = line_code_table.get(lineId, code); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotNull( + mob, String.format("线路[%s]code[%s]", lineId, code)); + return (T) mob; + } + + public static T getDeviceById(short lineId, String id, Class cls) { + MessageOrBuilder mob = line_id_table.get(lineId, id); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotNull( + mob, String.format("线路[%s]id[%s]", lineId, id)); + return (T) mob; } public static Stream getDevices(short lineId, Class cls) { - return line_map.get(lineId).stream() + return line_code_table.row(lineId).values().stream() .filter(builder -> cls.isAssignableFrom(builder.getClass())) .map(builder -> (T) builder); - // return line_code_table.row(lineId).values().stream() - // .filter(builder -> cls.isAssignableFrom(builder.getClass())) - // .map(builder -> (T) builder); } private static void fillLineCodeTable(Integer lineId, RtssGraphicStorage storage) { Short shortLineId = lineId.shortValue(); - List list = - storage.getAllFields().entrySet().stream() - .filter(entry -> entry.getKey().isRepeated()) - .flatMap(entry -> ((List) entry.getValue()).stream()) - .collect(Collectors.toList()); - line_map.put(shortLineId, list); - // storage.getAllFields().forEach((fd, value) -> { - // if (fd.isRepeated()) { - // List list = (List) value; - // for (MessageOrBuilder builder : list) { - // FieldDescriptor fieldDescriptor = - // builder.getDescriptorForType().findFieldByName("code"); - // String code = (String) builder.getField(fieldDescriptor); - // line_code_table.put(shortLineId, code, builder); - // } - // } - // }); + FieldDescriptor idField = CommonInfo.getDescriptor().findFieldByName("id"); + storage + .getAllFields() + .forEach( + (fd, value) -> { + if (fd.isRepeated()) { + List list = (List) value; + for (MessageOrBuilder builder : list) { + FieldDescriptor codeField = + builder.getDescriptorForType().findFieldByName("code"); + String code = (String) builder.getField(codeField); + line_code_table.put(shortLineId, code, builder); + + FieldDescriptor commonField = + builder.getDescriptorForType().findFieldByName("common"); + if (commonField == null) { + continue; + } + MessageOrBuilder common = (MessageOrBuilder) builder.getField(commonField); + String id = (String) common.getField(idField); + line_code_table.put(shortLineId, id, builder); + } + } + }); } /** @@ -489,7 +507,7 @@ public class LineGraphicDataRepository { } /** - * 生成默认的公里标对象 + * <<<<<<< HEAD 生成默认的公里标对象 * * @param list 公里标列表 * @return 对象信息 diff --git a/src/main/java/club/joylink/xiannccda/service/AlertMockService.java b/src/main/java/club/joylink/xiannccda/service/AlertMockService.java index 38fa659..fe070fc 100644 --- a/src/main/java/club/joylink/xiannccda/service/AlertMockService.java +++ b/src/main/java/club/joylink/xiannccda/service/AlertMockService.java @@ -34,11 +34,13 @@ public class AlertMockService { switch (alertType) { case BLUE_DISPLAY -> { Stream stream = LineGraphicDataRepository.getDevices(lineId, Station.class); - messageOrBuilder = stream.findFirst() + messageOrBuilder = stream + .filter(Station::getConcentrationStations) + .findFirst() .orElseThrow(BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL::exception); } case TRAIN_DELAY -> { - throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception(); + throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("尚未实现"); } case PLATFORM_DOOR_WITHOUT_LOCKED_SIGNAL, PLATFORM_DOOR_CANNOT_OPEN, PLATFORM_DOOR_CANNOT_CLOSE -> { Stream stream = LineGraphicDataRepository.getDevices(lineId, Platform.class);