Jelajahi Sumber

涂胶优化

liuwei 3 jam lalu
induk
melakukan
8c381d23fa
1 mengubah file dengan 108 tambahan dan 60 penghapusan
  1. 108 60
      src/com/mes/plc/TjPlcMonitorPanel.java

+ 108 - 60
src/com/mes/plc/TjPlcMonitorPanel.java

@@ -149,55 +149,36 @@ public class TjPlcMonitorPanel extends JPanel {
                 s7Client.updateHost(config.host);
             }
 
-            Exception a1Err = null;
-            Exception b1Err = null;
-            GunSnapshot a1 = null;
-            GunSnapshot b1 = null;
-            try {
-                a1 = readGun(config.a1);
-            } catch (Exception e) {
-                a1Err = e;
-            }
-            try {
-                b1 = readGun(config.b1);
-            } catch (Exception e) {
-                b1Err = e;
-            }
-
-            final GunSnapshot a1Final = a1;
-            final GunSnapshot b1Final = b1;
-            final String a1Msg = a1Err == null ? null : a1Err.getMessage();
-            final String b1Msg = b1Err == null ? null : b1Err.getMessage();
-            final boolean a1Ok = a1Err == null;
-            final boolean b1Ok = b1Err == null;
+            final GunSnapshot a1 = readGun(config.a1);
+            final GunSnapshot b1 = readGun(config.b1);
+            final boolean a1Ok = a1.allOk();
+            final boolean b1Ok = b1.allOk();
+            final boolean connDead = a1.okCount() == 0 && b1.okCount() == 0;
 
             SwingUtilities.invokeLater(() -> {
-                if (a1Final != null) {
-                    a1View.apply(a1Final);
-                }
-                if (b1Final != null) {
-                    b1View.apply(b1Final);
-                }
+                a1View.apply(a1);
+                b1View.apply(b1);
                 if (a1Ok && b1Ok) {
                     statusLabel.setForeground(new Color(0, 128, 0));
                     statusLabel.setText("已连接 · 实时刷新中");
+                } else if (connDead) {
+                    statusLabel.setForeground(Color.RED);
+                    statusLabel.setText("A1/B1均失败: " + shorten(a1.firstError()));
                 } else if (!a1Ok && !b1Ok) {
                     statusLabel.setForeground(Color.RED);
-                    statusLabel.setText("A1/B1均失败(常见:DB不存在/优化块/越界): " + shorten(a1Msg));
+                    statusLabel.setText("A1失败[" + a1.firstFailAddr + "]  B1失败[" + b1.firstFailAddr + "]");
                 } else if (!a1Ok) {
                     statusLabel.setForeground(Color.RED);
-                    statusLabel.setText("A1失败: " + shorten(a1Msg));
+                    statusLabel.setText("A1失败[" + a1.firstFailAddr + "]: " + shorten(a1.firstError()));
                 } else {
                     statusLabel.setForeground(Color.RED);
-                    statusLabel.setText("B1失败: " + shorten(b1Msg));
+                    statusLabel.setText("B1失败[" + b1.firstFailAddr + "]: " + shorten(b1.firstError()));
                 }
             });
 
