【增加列车信息转换】

This commit is contained in:
weizhihong 2023-06-27 17:58:10 +08:00
parent 465bb1c96d
commit 6e7a720425
7 changed files with 169 additions and 177 deletions

View File

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
@ -38,8 +39,7 @@ public class LineGraphicDataRepository {
/**
* 缓存线路数据信息
*
* @param lineCode 线路编号
* @param data 线路地图信息
* @param publishGi 发布信息
*/
public static void putLineGraph(PublishedGi publishGi) {
try {
@ -58,6 +58,27 @@ public class LineGraphicDataRepository {
}
}
/**
* 获取线路上区段名为sectionName的公里标
*
* @param lineId 线路ID
* @param sectionName 区段名称
* @return 公里标
*/
public static List<String> getSectionDefaultKmCodeList(int lineId, String sectionName) {
Map<String, Map<String, ? extends Builder>> lineDataMap = lineGraphMap.get(lineId);
if (CollectionUtils.isNotEmpty(lineDataMap)) {
Map<String, ? extends Builder> sectionMap = lineDataMap.get(DeviceType.Section.name());
for (Builder v : sectionMap.values()) {
DeviceInfoProto.Section.Builder builder = (DeviceInfoProto.Section.Builder) v;
if (Objects.equals(builder.getCode(), sectionName)) {
return builder.getKilometerCodeList();
}
}
}
return List.of();
}
/**
* 构建程序中的区段信息
*

View File

@ -1,18 +1,19 @@
package club.joylink.xiannccda.ats.message.collect;
import club.joylink.xiannccda.ats.message.MessageData;
import club.joylink.xiannccda.ats.message.MessageId;
import java.util.List;
/** 消息数据额外转换时间,比如:线路的数据转换为线网数据 */
public abstract class DeviceStatusConvertor {
/**
* 获取转换ID
* 获取对应的消息ID
*
* @return 转换ID
* @return 消息ID
*/
abstract String getId();
public abstract MessageId getMessageId();
/** 任务执行逻辑 */
abstract void run(List<MessageData> messageData);
public abstract void run(List<MessageData> messageDataList);
}

View File

@ -23,7 +23,7 @@ public class DeviceStatusConvertorManager {
public static void addStatusConvertor(MessageId msgId, DeviceStatusConvertor convertor) {
Map<String, DeviceStatusConvertor> convertorMap =
CONVERTOR_MAP.computeIfAbsent(msgId, k -> new ConcurrentHashMap<>());
convertorMap.put(convertor.getId(), convertor);
convertorMap.put(convertor.getClass().getSimpleName(), convertor);
}
/**

View File

@ -0,0 +1,67 @@
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.DeviceStatusData;
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.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;
import java.util.Objects;
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;
}
@Override
public void run(List<MessageData> messageDataList) {
List<Builder> buildList = Lists.newArrayList();
for (MessageData data : messageDataList) {
if (Objects.equals(getMessageId(), data.getMsgId())) { // 消息类型一致
TrainIndicationInitResponse response = (TrainIndicationInitResponse) data;
List<TrainCell> trainCellList = response.getTrains();
for (TrainCell trainCell : trainCellList) {
WsMessageProto.WsLineNetTrainOffsetMessage.Builder offset =
WsMessageProto.WsLineNetTrainOffsetMessage.newBuilder();
offset.setLineId(response.getLineId()); // 线路id
offset.setTrainIndex(trainCell.getTrainIndex()); // 列车标示号全线唯一
offset.setGroupId(trainCell.getGroupId()); // 车组号
offset.setDestinationId(trainCell.getDestinationId()); // 目的地ID
offset.setShow(false); // 是否显示
offset.setDir(0); // 初始设置无运行方向
// 获取到当前区段公里标
if (DeviceType.DEVICE_TYPE_TRACK.equals(trainCell.getDevType())) {
List<String> kmCodeList =
LineGraphicDataRepository.getSectionDefaultKmCodeList(
response.getLineId(), trainCell.getDevName());
if (CollectionUtils.isNotEmpty(kmCodeList)) {
offset.setKilometerCode(kmCodeList.get(0));
}
}
buildList.add(offset);
}
}
}
// 增加设备信息
DeviceStatusDataOperate.addDevices(buildList, dataSource);
}
}

View File

@ -0,0 +1,56 @@
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.DeviceStatusData;
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.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.protobuf.GeneratedMessageV3.Builder;
import java.util.List;
import java.util.Objects;
public class LineNetTrainRecordConvertor extends DeviceStatusConvertor {
final DeviceStatusData dataSource;
public LineNetTrainRecordConvertor(DeviceStatusData dataSource) {
this.dataSource = dataSource;
}
@Override
public MessageId getMessageId() {
return MessageId.TRAIN_RECORD;
}
@Override
public void run(List<MessageData> messageDataList) {
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()); // 车组号
offset.setDestinationId(response.getDestinationId()); // 目的地ID
offset.setDir(response.getDirection().getValue()); // 初始设置无运行方向
// 获取到当前区段公里标
List<String> kmCodeList =
LineGraphicDataRepository.getSectionDefaultKmCodeList(
response.getLineId(), response.getTrackName());
if (CollectionUtils.isNotEmpty(kmCodeList)) {
offset.setKilometerCode(kmCodeList.get(0));
}
buildList.add(offset);
}
}
// 增加设备信息
DeviceStatusDataOperate.addDevices(buildList, dataSource);
}
}

View File

@ -149,6 +149,7 @@ public class TrainRecordResponse extends MessageResponse {
* 0x02上行<br>
* 0x00无方向<br>
*/
@Getter
public static enum DirectionEnum {
Down(0x01),
Up(0x02),

View File

@ -1,110 +1,23 @@
package club.joylink.xiannccda.service;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusConvertorManager;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusData;
import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataRepository;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import club.joylink.xiannccda.dto.protos.DeviceStatusProto;
import club.joylink.xiannccda.dto.protos.TrainProto.TrainInfo;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainInitConvertor;
import club.joylink.xiannccda.ats.message.collect.convertor.LineNetTrainRecordConvertor;
import club.joylink.xiannccda.ws.IMessageServer;
import club.joylink.xiannccda.ws.LineNetMessageServer;
import club.joylink.xiannccda.ws.WsMessageServerManager;
import com.google.protobuf.GeneratedMessageV3.Builder;
import jakarta.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* 线路设备信息更新
*/
/** 线路设备信息更新 */
@Slf4j
@Service
public class LineDeviceStatusService {
private String dataKey = "lineNet";
private List<String> rightPathList =
List.of(
"G0105A", "G0105B", "G0105C", "DG0107", "G0109A", "G0109B", "G0109C", "G0109D", "G0109E",
"G0201A", "G0201B", "G0201C", "G0201D", "G0201E", "G0201F", "G0203A", "G0203B", "G0203C",
"G0203D", "G0301A", "G0301B", "G0301C", "G0301D", "G0301E", "DG0303", "G0401A", "G0401B",
"G0401C", "G0401D", "G0401E", "G0401F", "G0401G", "G0401H", "G0403A", "G0403B", "G0403C",
"G0403D", "G0501A", "G0501B", "G0501C", "G0501D", "DG0503", "DG0505", "G0507A", "G0507B",
"G0507C", "G0507D", "G0601A", "G0601B", "G0601C", "G0601D", "G0601E", "G0601F", "G0601G",
"G0601H", "G0603A", "G0603B", "G0603C", "G0603D", "G0701A", "G0701B", "G0701C", "G0701D",
"G0701E", "G0701F", "G0703A", "G0703B", "G0703C", "G0703D", "G0703E", "DG0705", "G0801A",
"G0801B", "G0801C", "G0801D", "G0801E", "G0801F", "G0803A", "G0803B", "G0803C", "G0803D",
"G0901A", "G0901B", "G0901C", "G0901D", "G0901E", "G0901F", "G0903A", "G0903B", "G0903C",
"G0903D", "DG0905", "G1001A", "G1001B", "G1001C", "G1001D", "G1001E", "G1001F", "G1001G",
"G1001H", "G1003A", "G1003B", "G1003C", "G1003D", "G1101A", "G1101B", "G1101C", "G1101D",
"G1101E", "G1101F", "G1101G", "G1101H", "G1101I", "G1103A", "G1103B", "G1103C", "G1103D",
"G1201A", "G1201B", "G1201C", "DG1203", "G1205A", "G1205B", "G1205C", "G1205D", "G1301A",
"G1301B", "G1301C", "G1301D", "G1301E", "G1303A", "G1303B", "G1303C", "G1303D", "G1401A",
"G1401B", "G1401C", "G1401D", "G1403A", "G1403B", "G1403C", "G1403D", "G1501A", "G1501B",
"G1501C", "G1501D", "G1503A", "G1503B", "G1503C", "G1503D", "DG1505", "G1601A", "G1601B",
"G1601C", "G1601D", "G1601E", "G1601F", "G1601G", "G1603A", "G1603B", "G1603C", "G1603D",
"G1701A", "G1701B", "G1701C", "G1701D", "G1701E", "G1701F", "G1701G", "G1703A", "G1703B",
"G1703C", "G1703D", "G1801A", "G1801B", "G1801C", "G1801D", "G1801E", "G1801F", "G1801G",
"G1801H", "G1801I", "DG1803", "G1805A", "G1805B", "G1805C", "G1805D", "G1901A", "G1901B",
"G1901C", "G1901D", "G1901E", "G1901F", "G1901G", "G1901H", "G1901I", "G1903A", "G1903B",
"G1903C", "G1903D", "G2001A", "G2001B", "G2001C", "G2001D", "G2001E", "G2001F", "G2001G",
"G2003A", "G2003B", "G2003C", "G2003D", "G2101A", "G2101B", "G2101C", "G2101D", "G2101E",
"G2101F", "G2101G", "G2103A", "G2103B", "G2103C", "G2103D", "DG2105", "G2107A", "G2107B",
"DG2109", "G2201A", "G2201B", "G2201C", "G2201D", "G2201E", "G2201F", "G2201G", "G2201H",
"G2201I", "G2203A", "G2203B", "G2203C", "G2203D", "G2301A", "G2301B", "G2301C", "G2301D",
"G2301E", "G2301F", "G2301G", "G2301H", "G2301I", "G2301J", "G2301K", "G2303A", "G2303B",
"G2303C", "G2303D", "G2401A", "G2401B", "G2401C", "G2401D", "G2401E", "G2401F", "DG2403",
"G2405A", "G2405B", "G2405C", "G2405D", "G2501A", "G2501B", "G2501C", "G2501D", "G2501E",
"G2503A", "G2503B", "G2503C", "G2503D", "G2601A", "G2601B", "G2601C", "G2601D", "G2601E",
"G2601F", "DG2603", "G2605A", "G2605B", "G2605C", "DG2607", "DG2609", "G2611A", "G2611B",
"G2611C");
private List<String> leftPathList =
List.of(
"G2610A", "G2610B", "G2610C", "DG2608", "DG2606", "G2604A", "G2604B", "G2604C", "DG2602",
"G2504A", "G2504B", "G2504C", "G2504D", "G2504E", "G2504F", "G2502A", "G2502B", "G2502C",
"G2502D", "G2406A", "G2406B", "G2406C", "G2406D", "G2406E", "G2404A", "G2404B", "G2404C",
"G2404D", "DG2402", "G2304A", "G2304B", "G2304C", "G2304D", "G2304E", "G2304F", "G2304G",
"G2302A", "G2302B", "G2302C", "G2302D", "G2204A", "G2204B", "G2204C", "G2204D", "G2204E",
"G2204F", "G2204G", "G2204H", "G2204I", "G2204J", "G2204K", "G2202A", "G2202B", "G2202C",
"G2202D", "G2110A", "G2110B", "G2110C", "G2110D", "G2110E", "G2110F", "G2110G", "G2110H",
"G2110I", "DG2108", "G2106A", "G2106B", "DG2104", "G2102A", "G2102B", "G2102C", "G2102D",
"G2004A", "G2004B", "G2004C", "G2004D", "G2004E", "G2004F", "G2004G", "G2002A", "G2002B",
"G2002C", "G2002D", "G1904A", "G1904B", "G1904C", "G1904D", "G1904E", "G1904F", "G1904G",
"G1902A", "G1902B", "G1902C", "G1902D", "G1806A", "G1806B", "G1806C", "G1806D", "G1806E",
"G1806F", "G1806G", "G1806H", "G1804A", "G1804B", "G1804C", "G1804D", "DG1802", "G1704A",
"G1704B", "G1704C", "G1704D", "G1704E", "G1704F", "G1704G", "G1704H", "G1704I", "G1704J",
"G1702A", "G1702B", "G1702C", "G1702D", "G1604A", "G1604B", "G1604C", "G1604D", "G1604E",
"G1604F", "G1604G", "G1602A", "G1602B", "G1602C", "G1602D", "G1508A", "G1508B", "G1508C",
"G1508D", "G1508E", "DG1506", "DG1504", "G1502A", "G1502B", "G1502C", "G1502D", "G1404A",
"G1404B", "G1404C", "G1404D", "G1402A", "G1402B", "G1402C", "G1402D", "G1304A", "G1304B",
"G1304C", "G1304D", "G1302A", "G1302B", "G1302C", "G1302D", "G1206A", "G1206B", "G1206C",
"G1206D", "G1206E", "G1204A", "G1204B", "G1204C", "G1204D", "DG1202", "G1104A", "G1104B",
"G1104C", "G1102A", "G1102B", "G1102C", "G1102D", "G1004A", "G1004B", "G1004C", "G1004D",
"G1004E", "G1004F", "G1004G", "G1004H", "G1004I", "G1002A", "G1002B", "G1002C", "G1002D",
"G0908A", "G0908B", "G0908C", "G0908D", "G0908E", "G0908F", "G0908G", "DG0906", "DG0904",
"G0902A", "G0902B", "G0902C", "G0902D", "G0804A", "G0804B", "G0804C", "G0804D", "G0804E",
"G0804F", "G0802A", "G0802B", "G0802C", "G0802D", "G0710A", "G0710B", "G0710C", "G0710D",
"G0710E", "G0710F", "DG0708", "DG0704", "G0702A", "G0702B", "G0702C", "G0702D", "G0604A",
"G0604B", "G0604C", "G0604D", "G0604E", "G0604F", "G0602A", "G0602B", "G0602C", "G0602D",
"G0506A", "G0506B", "G0506C", "G0506D", "G0506E", "G0506F", "G0506G", "G0504A", "G0504B",
"G0504C", "G0504D", "DG0502", "G0404A", "G0404B", "G0404C", "G0404D", "G0404E", "G0404F",
"G0402A", "G0402B", "G0402C", "G0402D", "G0306A", "G0306B", "G0306C", "G0306D", "G0306E",
"G0306F", "G0306G", "G0306H", "G0304A", "G0304B", "G0304C", "G0304D", "DG0302", "G0206A",
"G0206B", "G0206C", "G0206D", "G0206E", "G0204A", "G0204B", "G0204C", "G0204D", "G0202A",
"G0202B", "G0202C", "G0202D", "G0202E", "G0110A", "G0110B", "G0110C", "G0110D", "G0110E",
"DG0108", "G0106A", "G0106B", "G0106C", "DG0104");
private List<String> trainList =
new ArrayList<>(
List.of(
"301", "302", "303", "304", "305", "306", "307", "308", "309", "310", "311", "312"));
public static final String DATA_KEY = "lineNet";
private final WsMessageServerManager wsMessageServerManager;
@ -114,83 +27,16 @@ public class LineDeviceStatusService {
@PostConstruct
public void deviceStatusRefresh() {
IMessageServer iMessageServer =
new LineNetMessageServer(DeviceStatusDataRepository.getDeviceStatusData(dataKey));
// 设备状态数据集合
DeviceStatusData deviceStatusData = DeviceStatusDataRepository.getDeviceStatusData(DATA_KEY);
// 添加初始化转换方法
LineNetTrainInitConvertor initConvertor = new LineNetTrainInitConvertor(deviceStatusData);
DeviceStatusConvertorManager.addStatusConvertor(initConvertor.getMessageId(), initConvertor);
// 列车报点信息转换方法
LineNetTrainRecordConvertor recConvertor = new LineNetTrainRecordConvertor(deviceStatusData);
DeviceStatusConvertorManager.addStatusConvertor(recConvertor.getMessageId(), recConvertor);
// websocket发送服务
IMessageServer iMessageServer = new LineNetMessageServer(deviceStatusData);
wsMessageServerManager.registerMessageServer(iMessageServer);
// refreshTestData();
}
private void refreshTestData() {
Map<String, Integer> offsetMap = new HashMap<>(trainList.size()); // 列车到达位置
Map<String, TrainInfo.Builder> trainInfoMap = new HashMap<>(trainList.size()); // 列车信息集合
for (int i = 0, len = trainList.size(); i < len; i++) {
String code = trainList.get(i);
TrainInfo.Builder builder = TrainInfo.newBuilder(); // 列车信息
builder.setLineId(3);
builder.setTrainIndex(code);
builder.setGroupId(code);
builder.setDevType(DeviceStatusProto.DeviceType.forNumber(DeviceType.DEVICE_TYPE_TRACK.getVal()));
builder.setShow(true);
int offset = (i / 2) * 4;
if (i % 2 == 0) {
// builder.setDirection(0);
builder.setDevName(rightPathList.get(offset));
builder.setDestinationId(rightPathList.size() - 1);
} else {
// builder.setDirection(1);
builder.setDevName(leftPathList.get(offset));
builder.setDestinationId(leftPathList.size() - 1);
}
offsetMap.put(code, offset);
trainInfoMap.put(code, builder);
}
AtomicInteger count = new AtomicInteger(0);
Executors.newSingleThreadScheduledExecutor()
.scheduleWithFixedDelay(
() -> {
log.info("数据处理第" + count.incrementAndGet() + "");
List<String> codeList = randomTrainList();
List<Builder> builders = new LinkedList<>();
trainInfoMap.forEach(
(k, v) -> {
if (!codeList.contains(k)) {
return;
}
/*List<String> path = v.getDirection() % 2 == 0 ? rightPathList : leftPathList;
// 如果已经到达目的地
if (offsetMap.get(k) == v.getDestinationId()) {
v.setShow(false); // 到达终点隐藏
offsetMap.put(k, 0); // 位置归零
v.setDirection(v.getDirection() == 0 ? 1 : 0); // 方向转换
} else {
v.setDevName(path.get(offsetMap.get(k))); // 更新到下一个位置
v.setDestinationId(path.size() - 1);
offsetMap.put(k, offsetMap.get(k) + 1);
}*/
builders.add(v.clone());
});
DeviceStatusDataRepository.addDeviceStatusDataList(dataKey, builders);
},
1500,
1500,
TimeUnit.MILLISECONDS);
}
public List<String> randomTrainList() {
int num = (int) (Math.random() * trainList.size());
List<String> codeList = new ArrayList<>(num);
try {
for (int i = 0, len = trainList.size(); i < num; i++, len--) {
int index = (int) (Math.random() * len);
String code = trainList.get(index);
trainList.set(index, trainList.get(trainList.size() - 1));
trainList.set(trainList.size() - 1, code);
codeList.add(code);
}
} catch (Exception e) {
log.error("获取列车列表出错", e);
}
return codeList;
}
}