【修改线网列车信息更新】

This commit is contained in:
weizhihong 2023-07-18 09:49:24 +08:00
parent 1cd2b8afb8
commit 77e2f2e104
9 changed files with 128 additions and 129 deletions

View File

@ -11,27 +11,21 @@ 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<String, String> DEVICE_ID_NAME_MAP =
/** 设备主键名称映射 */
public static final Map<String, List<String>> DEVICE_ID_NAME_MAP =
Map.of(
"LineNetTrainOffsetDiagram",
"groupId",
List.of("groupId"),
"TrainInfo",
"groupId",
List.of("groupId"),
"WsLineNetTrainOffsetMessage",
"groupId");
List.of("lineId", "groupId"));
/**
* 批量放入设备状态
@ -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<String> 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());
}

View File

@ -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<String, Builder> 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<Long> 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));
}
}
}

View File

@ -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<Long> 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());
}
}

View File

@ -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<MessageData> messageDataList) {
try {
List<Builder> 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<Long> 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);
}

View File

@ -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<Long> 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);
}

View File

@ -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<Long> 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);
}

View File

@ -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());

View File

@ -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

@ -1 +1 @@
Subproject commit f86b35768560ddd4e2a3bef70c9b8dfe42510d8f
Subproject commit e8ac23070828a1eca3bd68b6f78e4e419123d139