线路数据匹配逻辑处理

This commit is contained in:
weizhihong 2023-07-21 09:21:11 +08:00
parent 604eb28066
commit 4fe577e87c
9 changed files with 1152 additions and 191 deletions

View File

@ -92,6 +92,20 @@ public class LineGraphicDataRepository {
lineGraphMap.remove(id);
}
/**
* 获取线路的区段集合
*
* @param lineId 线路ID
* @return 区段集合
*/
public static Map<String, Builder> getLineSectionBuild(int lineId) {
Map<String, Map<String, Builder>> lineDataMap = lineGraphMap.get(lineId);
if (CollectionUtils.isNotEmpty(lineDataMap)) {
return lineDataMap.get(DeviceType.Section.name());
}
return Map.of();
}
/**
* 构建程序中的区段信息
*
@ -117,6 +131,7 @@ public class LineGraphicDataRepository {
? (DeviceInfoProto.Section.Builder) cacheSectionMap.get(sid)
: DeviceInfoProto.Section.newBuilder().setId(sid);
sectionBuilder.setCode(section.getCode());
sectionBuilder.setDestinationCode(section.getDestinationCode());
if (section.getChildrenCount() > 0) {
sectionBuilder.addAllChildrenId(section.getChildrenList());
// 初始化逻辑区段信息,建立逻辑区段与物理区段之间的关系

View File

@ -6,11 +6,15 @@ import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository;
import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData;
import club.joylink.xiannccda.ats.message.line3.changer.DeviceNameChangerManage;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import club.joylink.xiannccda.dto.protos.DeviceInfoProto;
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 java.util.Objects;
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/** 线网的列车数据,一些公共方法 */
@ -51,27 +55,121 @@ public abstract class LineNetTrainComMethod {
*/
public static void setUpKilometerCode(
WsMessageProto.WsLineNetTrainOffsetMessage.Builder obj, DeviceType type, String deviceName) {
// 获取到当前区段公里标
List<Long> kmList = getDeviceKilometerCode(obj.getLineId(), type, deviceName);
if (CollectionUtils.isNotEmpty(kmList)) {
obj.setKilometerCode(selectDeviceKilometerCode(obj.getDir(), kmList));
} else {
log.warn(String.format("设备%s没有公里标信息", deviceName));
}
}
/**
* 根据目的码确定方向并确定公里标
*
* @param obj 要设置的对象
* @param destinationCode 目的码
* @param type 设备类型
* @param deviceName 设备名称
*/
public static void setTrainDirectionAndKilometerCode(
WsMessageProto.WsLineNetTrainOffsetMessage.Builder obj,
String destinationCode,
DeviceType type,
String deviceName) {
List<Long> deviceKmList = getDeviceKilometerCode(obj.getLineId(), type, deviceName);
if (CollectionUtils.isEmpty(deviceKmList)) {
return;
}
List<Long> destinationKmList = getDirectionCodeKilometerCode(obj.getLineId(), destinationCode);
if (CollectionUtils.isEmpty(destinationKmList)) {
return;
}
// 确定方向
long deviceMax = deviceKmList.stream().max(Long::compareTo).get();
long destinationMax = destinationKmList.stream().max(Long::compareTo).get();
if (destinationMax > deviceMax) { // 目的地最大公里标大于设备最大公里标上行
obj.setDir(TrainRunDirection.UP.getNum());
} else {
obj.setDir(TrainRunDirection.DOWN.getNum());
}
// 赋值公里标
obj.setKilometerCode(selectDeviceKilometerCode(obj.getDir(), deviceKmList));
}
/**
* 获取设备的公里标
*
* @param lineId 线路ID
* @param type 设备类型
* @param deviceName 设备名称
* @return 公里标列表
*/
private static List<Long> getDeviceKilometerCode(int lineId, DeviceType type, String deviceName) {
// 非区段道岔直接返回
if (!DeviceType.DEVICE_TYPE_TRACK.equals(type) && !DeviceType.DEVICE_TYPE_SWITCH.equals(type)) {
return;
return List.of();
}
// 转换成程序中的名称
String convertName = DeviceNameChangerManage.findMatch(type, deviceName);
// 获取到当前区段公里标
List<Long> kmList =
LineGraphicDataRepository.getKilometerCodeList(obj.getLineId(), convertName);
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);
return LineGraphicDataRepository.getKilometerCodeList(lineId, convertName);
}
/**
* 获取目的地码的公里标信息
*
* @param lineId 线路ID
* @param code 目的地码
* @return 公里标列表
*/
private static List<Long> getDirectionCodeKilometerCode(int lineId, String code) {
Map<String, Builder> map = LineGraphicDataRepository.getLineSectionBuild(lineId);
Optional<Builder> destination =
map.values().stream()
.filter(
s -> {
DeviceInfoProto.Section.Builder b = (DeviceInfoProto.Section.Builder) s;
return Objects.equals(b.getDestinationCode(), code);
})
.findFirst();
if (destination.isPresent()) {
DeviceInfoProto.Section.Builder b = (DeviceInfoProto.Section.Builder) destination.get();
return b.getConvertKilometerList();
} else {
log.warn(String.format("设备%s没有公里标信息", convertName));
return List.of();
}
}
/**
* 选择对应的公里标信息
*
* @param dir 运行方向
* @param kmList 公里标列表
* @return 公里标
*/
private static long selectDeviceKilometerCode(int dir, List<Long> kmList) {
long kilometer;
if (dir == 1) { // 下行取大值
kilometer = kmList.stream().min(Long::compareTo).get();
} else if (dir == 2) { // 上行取小值
kilometer = kmList.stream().max(Long::compareTo).get();
} else { // 无方向获取第一个
kilometer = kmList.get(0);
}
return kilometer;
}
@Getter
public static enum TrainRunDirection {
UP(2),
DOWN(1),
NO(0);
private int num;
TrainRunDirection(int num) {
this.num = num;
}
}
}

