jingbo пре 2 недеља
родитељ
комит
d73ffd615c
1 измењених фајлова са 68 додато и 6 уклоњено
  1. 68 6
      src/com/mes/util/PLCUtils.java

+ 68 - 6
src/com/mes/util/PLCUtils.java

@@ -119,12 +119,11 @@ public class PLCUtils {
         synchronized (getPlcLock(config)) {
         synchronized (getPlcLock(config)) {
             McPLC plc = getCachedPlc(config);
             McPLC plc = getCachedPlc(config);
             try {
             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) {
             } catch (RuntimeException e) {
                 discardCachedPlc(config, plc);
                 discardCachedPlc(config, plc);
                 throw e;
                 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 {
     public static class StationStatus {
         private final boolean allowStart;
         private final boolean allowStart;
         private final boolean allowDown;
         private final boolean allowDown;
@@ -570,6 +610,28 @@ public class PLCUtils {
         return points;
         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() {
     private static String currentTime() {
         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
     }
     }