liuwei 2 недель назад
Родитель
Сommit
90d3d6e746

+ 9 - 35
src/com/mes/component/DirectScanHandshake.java

@@ -31,7 +31,6 @@ public class DirectScanHandshake {
     private static final int SN_MAX_LENGTH = 24;
     private static final long RETRY_SCAN_MS = 10000;
     private static final long CLEAN_DELAY_MS = 33000;
-    private static final long OWNER_TIMEOUT_MS = 60000;
     private static final String OPRNO = "OP135";
 
     private enum State {
@@ -59,7 +58,6 @@ public class DirectScanHandshake {
     private volatile String lastScannedSn = "";
     private volatile String lastPlcWrittenSn = "";
     private volatile String lastDeliveredCnc = "";
-    private volatile long ownerRegisteredAt = 0;
 
     public DirectScanHandshake(String plcIp, TCPClient scanner) {
         this.plc = new S7PLC(EPlcType.S1500, plcIp);
@@ -101,7 +99,6 @@ public class DirectScanHandshake {
         }
         WorkJPanelCnc current = pendingCnc.get();
         if (current == panel) {
-            ownerRegisteredAt = System.currentTimeMillis();
             return true;
         }
         if (current != null) {
@@ -111,21 +108,22 @@ public class DirectScanHandshake {
         }
         if (pendingCnc.compareAndSet(current, panel)) {
             cncDelivered = false;
-            ownerRegisteredAt = System.currentTimeMillis();
             log.info("OP135 登记扫码归属工位:{}{}", panel.oprno, panel.pos);
             return true;
         }
         return false;
     }
 
-    /** 上料请求撤销且当前没有进行中的扫码时,释放尚未使用的归属。 */
-    public void releaseScanOwner(WorkJPanelCnc panel) {
-        if (state == State.IDLE || state == State.WAIT_REQUEST_RISE) {
-            if (pendingCnc.compareAndSet(panel, null)) {
-                ownerRegisteredAt = 0;
-                log.info("OP135 释放扫码归属工位:{}{}", panel.oprno, panel.pos);
-            }
+    /** 人工释放当前扫码归属,返回被释放的工位号。 */
+    public String releasePendingOwnerManually() {
+        WorkJPanelCnc owner = pendingCnc.getAndSet(null);
+        if (owner == null) {
+            return "";
         }
+        String station = owner.oprno + owner.pos;
+        showStatus("已手动释放扫码归属:" + station, 0);
+        log.info("OP135 已手动释放扫码归属工位:{}", station);
+        return station;
     }
 
     /** 只读监控数据,不参与握手判断。 */
@@ -217,7 +215,6 @@ public class DirectScanHandshake {
         }
 
         if (!request) {
-            expirePendingOwnerIfNeeded();
             handleRequestOff(previous);
             return;
         }
@@ -250,10 +247,6 @@ public class DirectScanHandshake {
         if (previous && state != State.WAIT_CLEAR && state != State.WAIT_REQUEST_RISE) {
             validatedSn = "";
             nextScanAt = 0;
-            if (!cncDelivered && pendingCnc.getAndSet(null) != null) {
-                ownerRegisteredAt = 0;
-                log.info("OP135 扫码请求结束,释放尚未交付的CNC归属");
-            }
             if (!plcOwnedSn.isEmpty()) {
                 scheduleDelayedCleanup();
             } else {
@@ -270,23 +263,6 @@ public class DirectScanHandshake {
         }
     }
 
-    private void expirePendingOwnerIfNeeded() {
-        WorkJPanelCnc owner = pendingCnc.get();
-        if (owner == null || ownerRegisteredAt == 0) {
-            return;
-        }
-        boolean waitingForFirstScanRequest = state == State.IDLE
-                || state == State.WAIT_REQUEST_RISE;
-        if (waitingForFirstScanRequest
-                && System.currentTimeMillis() - ownerRegisteredAt >= OWNER_TIMEOUT_MS) {
-            if (pendingCnc.compareAndSet(owner, null)) {
-                ownerRegisteredAt = 0;
-                showStatus("OP135 等待扫码请求超时,已释放CNC归属", -1);
-                log.warn("OP135 等待扫码请求超时,释放归属 {}{}", owner.oprno, owner.pos);
-            }
-        }
-    }
-
     private void scanAndValidate() {
         showStatus("OP135 扫码中...", 0);
         String sn = scanQrCode();
@@ -388,7 +364,6 @@ public class DirectScanHandshake {
             }
             cncDelivered = true;
             pendingCnc.compareAndSet(owner, null);
-            ownerRegisteredAt = 0;
             lastDeliveredCnc = owner.oprno + owner.pos;
             log.info("OP135 工件码 {} 已交付 {}{}", sn, owner.oprno, owner.pos);
         }
@@ -432,7 +407,6 @@ public class DirectScanHandshake {
         validatedSn = "";
         cncDelivered = false;
         pendingCnc.set(null);
-        ownerRegisteredAt = 0;
         clearAt = 0;
         state = State.WAIT_REQUEST_RISE;
         showStatus("OP135 33 秒清理完成,等待下一次请求", 0);

+ 29 - 0
src/com/mes/component/DirectScanMonitorPanel.java

@@ -24,6 +24,7 @@ public class DirectScanMonitorPanel extends JPanel {
     private JLabel subtitleLabel;
     private JLabel requestScanLight;
     private JLabel allowScanLight;
+    private JButton releaseOwnerBtn;
     private JTextField scannedSnField;
     private JTextField validatedSnField;
     private JTextField plcSnField;
@@ -72,6 +73,13 @@ public class DirectScanMonitorPanel extends JPanel {
         allowScanLight = createLight("允许扫码", WORK_WIDTH / 3, 50);
         allowScanLight.addMouseListener(toggleAllowScan());
 
+        releaseOwnerBtn = new JButton("释放绑定");
+        releaseOwnerBtn.setFont(new Font("微软雅黑", Font.PLAIN, 13));
+        releaseOwnerBtn.setBounds(WORK_WIDTH * 2 / 3, 50, WORK_WIDTH / 3, 30);
+        releaseOwnerBtn.setFocusable(false);
+        releaseOwnerBtn.addActionListener(e -> releaseOwnerManually());
+        add(releaseOwnerBtn);
+
         scannedSnField = createField("原始扫码", 90);
         validatedSnField = createField("校验通过", 130);
         plcSnField = createField("PLC最近写码", 170);
@@ -141,6 +149,27 @@ public class DirectScanMonitorPanel extends JPanel {
         };
     }
 
+    private void releaseOwnerManually() {
+        if (handshake == null) {
+            JOptionPane.showMessageDialog(null, "独立扫码枪64未配置");
+            return;
+        }
+        String owner = handshake.getPendingOwner();
+        if (owner.isEmpty()) {
+            JOptionPane.showMessageDialog(null, "当前没有绑定的CNC");
+            return;
+        }
+        if (!confirm("确定释放当前扫码绑定 " + owner + " 吗?")) {
+            return;
+        }
+        String released = handshake.releasePendingOwnerManually();
+        if (released.isEmpty()) {
+            JOptionPane.showMessageDialog(null, "释放绑定失败或绑定已变化");
+            return;
+        }
+        refresh();
+    }
+
     private JTextField createField(String labelText, int y) {
         JLabel label = new JLabel(labelText);
         label.setHorizontalAlignment(SwingConstants.CENTER);