From 77e2f2e10429b88a3592c397ce475b8893aaf622 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Tue, 18 Jul 2023 09:49:24 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E7=BA=BF=E7=BD=91?= =?UTF-8?q?=E5=88=97=E8=BD=A6=E4=BF=A1=E6=81=AF=E6=9B=B4=E6=96=B0=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collect/DeviceStatusDataOperate.java | 44 ++++++----- .../convertor/LineNetTrainComMethod.java | 73 +++++++++++++++++++ .../convertor/LineNetTrainInitConvertor.java | 27 +------ .../LineNetTrainRecordConvertor.java | 37 ++-------- .../LineNetTrainRemoveConvertor.java | 20 +---- .../LineNetTrainUpdateConvertor.java | 27 ++----- .../service/LineDeviceStatusService.java | 17 ++--- .../xiannccda/ws/LineNetMessageServer.java | 10 +-- xian-ncc-da-message | 2 +- 9 files changed, 128 insertions(+), 129 deletions(-) create mode 100644 src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainComMethod.java diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/DeviceStatusDataOperate.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/DeviceStatusDataOperate.java index 11a43ca..b7a559d 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/DeviceStatusDataOperate.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/DeviceStatusDataOperate.java @@ -11,33 +11,27 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.springframework.util.CollectionUtils; -/** - * 线路设备操作方法 - */ +/** 线路设备操作方法 */ public abstract class DeviceStatusDataOperate { - /** - * 默认的主键名称 - */ + /** 默认的主键名称 */ private static final String DEFAULT_ID_NAME = "id"; - /** - * 设备主键名称映射 - */ - private static final Map DEVICE_ID_NAME_MAP = + /** 设备主键名称映射 */ + public static final Map> DEVICE_ID_NAME_MAP = Map.of( "LineNetTrainOffsetDiagram", - "groupId", + List.of("groupId"), "TrainInfo", - "groupId", + List.of("groupId"), "WsLineNetTrainOffsetMessage", - "groupId"); + List.of("lineId", "groupId")); /** * 批量放入设备状态 * * @param builders 要添加的设备状态列表 - * @param data 存储内存 + * @param data 存储内存 */ public static void addDevices(List builders, DeviceStatusData data) { if (data == null || builders == null) { @@ -95,8 +89,12 @@ public abstract class DeviceStatusDataOperate { * @param builder 设备状态信息 * @return 状态类型 */ - private static String findType(Builder builder) { - return builder.getDefaultInstanceForType().getClass().getSimpleName(); + public static String findType(Builder builder) { + return findType(builder.getDefaultInstanceForType().getClass()); + } + + public static String findType(Class cls) { + return cls.getSimpleName(); } /** @@ -105,11 +103,17 @@ public abstract class DeviceStatusDataOperate { * @param protoBuilder 数据信息 * @return 主键 */ - private static String getIdVal(Builder protoBuilder) { - String idName = DEVICE_ID_NAME_MAP.getOrDefault(findType(protoBuilder), DEFAULT_ID_NAME); + public static String getIdVal(Builder protoBuilder) { + List idKeyNames = + DEVICE_ID_NAME_MAP.getOrDefault(findType(protoBuilder), List.of(DEFAULT_ID_NAME)); try { - return String.valueOf( - protoBuilder.getField(protoBuilder.getDescriptorForType().findFieldByName(idName))); + return idKeyNames.stream() + .map( + name -> + String.valueOf( + protoBuilder.getField( + protoBuilder.getDescriptorForType().findFieldByName(name)))) + .collect(Collectors.joining("_")); } catch (Exception e) { System.err.println(protoBuilder.getDescriptorForType().getName()); } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainComMethod.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainComMethod.java new file mode 100644 index 0000000..6d27527 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainComMethod.java @@ -0,0 +1,73 @@ +package club.joylink.xiannccda.ats.message.collect.convertor; + +import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; +import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; +import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository; +import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; +import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import club.joylink.xiannccda.dto.protos.WsMessageProto; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.google.protobuf.GeneratedMessageV3.Builder; +import java.util.List; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; + +/** 线网的列车数据,一些公共方法 */ +@Slf4j +public abstract class LineNetTrainComMethod { + + public static final String DATA_KEY = "lineNet"; + + static final String type = + DeviceStatusDataOperate.findType(WsMessageProto.WsLineNetTrainOffsetMessage.class); + + // 设备状态数据集合 + public static synchronized DeviceStatusData getDeviceStatusData() { + return DeviceStatusDataRepository.getDeviceStatusData(DATA_KEY); + } + + /** + * 获取已存在的设备状态信息 + * + * @param obj 提供Key信息 + * @return 旧的设备状态 + */ + public static void getDeviceStatusById(WsMessageProto.WsLineNetTrainOffsetMessage.Builder obj) { + Map builderMap = + getDeviceStatusData().getAllDeviceMap().getOrDefault(type, Map.of()); + String key = DeviceStatusDataOperate.getIdVal(obj); + if (builderMap.containsKey(key)) { + obj.mergeFrom(builderMap.get(key).build()); + } + } + + /** + * 设置公里标信息 + * + * @param obj 要设置的对象 + * @param type 所在设备类型 + * @param deviceName 设备名称 + */ + public static void setUpKilometerCode( + WsMessageProto.WsLineNetTrainOffsetMessage.Builder obj, DeviceType type, String deviceName) { + // 非区段、道岔直接返回 + if (!DeviceType.DEVICE_TYPE_TRACK.equals(type) && !DeviceType.DEVICE_TYPE_SWITCH.equals(type)) { + return; + } + // 获取到当前区段公里标 + List kmList = LineGraphicDataRepository.getKilometerCodeList(obj.getLineId(), deviceName); + if (CollectionUtils.isNotEmpty(kmList)) { + long kilometer; + if (obj.getDir() == 1) { // 下行取大值 + kilometer = kmList.stream().min(Long::compareTo).get(); + } else if (obj.getDir() == 2) { // 上行取小值 + kilometer = kmList.stream().max(Long::compareTo).get(); + } else { // 无方向获取第一个 + kilometer = kmList.get(0); + } + obj.setKilometerCode(kilometer); + } else { + log.warn(String.format("设备%s没有公里标信息", deviceName)); + } + } +} diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainInitConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainInitConvertor.java index 765964e..d40a405 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainInitConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainInitConvertor.java @@ -1,17 +1,13 @@ package club.joylink.xiannccda.ats.message.collect.convertor; -import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; import club.joylink.xiannccda.ats.message.MessageData; import club.joylink.xiannccda.ats.message.MessageId; import club.joylink.xiannccda.ats.message.collect.DeviceStatusConvertor; import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; -import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; -import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.ats.message.line3.rep.TrainIndicationInitResponse; import club.joylink.xiannccda.ats.message.line3.rep.TrainIndicationInitResponse.TrainCell; import club.joylink.xiannccda.dto.protos.WsMessageProto; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.google.common.collect.Lists; import com.google.protobuf.GeneratedMessageV3.Builder; import java.util.List; @@ -21,13 +17,6 @@ import lombok.extern.slf4j.Slf4j; /** 接收列车初始化消息,就收转换线网的列车数据 */ @Slf4j public class LineNetTrainInitConvertor extends DeviceStatusConvertor { - - final DeviceStatusData dataSource; - - public LineNetTrainInitConvertor(DeviceStatusData dataSource) { - this.dataSource = dataSource; - } - @Override public MessageId getMessageId() { return MessageId.TRAIN_INDICATION_INIT; @@ -46,23 +35,15 @@ public class LineNetTrainInitConvertor extends DeviceStatusConvertor { offset.setLineId(response.getLineId()); // 线路id offset.setTrainIndex(trainCell.getTrainIndex()); // 列车标示号,全线唯一 offset.setGroupId(trainCell.getGroupId()); // 车组号 - offset.setShow(false); // 是否显示 offset.setDir(0); // 初始设置无运行方向 - // 获取到当前区段公里标 - if (DeviceType.DEVICE_TYPE_TRACK.equals(trainCell.getDevType())) { - List kmCodeList = - LineGraphicDataRepository.getKilometerCodeList( - response.getLineId(), trainCell.getDevName()); - if (CollectionUtils.isNotEmpty(kmCodeList)) { - offset.setShow(true); // 是否显示 - offset.setKilometerCode(kmCodeList.get(0)); - } - } + LineNetTrainComMethod.setUpKilometerCode( + offset, trainCell.getDevType(), trainCell.getDevName()); + offset.setShow(offset.getKilometerCode() != 0); buildList.add(offset); } } } // 增加设备信息 - DeviceStatusDataOperate.addDevices(buildList, dataSource); + DeviceStatusDataOperate.addDevices(buildList, LineNetTrainComMethod.getDeviceStatusData()); } } 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 4873eb4..66b683c 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 @@ -1,15 +1,13 @@ package club.joylink.xiannccda.ats.message.collect.convertor; -import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; import club.joylink.xiannccda.ats.message.MessageData; import club.joylink.xiannccda.ats.message.MessageId; import club.joylink.xiannccda.ats.message.collect.DeviceStatusConvertor; import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; -import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; +import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.ats.message.line3.rep.TrainRecordResponse; import club.joylink.xiannccda.dto.protos.WsMessageProto; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.google.common.collect.Lists; import com.google.protobuf.GeneratedMessageV3.Builder; import java.util.List; @@ -19,12 +17,6 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class LineNetTrainRecordConvertor extends DeviceStatusConvertor { - final DeviceStatusData dataSource; - - public LineNetTrainRecordConvertor(DeviceStatusData dataSource) { - this.dataSource = dataSource; - } - @Override public MessageId getMessageId() { return MessageId.TRAIN_RECORD; @@ -34,6 +26,7 @@ public class LineNetTrainRecordConvertor extends DeviceStatusConvertor { public void run(List messageDataList) { try { List buildList = Lists.newArrayList(); + // 已存在的设备信息 for (MessageData data : messageDataList) { if (Objects.equals(getMessageId(), data.getMsgId())) { // 消息类型一致 TrainRecordResponse response = (TrainRecordResponse) data; @@ -41,31 +34,17 @@ public class LineNetTrainRecordConvertor extends DeviceStatusConvertor { WsMessageProto.WsLineNetTrainOffsetMessage.newBuilder(); offset.setLineId(response.getLineId()); // 线路id offset.setGroupId(response.getGroupId()); // 车组号 + LineNetTrainComMethod.getDeviceStatusById(offset); // 合并老旧数据 offset.setDir(response.getDirection().getValue()); // 初始设置无运行方向 - offset.setShow(false); - // 获取到当前区段公里标 - List kmCodeList = - LineGraphicDataRepository.getKilometerCodeList( - response.getLineId(), response.getTrackName()); - if (CollectionUtils.isNotEmpty(kmCodeList)) { - offset.setShow(true); // 如果公里标存在则 - long kilometer; - if (offset.getDir() == 1) { // 下行取大值 - kilometer = kmCodeList.stream().min(Long::compareTo).get(); - } else if (offset.getDir() == 2) { // 上行取小值 - kilometer = kmCodeList.stream().max(Long::compareTo).get(); - } else { // 无方向获取第一个 - kilometer = kmCodeList.get(0); - } - offset.setKilometerCode(kilometer); - } else { - log.warn(String.format("设备%s没有公里标信息", response.getTrackName())); - } + // 设置公里标信息 + LineNetTrainComMethod.setUpKilometerCode( + offset, DeviceType.DEVICE_TYPE_TRACK, response.getTrackName()); + offset.setShow(offset.getKilometerCode() != 0); buildList.add(offset); } } // 增加设备信息 - DeviceStatusDataOperate.addDevices(buildList, dataSource); + DeviceStatusDataOperate.addDevices(buildList, LineNetTrainComMethod.getDeviceStatusData()); } catch (Exception e) { log.error("信息生成出错", e); } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRemoveConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRemoveConvertor.java index c680696..aa1b1c0 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRemoveConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainRemoveConvertor.java @@ -21,12 +21,6 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class LineNetTrainRemoveConvertor extends DeviceStatusConvertor { - final DeviceStatusData dataSource; - - public LineNetTrainRemoveConvertor(DeviceStatusData dataSource) { - this.dataSource = dataSource; - } - @Override public MessageId getMessageId() { return MessageId.TRAIN_INDICATION_REMOVE; @@ -44,20 +38,14 @@ public class LineNetTrainRemoveConvertor extends DeviceStatusConvertor { offset.setLineId(response.getLineId()); offset.setGroupId(response.getGroupId()); offset.setTrainIndex(response.getTrainIndex()); + LineNetTrainComMethod.getDeviceStatusById(offset); // 合并老旧数据 + LineNetTrainComMethod.setUpKilometerCode( + offset, response.getDevType(), response.getDevName()); offset.setShow(false); - // 获取到当前区段公里标 - if (DeviceType.DEVICE_TYPE_TRACK.equals(response.getDevType())) { - List kmCodeList = - LineGraphicDataRepository.getKilometerCodeList( - response.getLineId(), response.getDevName()); - if (CollectionUtils.isNotEmpty(kmCodeList)) { - offset.setKilometerCode(kmCodeList.get(0)); - } - } buildList.add(offset); } } - DeviceStatusDataOperate.addDevices(buildList, dataSource); + DeviceStatusDataOperate.addDevices(buildList, LineNetTrainComMethod.getDeviceStatusData()); } catch (Exception e) { log.error("移除列车信息", e); } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainUpdateConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainUpdateConvertor.java index 19c6a7d..b8f03ef 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainUpdateConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/LineNetTrainUpdateConvertor.java @@ -1,17 +1,12 @@ package club.joylink.xiannccda.ats.message.collect.convertor; -import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; import club.joylink.xiannccda.ats.message.MessageData; import club.joylink.xiannccda.ats.message.MessageId; import club.joylink.xiannccda.ats.message.collect.DeviceStatusConvertor; import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; -import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; -import club.joylink.xiannccda.ats.message.line3.device.DeviceType; -import club.joylink.xiannccda.ats.message.line3.rep.TrainIndicationRemoveResponse; import club.joylink.xiannccda.ats.message.line3.rep.TrainIndicationUpdateResponse; import club.joylink.xiannccda.dto.protos.WsMessageProto; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.google.common.collect.Lists; import com.google.protobuf.GeneratedMessageV3.Builder; import java.util.List; @@ -22,12 +17,6 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class LineNetTrainUpdateConvertor extends DeviceStatusConvertor { - final DeviceStatusData dataSource; - - public LineNetTrainUpdateConvertor(DeviceStatusData dataSource) { - this.dataSource = dataSource; - } - @Override public MessageId getMessageId() { return MessageId.TRAIN_INDICATION_UPDATE; @@ -45,20 +34,14 @@ public class LineNetTrainUpdateConvertor extends DeviceStatusConvertor { offset.setLineId(response.getLineId()); // 线路id offset.setTrainIndex(response.getTrainIndex()); // 列车标示号,全线唯一 offset.setGroupId(response.getGroupId()); // 车组号 - // 获取到当前区段公里标 - if (DeviceType.DEVICE_TYPE_TRACK.equals(response.getDevType())) { - List kmCodeList = - LineGraphicDataRepository.getKilometerCodeList( - response.getLineId(), response.getDevName()); - if (CollectionUtils.isNotEmpty(kmCodeList)) { - offset.setShow(true); // 是否显示 - offset.setKilometerCode(kmCodeList.get(0)); - } - } + LineNetTrainComMethod.getDeviceStatusById(offset); // 合并老旧数据 + LineNetTrainComMethod.setUpKilometerCode( + offset, response.getDevType(), response.getDevName()); + offset.setShow(offset.getKilometerCode() != 0); buildList.add(offset); } } - DeviceStatusDataOperate.addDevices(buildList, dataSource); + DeviceStatusDataOperate.addDevices(buildList, LineNetTrainComMethod.getDeviceStatusData()); } catch (Exception e) { log.error("移除列车信息", e); } diff --git a/src/main/java/club/joylink/xiannccda/service/LineDeviceStatusService.java b/src/main/java/club/joylink/xiannccda/service/LineDeviceStatusService.java index e07fceb..c833dc1 100644 --- a/src/main/java/club/joylink/xiannccda/service/LineDeviceStatusService.java +++ b/src/main/java/club/joylink/xiannccda/service/LineDeviceStatusService.java @@ -51,24 +51,17 @@ public class LineDeviceStatusService { @PostConstruct public void deviceStatusRefresh() { this.createDataConvertor(); - // 设备状态数据集合 - DeviceStatusData deviceStatusData = DeviceStatusDataRepository.getDeviceStatusData(DATA_KEY); // 添加初始化转换方法 - LineNetTrainInitConvertor initConvertor = new LineNetTrainInitConvertor(deviceStatusData); - DeviceStatusConvertorManager.addStatusConvertor(initConvertor); + DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainInitConvertor()); // 列车报点信息转换方法 - LineNetTrainRecordConvertor recConvertor = new LineNetTrainRecordConvertor(deviceStatusData); - DeviceStatusConvertorManager.addStatusConvertor(recConvertor); + DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainRecordConvertor()); // 列车更新转换方法 - LineNetTrainUpdateConvertor updateConvertor = new LineNetTrainUpdateConvertor(deviceStatusData); - DeviceStatusConvertorManager.addStatusConvertor(updateConvertor); + DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainUpdateConvertor()); // 列车删除转换方法 - LineNetTrainRemoveConvertor removeConvertor = new LineNetTrainRemoveConvertor(deviceStatusData); - DeviceStatusConvertorManager.addStatusConvertor(removeConvertor); + DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainRemoveConvertor()); // websocket发送服务 - IMessageServer iMessageServer = new LineNetMessageServer(deviceStatusData); - wsMessageServerManager.registerMessageServer(iMessageServer); + wsMessageServerManager.registerMessageServer(new LineNetMessageServer()); wsMessageServerManager.registerMessageServer(new LineTrainMessageServer()); wsMessageServerManager.registerMessageServer(new LineDeviceMessageServer()); diff --git a/src/main/java/club/joylink/xiannccda/ws/LineNetMessageServer.java b/src/main/java/club/joylink/xiannccda/ws/LineNetMessageServer.java index 8d5386b..0eb4ded 100644 --- a/src/main/java/club/joylink/xiannccda/ws/LineNetMessageServer.java +++ b/src/main/java/club/joylink/xiannccda/ws/LineNetMessageServer.java @@ -1,7 +1,7 @@ package club.joylink.xiannccda.ws; - import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; +import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainComMethod; import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; import club.joylink.xiannccda.dto.protos.WsMessageProto.WsLineNetMessage; import com.google.protobuf.Descriptors.FieldDescriptor.Type; @@ -15,16 +15,14 @@ import java.util.function.Function; import lombok.extern.slf4j.Slf4j; import org.springframework.util.CollectionUtils; -/** - * 线网图websocket消息服务 - */ +/** 线网图websocket消息服务 */ @Slf4j public class LineNetMessageServer implements IMessageServer { final DeviceStatusData dataSource; - public LineNetMessageServer(DeviceStatusData dataSource) { - this.dataSource = dataSource; + public LineNetMessageServer() { + this.dataSource = LineNetTrainComMethod.getDeviceStatusData(); } @Override diff --git a/xian-ncc-da-message b/xian-ncc-da-message index f86b357..e8ac230 160000 --- a/xian-ncc-da-message +++ b/xian-ncc-da-message @@ -1 +1 @@ -Subproject commit f86b35768560ddd4e2a3bef70c9b8dfe42510d8f +Subproject commit e8ac23070828a1eca3bd68b6f78e4e419123d139