设备名称变更

This commit is contained in:
tiger_zhou 2023-07-19 10:10:38 +08:00
parent 0eec98f251
commit a50aaeaadf
14 changed files with 381 additions and 75 deletions

View File

@ -19,15 +19,21 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
/** 发布时缓存在内存中的线路数据 */
/**
* 发布时缓存在内存中的线路数据
*/
@Slf4j
public class LineGraphicDataRepository {
/** 线路数据信息 */
/**
* 线路数据信息
*/
private static final Map<Integer, Map<String, Map<String, Builder>>> lineGraphMap =
new ConcurrentHashMap<>();
/** 线路各坐标系转换关系 */
/**
* 线路各坐标系转换关系
*/
private static final Map<Integer, Map<String, CoordinateConvertor>> lineCoordinateMain =
new HashMap<>();
@ -41,7 +47,7 @@ public class LineGraphicDataRepository {
if (publishGi.getLineId() != null) {
LayoutGraphicsProto.RtssGraphicStorage storage =
LayoutGraphicsProto.RtssGraphicStorage.parseFrom(publishGi.getProto());
DeviceNameChanger.init(storage);
// DeviceNameChanger.init(storage);
Map<String, Map<String, Builder>> lineDataMap = new HashMap<>();
// 构建区段
sectionInitLineGraph(publishGi.getLineId(), lineDataMap, storage);
@ -85,7 +91,9 @@ public class LineGraphicDataRepository {
return List.of();
}
/** 删除线路绘图数据 */
/**
* 删除线路绘图数据
*/
public static void removeLineGraph(Integer id) {
lineGraphMap.remove(id);
}
@ -317,7 +325,9 @@ public class LineGraphicDataRepository {
return convertorMap.get(convertorKey).convertorKilometer(kilometer, CoordinateEnum.MAIN_LINE);
}
/** 坐标系枚举 */
/**
* 坐标系枚举
*/
@Getter
private enum CoordinateEnum {
MAIN_LINE("MAIN_LINE", List.of("YDK", "ZDK", "XDK", "SDK")),
@ -362,7 +372,9 @@ public class LineGraphicDataRepository {
}
}
/** 坐标转换对象 */
/**
* 坐标转换对象
*/
@Getter
private static class CoordinateConvertor {

View File

@ -1,8 +1,10 @@
package club.joylink.xiannccda.ats.message;
import club.joylink.xiannccda.ats.message.line3.changer.DeviceNameChangerManage;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@ -13,6 +15,15 @@ import org.springframework.stereotype.Component;
@Component
public class OccMessageManage implements ApplicationRunner {
@Value("${occ.host}")
private String serverHost;
@Value("${occ.realPort}")
private Integer realPort;
@Value("${occ.unRealPort}")
private Integer unRealPort;
@Value("${occ.lineId}")
private Integer lineId;
Map<Integer, XianOccMessagingClient> clientMap = new HashMap<>();
public void sendMsg(Integer lineId, MessageData md, boolean isRealTime) {
@ -29,8 +40,9 @@ public class OccMessageManage implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
DeviceNameChangerManage.init();
// 读取数据配置创建客户端
this.registerClient(new XianOccMessagingClient(3, "localhost"));
this.registerClient(new XianOccMessagingClient(this.lineId, this.realPort, this.unRealPort, this.serverHost));
for (XianOccMessagingClient client : this.clientMap.values()) {
client.connect();
}

View File

@ -5,8 +5,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class XianOccMessagingClient {
final int realTimePortBase = 2600;
final int nonRealTimePortBase = 2700;
// final int realTimePortBase = 2600;
// final int nonRealTimePortBase = 2700;
/**
* 线路号 实时信息的侦听端口号为2600+line_id非实时信息的侦听端口号为2700+line_id (如对于地铁1号线来说实时信息的侦听端口号为2601非实时信息的侦听端口号为2701)
*/
@ -33,14 +33,14 @@ public class XianOccMessagingClient {
}
public XianOccMessagingClient(int lineId, String host) {
public XianOccMessagingClient(int lineId, Integer realPort, Integer unRealPort, String host) {
this.host = host;
this.lineId = lineId;
final int realTimePort = realTimePortBase + lineId;
final int nonRealTimePort = nonRealTimePortBase + lineId;
// final int realTimePort = realTimePortBase + lineId;
// final int nonRealTimePort = nonRealTimePortBase + lineId;
// 创建实时和非实时消息连接
this.rtConnection = new OccTcpClientConnection(this, host, realTimePort);
this.nrtConnection = new OccTcpClientConnection(this, host, nonRealTimePort);
this.rtConnection = new OccTcpClientConnection(this, host, realPort);
this.nrtConnection = new OccTcpClientConnection(this, host, unRealPort);
}

View File

@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
@Deprecated
public abstract class DeviceNameChanger implements NameChanger {

View File

@ -0,0 +1,115 @@
package club.joylink.xiannccda.ats.message.line3.changer;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
@Slf4j
public class DeviceNameChangerManage {
final static Table<Integer, DeviceType, List<NameChangerFilter>> CHANGER_TABLE = HashBasedTable.create();
public static String findMatch(DeviceType deviceType, String occName) {
List<NameChangerFilter> filters = CHANGER_TABLE.get(3, deviceType);
if (CollectionUtils.isEmpty(filters)) {
log.error("未找到对应的转换的occName:[{}] debiceType[{}]", occName, deviceType);
return occName;
}
for (NameChangerFilter filter : filters) {
String newName = filter.changeDeviceName(occName);
if (Objects.nonNull(newName)) {
return newName;
}
}
log.error("不能转换的occName:[{}] debiceType[{}]", occName, deviceType);
return occName;
}
public static void init() throws Exception {
List<Class> classes = getClassFQName("club.joylink.xiannccda.ats.message.line3.changer", NameChangerFilter.class);
List<NameChangerFilter> filters = Lists.newArrayList();
for (Class clazz : classes) {
Object obj = clazz.getDeclaredConstructor().newInstance();
filters.add((NameChangerFilter) obj);
}
Map<DeviceType, List<NameChangerFilter>> filteMaper = filters.stream().collect(Collectors.groupingBy(NameChangerFilter::deviceType));
filteMaper.forEach((k, v) -> {
Collections.sort(v, Comparator.comparingInt(NameChangerFilter::filterIndex));
CHANGER_TABLE.put(3, k, v);
});
}
private static List<Class> getClassFQName(String packgeName, Class clz) {
// final String BASE_PACKAGE = packgeName;
final String RESOURCE_PATTERN = "/**/*.class";
List<Class> list = new ArrayList<>();
if (packgeName == null || clz == null) {
return list;
}
//spring工具类可以获取指定路径下的全部类
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
try {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(packgeName) + RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(pattern);
//MetadataReader 的工厂类
MetadataReaderFactory readerfactory = new CachingMetadataReaderFactory(resourcePatternResolver);
for (Resource resource : resources) {
//用于读取类信息
MetadataReader reader = readerfactory.getMetadataReader((org.springframework.core.io.Resource) resource);
//扫描到的class
String classname = reader.getClassMetadata().getClassName();
Class<?> clazz = Class.forName(classname);
// 判断是否实现指定类型
for (Class<?> anInterface : clazz.getInterfaces()) {
if (anInterface == clz) {
list.add(clazz);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) throws Exception {
DeviceNameChangerManage.init();
String d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_TRACK, "T13301C");
System.out.println(d);
d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_TRACK, "T06301C");
System.out.println(d);
d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_TRACK, "T04325G");
System.out.println(d);
/*d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_TRACK, "TD_2DG");
System.out.println(d);
d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SWITCH, "P02309");
System.out.println(d);*/
d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SIGNAL, "S03305");
System.out.println(d);
/* d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SIGNAL, "XR");
System.out.println(d);
d = DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SIGNAL, "X1506_L2");
System.out.println(d);*/
}
}

View File

@ -0,0 +1,14 @@
package club.joylink.xiannccda.ats.message.line3.changer;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
public interface NameChangerFilter {
default int filterIndex() {
return 0;
}
DeviceType deviceType();
String changeDeviceName(String sourceName);
}

View File

@ -0,0 +1,63 @@
package club.joylink.xiannccda.ats.message.line3.changer;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public class SignalChanger {
public static class Signal1Changer implements NameChangerFilter {
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_SIGNAL;
}
@Override
public String changeDeviceName(String sourceName) {
if (sourceName.length() <= 2) {
return sourceName;
}
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})([\\d]{2})3?(.*?)$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
String head = matcher.group(1);
String mid = matcher.group(2);
String tail = matcher.group(3);
return head + tail + mid;
}
return null;
}
}
public static class Signal2Changer implements NameChangerFilter {
@Override
public int filterIndex() {
return 2;
}
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_SIGNAL;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("^([A-Za-z]{0,2}.*?)(\\p{P})(.*?)$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
String head = matcher.group(1);
String flag = matcher.group(2);
String tail = matcher.group(3);
if (StringUtils.equals(flag, "_")) {
flag = "-";
}
return head + flag + tail;
}
return null;
}
}
}

View File

@ -0,0 +1,26 @@
package club.joylink.xiannccda.ats.message.line3.changer;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SwitchChanger implements NameChangerFilter {
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_SWITCH;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(\\d{2})3(.*?)$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
String head = matcher.group(2);
String tail = matcher.group(3);
return "P" + tail + head;
}
return null;
}
}

View File

@ -0,0 +1,96 @@
package club.joylink.xiannccda.ats.message.line3.changer;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TrackChanger {
public static class Track1Changer implements NameChangerFilter {
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_TRACK;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})([\\d]{1,2})3?(.*?)([A-Z])$");
// Pattern regex = Pattern.compile("^([A-Za-z]{1,2})([\\d]{2})3(.*?)([A-Z])$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
String head = matcher.group(2);
String tail = matcher.group(3);
String tailMeta = matcher.group(4);
return "G" + tail + head + tailMeta;
}
return null;
}
}
public static class Track4Changer implements NameChangerFilter {
@Override
public int filterIndex() {
return 3;
}
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_TRACK;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("^[A-Za-z]{1,2}([\\d]{1,2})3(.*?)$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
String head = matcher.group(1);
String tail = matcher.group(2);
return "G" + tail + head;
}
return null;
}
}
public static class Track2Changer implements NameChangerFilter {
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_TRACK;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("T([A-Za-z]{1}\\p{P}.*?)$");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
public static class Track3Changer implements NameChangerFilter {
@Override
public int filterIndex() {
return 99;
}
@Override
public DeviceType deviceType() {
return DeviceType.DEVICE_TYPE_TRACK;
}
@Override
public String changeDeviceName(String sourceName) {
Pattern regex = Pattern.compile("^T(\\d*$)");
Matcher matcher = regex.matcher(sourceName);
if (matcher.find()) {
return "ST" + matcher.group(1);
}
return null;
}
}
}

