diff --git a/src/test/java/club/joylink/xiannccda/util/Device.java b/src/test/java/club/joylink/xiannccda/util/Device.java new file mode 100644 index 0000000..3a7ba73 --- /dev/null +++ b/src/test/java/club/joylink/xiannccda/util/Device.java @@ -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 findAllDeviceName(JSONArray dataArray); + + abstract String changeDeviceName(String nccDeviceName); + + static Map 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> match(List nccDeviceNames, DeviceType deviceType, JSONObject jsonObject) { + Device device = findDevice(deviceType); + Map> 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 mapDevices = device.findAllDeviceName(ja); + for (String dn : nccDeviceNames) { + String newName = device.changeDeviceName(dn); + if (mapDevices.contains(newName) || mapDevices.contains(simpleChange(dn))) { + + List 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 findAllDeviceName(JSONArray dataArray) { + List 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 findAllDeviceName(JSONArray dataArray) { + Map jsonMap = dataArray.stream().filter(d -> d instanceof JSONObject).map(d -> (JSONObject) d).collect(Collectors.toMap((d) -> d.getString("code"), Function.identity())); + List 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 findAllDeviceName(JSONArray dataArray) { + List 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 findAllDeviceName(JSONArray dataArray) { + return null; + } + + @Override + String changeDeviceName(String nccDeviceName) { + return null; + } + }*/ +} +