设备名称变更

This commit is contained in:
tiger_zhou 2023-07-18 14:19:00 +08:00
parent da6e506688
commit a81568ebfb
2 changed files with 57 additions and 87 deletions

View File

@ -6,23 +6,13 @@ import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.RtssGraphicStorage;
import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section;
import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Signal;
import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Turnout;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.Message;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public abstract class DeviceNameChanger {
public abstract class DeviceNameChanger implements NameChanger {
abstract void collectNames(LayoutGraphicsProto.RtssGraphicStorage graphicStorage);
abstract String changeDeviceName(String nccDeviceName, List<String> graphicNames);
static Map<DeviceType, DeviceNameChanger> NCC_DEVICE_TYPE_NAME_MAPPER = Maps.newHashMap();
@ -30,7 +20,6 @@ public abstract class DeviceNameChanger {
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SWITCH, new SwitchDevice());
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SIGNAL, new SignalDevice());
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_TRACK, new TrackDevice());
// NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_PLATFORM, "stationStandList");
}
private final static Map<DeviceType, List<String>> DEVICE_NAMES = Maps.newHashMap();
@ -43,7 +32,7 @@ public abstract class DeviceNameChanger {
}
public static String nameMatch(DeviceType dt, String name) {
public static String findMatch(DeviceType dt, String name) {
DeviceNameChanger nameChanger = NCC_DEVICE_TYPE_NAME_MAPPER.get(dt);
if (Objects.nonNull(nameChanger)) {
List<String> deviceNames = DEVICE_NAMES.get(dt);
@ -52,11 +41,16 @@ public abstract class DeviceNameChanger {
return name;
}
public static void main(String[] args) {
// X1506_L2
new TrackDevice().changeDeviceName("TP_2DG", List.of("P_2DG"));
new SignalDevice().changeDeviceName("X1506_L2", List.of("X1506-L2"));
}
public static class SwitchDevice extends DeviceNameChanger {
@Override
void collectNames(RtssGraphicStorage graphicStorage) {
public void collectNames(RtssGraphicStorage graphicStorage) {
List<String> names = graphicStorage.getTurnoutsList().stream().map(Turnout::getCode).toList();
DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SWITCH, names);
}
@ -78,103 +72,36 @@ public abstract class DeviceNameChanger {
@Override
void collectNames(RtssGraphicStorage graphicStorage) {
public void collectNames(RtssGraphicStorage graphicStorage) {
List<String> names = graphicStorage.getSectionList().stream().map(Section::getCode).toList();
DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_TRACK, names);
}
@Override
String changeDeviceName(String nccDeviceName, List<String> graphicNames) {
public String changeDeviceName(String nccDeviceName, List<String> graphicNames) {
if (nccDeviceName.length() < 4) {
return nccDeviceName;
}
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$");
Matcher matcher = regex.matcher(nccDeviceName);
String newName = "";
String code = "";
if (matcher.find()) {
code = matcher.group(2);
if (StringUtils.contains(code, "_")) {
newName = code;
} else {
String head = code.substring(0, 2);
String end = code.substring(3);
String tail = "";
if (code.matches(".*?[A-Z]$")) {
tail = end.substring(end.length() - 1);
}
newName = end + head + tail;
}
}
final String finalName = newName;
final String finalCode = code;
String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null);
if (Objects.nonNull(finder)) {
return finder;
}
return nccDeviceName;
return super.changeDeviceName(nccDeviceName, graphicNames);
}
}
public static class SignalDevice extends DeviceNameChanger {
@Override
void collectNames(RtssGraphicStorage graphicStorage) {
public void collectNames(RtssGraphicStorage graphicStorage) {
List<String> names = graphicStorage.getSignalsList().stream().map(Signal::getCode).toList();
DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SIGNAL, names);
}
@Override
String changeDeviceName(String nccDeviceName, List<String> graphicNames) {
public String changeDeviceName(String nccDeviceName, List<String> graphicNames) {
if (nccDeviceName.length() <= 3) {
return nccDeviceName;
}
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$");
Matcher matcher = regex.matcher(nccDeviceName);
String newName = "";
String code = "";
if (matcher.find()) {
code = matcher.group(2);
if (StringUtils.contains(code, "_")) {
newName = code;
} else {
String head = code.substring(0, 2);
String end = code.substring(3);
String tail = "";
if (code.matches(".*?[A-Z]$")) {
tail = end.substring(end.length() - 1);
}
newName = end + head + tail;
}
}
final String finalName = newName.replaceAll("_", "-");
final String finalCode = code.replaceAll("_", "-");
String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null);
if (Objects.nonNull(finder)) {
return finder;
}
return nccDeviceName;
return super.changeDeviceName(nccDeviceName.replaceAll("_", "-"), graphicNames);
}
}
public static void main(String[] args) {
String nccDeviceName = "TP_2DG";
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$");
Matcher matcher = regex.matcher(nccDeviceName);
String newName = "";
String code = "";
if (matcher.find()) {
code = matcher.group(2);
String head = code.substring(0, 2);
String end = code.substring(3);
String tail = "";
if (code.matches(".*?[A-Z]$")) {
tail = end.substring(end.length() - 1);
}
newName = end + head + tail;
System.out.println(newName);
}
}
}

View File

@ -0,0 +1,43 @@
package club.joylink.xiannccda.ats.message.line3;
import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public interface NameChanger {
void collectNames(LayoutGraphicsProto.RtssGraphicStorage graphicStorage);
default String changeDeviceName(String nccDeviceName, List<String> graphicNames) {
Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$");
Matcher matcher = regex.matcher(nccDeviceName);
String newName = "";
String code = "";
if (matcher.find()) {
code = matcher.group(2);
if (StringUtils.contains(code, "_")) {
newName = code;
} else {
String head = code.substring(0, 2);
String end = code.substring(3);
String tail = "";
if (code.matches(".*?[A-Z]$")) {
tail = end.substring(end.length() - 1);
}
newName = end + head + tail;
}
}
final String finalName = newName;
final String finalCode = code;
String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null);
if (Objects.nonNull(finder)) {
return finder;
}
return nccDeviceName;
}
}