View File

@ -11,3 +11,8 @@ logging:
club.joylink.xiannccda.mapper: debug
file:
path: /usr/local/joylink/logs/xianncc
occ:
host: localhost
realPort: 123
unRealPort: 123
lineId: 3

View File

@ -15,3 +15,9 @@ logging:
path: /logs/xiannccda
level:
root: "info"
occ:
host: 10.255.11.15
realPort: 5603
unRealPort: 5703
lineId: 3

View File

@ -2,7 +2,7 @@ server:
port: 9081
spring:
profiles:
active: dev
active: local
banner:
location: classpath:banner.txt
datasource:

View File

@ -43,30 +43,6 @@ public class CollectorData {
@Autowired
NccMockDataService nccMockDataService;
/* @Test
public void dd() throws UnsupportedEncodingException {
LambdaQueryWrapper<NccMockData> queryWrapper = Wrappers.<NccMockData>lambdaQuery();
queryWrapper.eq(NccMockData::getMsgId, "TRAIN_INDICATION_UPDATE");
List<MessageResponse> datas = this.nccMockDataService.loadALLData(queryWrapper);
List<TrainIndicationUpdateResponse> bitmapResponses = datas.stream().map(d -> (TrainIndicationUpdateResponse) d).collect(Collectors.toList());
Set<String> sets = new HashSet<>();
for (TrainIndicationUpdateResponse bitmapRespons : bitmapResponses) {
ByteBuf buf = Unpooled.buffer();
// buf.writeInt(842413824);
buf.writeInt(bitmapRespons.getDestinationId());
byte[] dd = new byte[4];
buf.readBytes(dd);
String s = new String(dd, "utf-8");
if (StringUtils.isEmpty(s.trim())) {
System.out.println(bitmapRespons);
}
sets.add(s);
}
System.out.println(sets);
}*/
@Test
public void collector() {

View File

@ -1,5 +1,6 @@
package club.joylink.xiannccda.util;
import club.joylink.xiannccda.ats.message.line3.changer.DeviceNameChangerManage;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
@ -85,14 +86,7 @@ public abstract class Device {
@Override
public String changeDeviceName(String nccDeviceName) {
if (nccDeviceName.length() < 3) {
return nccDeviceName;
}
String started = nccDeviceName.substring(0, 1);
String head = nccDeviceName.substring(1, 3);
String tail = nccDeviceName.substring(4);
return started + tail + head;
return DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SWITCH, nccDeviceName);
}
}
@ -120,18 +114,10 @@ public abstract class Device {
@Override
String changeDeviceName(String nccDeviceName) {
if (nccDeviceName.length() < 4) {
return nccDeviceName;
if (StringUtils.equals(nccDeviceName, "T13301C")) {
System.out.println("aaaaaaaaaaaaaa");
}
String head = nccDeviceName.substring(1, 3);
String tail = nccDeviceName.substring(4);
String tailEnd = "";
if (tail.matches(".*?[A-Z]$")) {
tail = nccDeviceName.substring(4, nccDeviceName.length() - 1);
tailEnd = nccDeviceName.substring(nccDeviceName.length() - 1);
}
return "G" + tail + head + tailEnd;
return DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_TRACK, nccDeviceName);
}
}
@ -145,13 +131,7 @@ public abstract class Device {
@Override
String changeDeviceName(String nccDeviceName) {
if (nccDeviceName.length() <= 3) {
return nccDeviceName;
}
String started = nccDeviceName.substring(0, 1);
String head = nccDeviceName.substring(1, 3);
String tail = nccDeviceName.substring(4);
return started + tail + head;
return DeviceNameChangerManage.findMatch(DeviceType.DEVICE_TYPE_SIGNAL, nccDeviceName);
}
}