View File

@ -35,9 +35,8 @@ public class LineNetTrainInitConvertor extends DeviceStatusConvertor {
offset.setLineId(response.getLineId()); // 线路id
offset.setTrainIndex(trainCell.getTrainIndex()); // 列车标示号全线唯一
offset.setGroupId(trainCell.getGroupId()); // 车组号
offset.setDir(0); // 初始设置无运行方向
LineNetTrainComMethod.setUpKilometerCode(
offset, trainCell.getDevType(), trainCell.getDevName());
LineNetTrainComMethod.setTrainDirectionAndKilometerCode(
offset, trainCell.getDestinationId(), trainCell.getDevType(), trainCell.getDevName());
offset.setShow(offset.getKilometerCode() != 0);
buildList.add(offset);
}

View File

@ -1,52 +0,0 @@
package club.joylink.xiannccda.ats.message.collect.convertor;
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.line3.device.DeviceType;
import club.joylink.xiannccda.ats.message.line3.rep.TrainRecordResponse;
import club.joylink.xiannccda.dto.protos.WsMessageProto;
import com.google.common.collect.Lists;
import com.google.protobuf.GeneratedMessageV3.Builder;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LineNetTrainRecordConvertor extends DeviceStatusConvertor {
@Override
public MessageId getMessageId() {
return MessageId.TRAIN_RECORD;
}
@Override
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;
WsMessageProto.WsLineNetTrainOffsetMessage.Builder offset =
WsMessageProto.WsLineNetTrainOffsetMessage.newBuilder();
offset.setLineId(response.getLineId()); // 线路id
offset.setGroupId(response.getGroupId()); // 车组号
LineNetTrainComMethod.getDeviceStatusById(offset); // 合并老旧数据
offset.setDir(response.getDirection().getValue()); // 初始设置无运行方向
// 设置公里标信息
LineNetTrainComMethod.setUpKilometerCode(
offset, DeviceType.DEVICE_TYPE_TRACK, response.getTrackName());
offset.setShow(offset.getKilometerCode() != 0);
buildList.add(offset);
}
}
// 增加设备信息
DeviceStatusDataOperate.addDevices(buildList, LineNetTrainComMethod.getDeviceStatusData());
} catch (Exception e) {
log.error("信息生成出错", e);
}
}
}

View File

@ -35,8 +35,8 @@ public class LineNetTrainUpdateConvertor extends DeviceStatusConvertor {
offset.setTrainIndex(response.getTrainIndex()); // 列车标示号全线唯一
offset.setGroupId(response.getGroupId()); // 车组号
LineNetTrainComMethod.getDeviceStatusById(offset); // 合并老旧数据
LineNetTrainComMethod.setUpKilometerCode(
offset, response.getDevType(), response.getDevName());
LineNetTrainComMethod.setTrainDirectionAndKilometerCode(
offset, response.getDestinationId(), response.getDevType(), response.getDevName());
offset.setShow(offset.getKilometerCode() != 0);
buildList.add(offset);
}

View File

