|
|
@@ -119,12 +119,11 @@ public class PLCUtils {
|
|
|
synchronized (getPlcLock(config)) {
|
|
|
McPLC plc = getCachedPlc(config);
|
|
|
try {
|
|
|
- return new StationStatus(
|
|
|
- readBoolean(plc, config, config.getAllowStart()),
|
|
|
- readBoolean(plc, config, config.getAllowDown()),
|
|
|
- readBoolean(plc, config, config.getStartMethod()),
|
|
|
- readBoolean(plc, config, config.getStopMethod()),
|
|
|
- readBoolean(plc, config, config.getFault()));
|
|
|
+ StationStatus batchStatus = readStationStatusBatch(plc, config);
|
|
|
+ if (batchStatus != null) {
|
|
|
+ return batchStatus;
|
|
|
+ }
|
|
|
+ return readStationStatusOneByOne(plc, config);
|
|
|
} catch (RuntimeException e) {
|
|
|
discardCachedPlc(config, plc);
|
|
|
throw e;
|
|
|
@@ -132,6 +131,47 @@ public class PLCUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private static StationStatus readStationStatusOneByOne(McPLC plc, StationConfig config) {
|
|
|
+ return new StationStatus(
|
|
|
+ readBoolean(plc, config, config.getAllowStart()),
|
|
|
+ readBoolean(plc, config, config.getAllowDown()),
|
|
|
+ readBoolean(plc, config, config.getStartMethod()),
|
|
|
+ readBoolean(plc, config, config.getStopMethod()),
|
|
|
+ readBoolean(plc, config, config.getFault()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static StationStatus readStationStatusBatch(McPLC plc, StationConfig config) {
|
|
|
+ int allowStart = mAddressNumber(config.getAllowStart());
|
|
|
+ int allowDown = mAddressNumber(config.getAllowDown());
|
|
|
+ int start = mAddressNumber(config.getStartMethod());
|
|
|
+ int finish = mAddressNumber(config.getStopMethod());
|
|
|
+ int fault = mAddressNumber(config.getFault());
|
|
|
+ if (allowStart < 0 || allowDown < 0 || start < 0 || finish < 0 || fault < 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ int min = Math.min(Math.min(allowStart, allowDown), Math.min(Math.min(start, finish), fault));
|
|
|
+ int max = Math.max(Math.max(allowStart, allowDown), Math.max(Math.max(start, finish), fault));
|
|
|
+ int count = max - min + 1;
|
|
|
+ if (count > 32) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String startAddress = "M" + min;
|
|
|
+ try {
|
|
|
+ List<Boolean> values = plc.readBoolean(startAddress, count);
|
|
|
+ return new StationStatus(
|
|
|
+ getBitValue(values, allowStart - min),
|
|
|
+ getBitValue(values, allowDown - min),
|
|
|
+ getBitValue(values, start - min),
|
|
|
+ getBitValue(values, finish - min),
|
|
|
+ getBitValue(values, fault - min));
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logPlcFailure("readStationStatusBatch", config, startAddress + "-" + "M" + max, plc, e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static class StationStatus {
|
|
|
private final boolean allowStart;
|
|
|
private final boolean allowDown;
|
|
|
@@ -570,6 +610,28 @@ public class PLCUtils {
|
|
|
return points;
|
|
|
}
|
|
|
|
|
|
+ private static int mAddressNumber(String address) {
|
|
|
+ if (isEmpty(address)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ String normalized = address.trim().toUpperCase(Locale.ROOT);
|
|
|
+ if (!normalized.startsWith("M")) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(normalized.substring(1));
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean getBitValue(List<Boolean> values, int index) {
|
|
|
+ return values != null
|
|
|
+ && index >= 0
|
|
|
+ && index < values.size()
|
|
|
+ && Boolean.TRUE.equals(values.get(index));
|
|
|
+ }
|
|
|
+
|
|
|
private static String currentTime() {
|
|
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
}
|