模拟设备状态变更接口调整

This commit is contained in:
tiger_zhou 2023-07-17 17:34:03 +08:00
parent bea73f80fb
commit 1cd2b8afb8

View File

@ -0,0 +1,171 @@
package club.joylink.xiannccda.util;
import club.joylink.xiannccda.ats.message.line3.device.DeviceType;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
public abstract class Device {
abstract List<String> findAllDeviceName(JSONArray dataArray);
abstract String changeDeviceName(String nccDeviceName);
static Map<DeviceType, String> NCC_DEVICE_TYPE_NAME_MAPPER = Maps.newHashMap();
static {
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SWITCH, "switchList");
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SIGNAL, "signalList");
NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_TRACK, "sectionList");
// NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_PLATFORM, "stationStandList");
}
private static Device findDevice(DeviceType deviceType) {
if (deviceType == DeviceType.DEVICE_TYPE_SWITCH) {
return new SwitchDevice();
} else if (deviceType == DeviceType.DEVICE_TYPE_TRACK) {
return new TrackDevice();
} else if (deviceType == DeviceType.DEVICE_TYPE_SIGNAL) {
return new SignalDevice();
} /*else if (deviceType == DeviceType.DEVICE_TYPE_PLATFORM) {
return new PlatformDevice();
}*/
return null;
}
private static String simpleChange(String nccSource) {
if (StringUtils.contains(nccSource, "_")) {
return nccSource.replace("_", "-");
}
return nccSource;
}
public static Map<Boolean, List<String>> match(List<String> nccDeviceNames, DeviceType deviceType, JSONObject jsonObject) {
Device device = findDevice(deviceType);
Map<Boolean, List<String>> findMapper = Maps.newHashMap();
findMapper.put(false, Lists.newArrayList(nccDeviceNames));
String jsonName = NCC_DEVICE_TYPE_NAME_MAPPER.get(deviceType);
if (Objects.isNull(device) || Objects.isNull(jsonName)) {
return findMapper;
}
JSONArray ja = jsonObject.getJSONArray(jsonName);
List<String> mapDevices = device.findAllDeviceName(ja);
for (String dn : nccDeviceNames) {
String newName = device.changeDeviceName(dn);
if (mapDevices.contains(newName) || mapDevices.contains(simpleChange(dn))) {
List<String> names = findMapper.get(true);
if (CollectionUtils.isEmpty(names)) {
names = Lists.newArrayList();
findMapper.put(true, names);
}
names.add(dn);
names = findMapper.get(false);
names.remove(dn);
}
}
return findMapper;
}
public static class SwitchDevice extends Device {
@Override
public List<String> findAllDeviceName(JSONArray dataArray) {
List<String> mapDeviceNames = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).map(d -> d.getString("name")).toList();
return mapDeviceNames;
}
@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;
}
}
public static class TrackDevice extends Device {
@Override
List<String> findAllDeviceName(JSONArray dataArray) {
Map<String, JSONObject> jsonMap = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).collect(Collectors.toMap((d) -> d.getString("code"), Function.identity()));
List<String> newNames = Lists.newArrayList();
for (JSONObject value : jsonMap.values()) {
// JSONArray hasLogic = value.getJSONArray("logicSectionList");
String physicsTrackCode = value.getString("parentCode");
String name = value.getString("name");
if (StringUtils.isEmpty(physicsTrackCode)) {
newNames.add(name);
} else {
JSONObject physicsTrack = jsonMap.get(physicsTrackCode);
String pn = physicsTrack.getString("name");
newNames.add(pn + name);
}
}
return newNames;
}
@Override
String changeDeviceName(String nccDeviceName) {
if (nccDeviceName.length() < 4) {
return nccDeviceName;
}
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;
}
}
public static class SignalDevice extends Device {
@Override
List<String> findAllDeviceName(JSONArray dataArray) {
List<String> mapDeviceNames = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).map(d -> d.getString("name")).toList();
return mapDeviceNames;
}
@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;
}
}
/* public static class PlatformDevice extends Device {
@Override
List<String> findAllDeviceName(JSONArray dataArray) {
return null;
}
@Override
String changeDeviceName(String nccDeviceName) {
return null;
}
}*/
}