-            if (a1Err != null && b1Err != null) {
-                if (s7Client != null) {
-                    s7Client.close();
-                    s7Client = null;
-                }
+            if (connDead && s7Client != null) {
+                s7Client.close();
+                s7Client = null;
             }
         } catch (Exception e) {
             if (s7Client != null) {
@@ -225,28 +206,90 @@ public class TjPlcMonitorPanel extends JPanel {
         return m;
     }
 
-    private GunSnapshot readGun(TjPlcConfig.GunAddr gun) throws Exception {
+    private GunSnapshot readGun(TjPlcConfig.GunAddr gun) {
         GunSnapshot s = new GunSnapshot();
-        s.frameCode = s7Client.readFrameCode(gun);
-        s.scanFeedback = s7Client.readBit(gun.scanFeedbackByte, gun.scanFeedbackBit);
-        s.allowEntry = s7Client.readBit(gun.allowEntryByte, gun.allowEntryBit);
-        s.pathDone = s7Client.readBit(gun.pathDoneByte, gun.pathDoneBit);
-        s.allDone = s7Client.readBit(gun.allDoneByte, gun.allDoneBit);
-        s.seq = s7Client.readUInt16(gun.seqOffset);
-        s.glueSpeed = s7Client.readFloat(gun.glueSpeedOffset);
-        s.glueLength = s7Client.readFloat(gun.glueLengthOffset);
+        s.frameCode = readText(() -> {
+            String v = s7Client.readFrameCode(gun);
+            return (v == null || v.isEmpty()) ? "(空)" : v;
+        }, gun.frameCodeAddr(config.db), s);
+        s.scanFeedback = readText(() -> s7Client.readBit(gun.scanFeedbackByte, gun.scanFeedbackBit) ? "1" : "0",
+                gun.scanFeedbackAddr(config.db), s);
+        s.allowEntry = readText(() -> s7Client.readBit(gun.allowEntryByte, gun.allowEntryBit) ? "1" : "0",
+                gun.allowEntryAddr(config.db), s);
+        s.pathDone = readText(() -> s7Client.readBit(gun.pathDoneByte, gun.pathDoneBit) ? "1" : "0",
+                gun.pathDoneAddr(config.db), s);
+        s.allDone = readText(() -> s7Client.readBit(gun.allDoneByte, gun.allDoneBit) ? "1" : "0",
+                gun.allDoneAddr(config.db), s);
+        s.seq = readText(() -> String.valueOf(s7Client.readUInt16(gun.seqOffset)),
+                gun.seqAddr(config.db), s);
+        s.glueSpeed = readText(() -> String.format("%.3f", s7Client.readFloat(gun.glueSpeedOffset)),
+                gun.glueSpeedAddr(config.db), s);
+        s.glueLength = readText(() -> String.format("%.3f", s7Client.readFloat(gun.glueLengthOffset)),
+                gun.glueLengthAddr(config.db), s);
         return s;
     }
 
+    private FieldVal readText(FieldReader reader, String addr, GunSnapshot s) {
+        FieldVal fv = new FieldVal();
+        fv.addr = addr;
+        try {
+            fv.text = reader.read();
+            fv.ok = true;
+        } catch (Exception e) {
+            fv.ok = false;
+            fv.text = "失败@" + addr;
+            fv.error = e.getMessage();
+            if (s.firstFailAddr == null) {
+                s.firstFailAddr = addr;
+                s.firstFailMsg = e.getMessage();
+            }
+        }
+        return fv;
+    }
+
+    private interface FieldReader {
+        String read() throws Exception;
+    }
+
+    private static class FieldVal {
+        boolean ok;
+        String text = "-";
+        String addr;
+        String error;
+    }
+
     private static class GunSnapshot {
-        String frameCode;
-        boolean scanFeedback;
-        boolean allowEntry;
-        boolean pathDone;
-        boolean allDone;
-        int seq;
-        float glueSpeed;
-        float glueLength;
+        FieldVal frameCode = new FieldVal();
+        FieldVal scanFeedback = new FieldVal();
+        FieldVal allowEntry = new FieldVal();
+        FieldVal pathDone = new FieldVal();
+        FieldVal allDone = new FieldVal();
+        FieldVal seq = new FieldVal();
+        FieldVal glueSpeed = new FieldVal();
+        FieldVal glueLength = new FieldVal();
+        String firstFailAddr;
+        String firstFailMsg;
+
+        boolean allOk() {
+            return okCount() == 8;
+        }
+
+        int okCount() {
+            int n = 0;
+            if (frameCode.ok) n++;
+            if (scanFeedback.ok) n++;
+            if (allowEntry.ok) n++;
+            if (pathDone.ok) n++;
+            if (allDone.ok) n++;
+            if (seq.ok) n++;
+            if (glueSpeed.ok) n++;
+            if (glueLength.ok) n++;
+            return n;
+        }
+
+        String firstError() {
+            return firstFailMsg;
+        }
     }
 
     private static class GunView extends JPanel {
@@ -317,14 +360,19 @@ public class TjPlcMonitorPanel extends JPanel {
         }
 
         void apply(GunSnapshot s) {
-            frameCode.setText(s.frameCode == null || s.frameCode.isEmpty() ? "(空)" : s.frameCode);
-            scanFeedback.setText(s.scanFeedback ? "1" : "0");
-            allowEntry.setText(s.allowEntry ? "1" : "0");
-            pathDone.setText(s.pathDone ? "1" : "0");
-            allDone.setText(s.allDone ? "1" : "0");
-            seq.setText(String.valueOf(s.seq));
-            glueSpeed.setText(String.format("%.3f", s.glueSpeed));
-            glueLength.setText(String.format("%.3f", s.glueLength));
+            setField(frameCode, s.frameCode);
+            setField(scanFeedback, s.scanFeedback);
+            setField(allowEntry, s.allowEntry);
+            setField(pathDone, s.pathDone);
+            setField(allDone, s.allDone);
+            setField(seq, s.seq);
+            setField(glueSpeed, s.glueSpeed);
+            setField(glueLength, s.glueLength);
+        }
+
+        private static void setField(JLabel label, FieldVal fv) {
+            label.setText(fv.text);
+            label.setForeground(fv.ok ? Color.BLACK : Color.RED);
         }
 
         private static JLabel valueLabel() {