@ -183,6 +183,26 @@ public final class DeviceInfoProto {
*/
com.google.protobuf.ByteString
getPhysicalSectionIdBytes();
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The destinationCode.
*/
java.lang.String getDestinationCode();
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The bytes for destinationCode.
*/
com.google.protobuf.ByteString
getDestinationCodeBytes();
}
/**
* <pre>
@ -208,6 +228,7 @@ public final class DeviceInfoProto {
kilometerSystem_ = java.util.Collections.emptyList();
convertKilometer_ = emptyLongList();
physicalSectionId_ = "";
destinationCode_ = "";
}
@java.lang.Override
@ -518,6 +539,53 @@ public final class DeviceInfoProto {
}
}
public static final int DESTINATIONCODE_FIELD_NUMBER = 7;
@SuppressWarnings("serial")
private volatile java.lang.Object destinationCode_ = "";
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The destinationCode.
*/
@java.lang.Override
public java.lang.String getDestinationCode() {
java.lang.Object ref = destinationCode_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
destinationCode_ = s;
return s;
}
}
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The bytes for destinationCode.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getDestinationCodeBytes() {
java.lang.Object ref = destinationCode_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
destinationCode_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
@ -555,6 +623,9 @@ public final class DeviceInfoProto {
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(physicalSectionId_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 6, physicalSectionId_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(destinationCode_)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 7, destinationCode_);
}
getUnknownFields().writeTo(output);
}
@ -599,6 +670,9 @@ public final class DeviceInfoProto {
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(physicalSectionId_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(6, physicalSectionId_);
}
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(destinationCode_)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(7, destinationCode_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
@ -626,6 +700,8 @@ public final class DeviceInfoProto {
.equals(other.getConvertKilometerList())) return false;
if (!getPhysicalSectionId()
.equals(other.getPhysicalSectionId())) return false;
if (!getDestinationCode()
.equals(other.getDestinationCode())) return false;
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@ -655,6 +731,8 @@ public final class DeviceInfoProto {
}
hash = (37 * hash) + PHYSICALSECTIONID_FIELD_NUMBER;
hash = (53 * hash) + getPhysicalSectionId().hashCode();
hash = (37 * hash) + DESTINATIONCODE_FIELD_NUMBER;
hash = (53 * hash) + getDestinationCode().hashCode();
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
@ -803,6 +881,7 @@ public final class DeviceInfoProto {
bitField0_ = (bitField0_ & ~0x00000008);
convertKilometer_ = emptyLongList();
physicalSectionId_ = "";
destinationCode_ = "";
return this;
}
@ -867,6 +946,9 @@ public final class DeviceInfoProto {
if (((from_bitField0_ & 0x00000020) != 0)) {
result.physicalSectionId_ = physicalSectionId_;
}
if (((from_bitField0_ & 0x00000040) != 0)) {
result.destinationCode_ = destinationCode_;
}
}
@java.lang.Override
@ -942,6 +1024,11 @@ public final class DeviceInfoProto {
bitField0_ |= 0x00000020;
onChanged();
}
if (!other.getDestinationCode().isEmpty()) {
destinationCode_ = other.destinationCode_;
bitField0_ |= 0x00000040;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
@ -1018,6 +1105,11 @@ public final class DeviceInfoProto {
bitField0_ |= 0x00000020;
break;
} // case 50
case 58: {
destinationCode_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000040;
break;
} // case 58
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
@ -1858,6 +1950,98 @@ public final class DeviceInfoProto {
onChanged();
return this;
}
private java.lang.Object destinationCode_ = "";
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The destinationCode.
*/
public java.lang.String getDestinationCode() {
java.lang.Object ref = destinationCode_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
destinationCode_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return The bytes for destinationCode.
*/
public com.google.protobuf.ByteString
getDestinationCodeBytes() {
java.lang.Object ref = destinationCode_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
destinationCode_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @param value The destinationCode to set.
* @return This builder for chaining.
*/
public Builder setDestinationCode(
java.lang.String value) {
if (value == null) { throw new NullPointerException(); }
destinationCode_ = value;
bitField0_ |= 0x00000040;
onChanged();
return this;
}
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @return This builder for chaining.
*/
public Builder clearDestinationCode() {
destinationCode_ = getDefaultInstance().getDestinationCode();
bitField0_ = (bitField0_ & ~0x00000040);
onChanged();
return this;
}
/**
* <pre>
* 目的地码
* </pre>
*
* <code>string destinationCode = 7;</code>
* @param value The bytes for destinationCode to set.
* @return This builder for chaining.
*/
public Builder setDestinationCodeBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
destinationCode_ = value;
bitField0_ |= 0x00000040;
onChanged();
return this;
}
@java.lang.Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
@ -3348,16 +3532,16 @@ public final class DeviceInfoProto {
static {
java.lang.String[] descriptorData = {
"\n\021device_info.proto\022\005state\032\033stationLayou" +
"tGraphics.proto\"\243\001\n\007Section\022\n\n\002id\030\001 \001(\t\022" +
"tGraphics.proto\"\274\001\n\007Section\022\n\n\002id\030\001 \001(\t\022" +
"\014\n\004code\030\002 \001(\t\022\022\n\nchildrenId\030\003 \003(\t\0225\n\017kil" +
"ometerSystem\030\004 \003(\0132\034.graphicData.Kilomet" +
"erSystem\022\030\n\020convertKilometer\030\005 \003(\003\022\031\n\021ph" +
"ysicalSectionId\030\006 \001(\t\"t\n\007Turnout\022\n\n\002id\030\001" +
" \001(\t\022\014\n\004code\030\002 \001(\t\0225\n\017kilometerSystem\030\003 " +
"\003(\0132\034.graphicData.KilometerSystem\022\030\n\020con" +
"vertKilometer\030\004 \003(\003B4\n!club.joylink.xian" +
"nccda.dto.protosB\017DeviceInfoProtob\006proto" +
"3"
"ysicalSectionId\030\006 \001(\t\022\027\n\017destinationCode" +
"\030\007 \001(\t\"t\n\007Turnout\022\n\n\002id\030\001 \001(\t\022\014\n\004code\030\002 " +
"\001(\t\0225\n\017kilometerSystem\030\003 \003(\0132\034.graphicDa" +
"ta.KilometerSystem\022\030\n\020convertKilometer\030\004" +
" \003(\003B4\n!club.joylink.xiannccda.dto.proto" +
"sB\017DeviceInfoProtob\006proto3"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
@ -3369,7 +3553,7 @@ public final class DeviceInfoProto {
internal_static_state_Section_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_state_Section_descriptor,
new java.lang.String[] { "Id", "Code", "ChildrenId", "KilometerSystem", "ConvertKilometer", "PhysicalSectionId", });
new java.lang.String[] { "Id", "Code", "ChildrenId", "KilometerSystem", "ConvertKilometer", "PhysicalSectionId", "DestinationCode", });
internal_static_state_Turnout_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_state_Turnout_fieldAccessorTable = new

View File

@ -1,13 +1,10 @@
package club.joylink.xiannccda.service;
import club.joylink.xiannccda.ats.message.MessageData;
import club.joylink.xiannccda.ats.message.MessageId;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusConvertorManager;
import club.joylink.xiannccda.ats.message.collect.convertor.DeviceChangeStatusConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.DeviceInitConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainInitConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainRecordConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainRemoveConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainUpdateConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.TrainInitConvertor;
@ -20,10 +17,7 @@ import club.joylink.xiannccda.ws.LineNetMessageServer;
import club.joylink.xiannccda.ws.LineTrainMessageServer;
import club.joylink.xiannccda.ws.WsMessageServerManager;
import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -47,8 +41,6 @@ public class LineDeviceStatusService {
this.createDataConvertor();
// 添加初始化转换方法
DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainInitConvertor());
// 列车报点信息转换方法
DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainRecordConvertor());
// 列车更新转换方法
DeviceStatusConvertorManager.addStatusConvertor(new LineNetTrainUpdateConvertor());
// 列车删除转换方法
@ -58,9 +50,6 @@ public class LineDeviceStatusService {
wsMessageServerManager.registerMessageServer(new LineNetMessageServer());
wsMessageServerManager.registerMessageServer(new LineTrainMessageServer());
wsMessageServerManager.registerMessageServer(new LineDeviceMessageServer());
// 加载数据
refreshTestData();
}
private void createDataConvertor() {
@ -72,34 +61,4 @@ public class LineDeviceStatusService {
DeviceStatusConvertorManager.addStatusConvertor(new TrainRecordConvertor());
DeviceStatusConvertorManager.addStatusConvertor(new TrainUpdateConvertor());
}
public void refreshTestData() {
List<MessageData> allMockData = nccMockDataService.loadAllTrainInitData();
DeviceStatusConvertorManager.doConvertor(allMockData);
int frequency = 1500, stopTime = 30000;
AtomicLong id = new AtomicLong(0);
AtomicLong resetTimes = new AtomicLong(stopTime);
Executors.newSingleThreadScheduledExecutor()
.scheduleWithFixedDelay(
() -> {
boolean isEmpty = true;
if (resetTimes.get() == stopTime) { // 代表还没有停顿不相等说明已经进入空循环
List<MessageData> updateMockData =
nccMockDataService.loadUpdateData(id, MessageId.TRAIN_RECORD.name(), 2);
DeviceStatusConvertorManager.doConvertor(updateMockData);
isEmpty = updateMockData.size() == 0;
}
if (isEmpty) { // 假数据已经用完了开始重复使用中间停顿30000ms
if (resetTimes.get() <= 0) {
id.set(0);
resetTimes.set(stopTime);
} else {
resetTimes.set(resetTimes.get() - frequency);
}
}
},
frequency,
frequency,
TimeUnit.MILLISECONDS);
}
}

@ -1 +1 @@
Subproject commit 90b6f4600e531c496d849163653acb80c6e933ea
Subproject commit 4f27cc3670bcb4071706bb0e66d6fdfd817e27a4