liuwei 1 hete
szülő
commit
d93cd0ff26
2 módosított fájl, 49 hozzáadás és 18 törlés
  1. 46 16
      src/com/mes/ui/MesClient.java
  2. 3 2
      src/resources/config/config.properties

+ 46 - 16
src/com/mes/ui/MesClient.java

@@ -84,6 +84,8 @@ public class MesClient extends JFrame {
     public static boolean mesEnabledEast = true;  // 东侧MES启用状态(G-L)
     // 中转车启用(false=隐藏中转UI并跳过中转业务;扫码逻辑不变)
     public static boolean transferEnabled = false;
+    // OPC UA 直连采集(false=不连机床、不采参、隐藏CNC监控Tab)
+    public static boolean opcuaEnabled = false;
 
 
     public static WorkJPanelOP40 workJPanelOP40A;
@@ -175,20 +177,23 @@ public class MesClient extends JFrame {
 
                     startHeartBeatTimer();
 
-                    // 异步初始化OPC UA连接,不阻塞UI和登录
-                    // OPC UA连接放到后台线程执行,避免拖慢程序启动
-                    new Thread(() -> {
-                        try {
-                            log.info("开始异步初始化OPC UA连接...");
-                            initOpcUaConnection();
-                            startCncParamUpload();  // 定时上传CNC参数到服务端
-                            startCncDisplayRefresh(); // 启动CNC数据显示刷新
-                            startCncParamRead(); // 定时采集CNC参数保存到本地
-                            log.info("OPC UA异步初始化完成");
-                        } catch (Exception e) {
-                            log.error("OPC UA异步初始化异常: {}", e.getMessage());
-                        }
-                    }, "OpcUa-Init-Thread").start();
+                    // 异步初始化OPC UA连接,不阻塞UI和登录(开关关闭时跳过)
+                    if (opcuaEnabled) {
+                        new Thread(() -> {
+                            try {
+                                log.info("开始异步初始化OPC UA连接...");
+                                initOpcUaConnection();
+                                startCncParamUpload();  // 定时上传CNC参数到服务端
+                                startCncDisplayRefresh(); // 启动CNC数据显示刷新
+                                startCncParamRead(); // 定时采集CNC参数保存到本地
+                                log.info("OPC UA异步初始化完成");
+                            } catch (Exception e) {
+                                log.error("OPC UA异步初始化异常: {}", e.getMessage());
+                            }
+                        }, "OpcUa-Init-Thread").start();
+                    } else {
+                        log.info("OPC UA直连已关闭(mes.opcua_enabled=false),跳过机床采集");
+                    }
 
 
                 }catch (Exception e){
@@ -215,8 +220,12 @@ public class MesClient extends JFrame {
             transferEnabled = Boolean.parseBoolean(pro.getProperty("mes.transfer_enabled").trim());
         }
         log.info("中转车启用: {}", transferEnabled);
+        if (pro.getProperty("mes.opcua_enabled") != null) {
+            opcuaEnabled = Boolean.parseBoolean(pro.getProperty("mes.opcua_enabled").trim());
+        }
+        log.info("OPC UA直连启用: {}", opcuaEnabled);
 
-        // 读取OPC UA配置
+        // 读取OPC UA配置(开关关闭时仍保留配置,便于恢复;不连接)
         if (pro.getProperty("opcua.user") != null) {
             opcua_user = pro.getProperty("opcua.user");
         }
@@ -259,6 +268,10 @@ public class MesClient extends JFrame {
      * 初始化OPC UA连接 - 有线直连模式,同时连接所有CNC
      */
     public static void initOpcUaConnection() {
+        if (!opcuaEnabled) {
+            log.info("OPC UA直连已关闭,跳过初始化");
+            return;
+        }
         try {
             if (opcua_urls == null || opcua_urls.length == 0) {
                 log.warn("未配置CNC设备地址,跳过OPC UA初始化");
@@ -457,6 +470,9 @@ public class MesClient extends JFrame {
      * 定时上传CNC参数到服务端
      */
     public static void startCncParamUpload() {
+        if (!opcuaEnabled) {
+            return;
+        }
         if (cncParamUploadTimer != null) {
             cncParamUploadTimer.cancel();
         }
@@ -500,6 +516,9 @@ public class MesClient extends JFrame {
      * 有线直连模式:同时采集所有CNC设备
      */
     public static void startCncParamRead() {
+        if (!opcuaEnabled) {
+            return;
+        }
         if (cncParamReadTimer != null) {
             cncParamReadTimer.cancel();
         }
@@ -693,6 +712,9 @@ public class MesClient extends JFrame {
      * @param deviceCode 设备编号,格式为 CNC-01 ~ CNC-06
      */
     public static void collectCncParam(String sn, String oprno, String deviceCode) {
+        if (!opcuaEnabled) {
+            return;
+        }
         try {
             // 解析设备编号获取CNC索引
             int cncIndex = 0;
@@ -911,6 +933,9 @@ public class MesClient extends JFrame {
      * 启动CNC数据显示定时刷新
      */
     public static void startCncDisplayRefresh() {
+        if (!opcuaEnabled) {
+            return;
+        }
         if (cncDisplayTimer != null) {
             cncDisplayTimer.cancel();
         }
@@ -1280,7 +1305,11 @@ public class MesClient extends JFrame {
         // 将子 TabbedPane 添加到主 TabbedPane
         tabbedPane.addTab("工作记录", null, workRecordTabbedPane, null);
 
-        // CNC数据监控面板
+        // CNC数据监控面板(opcuaEnabled=false 时隐藏 Tab,逻辑保留)
+        if (!opcuaEnabled) {
+            cncPanel = null;
+            cncScrollPane = null;
+        } else {
         cncPanel = new JPanel();
         cncScrollPane = new JScrollPane(cncPanel);
         cncPanel.setLayout(null);
@@ -1541,6 +1570,7 @@ public class MesClient extends JFrame {
         cncPanel.add(cncLblAlarmMsg);
 
         tabbedPane.addTab("CNC监控", null, cncScrollPane, null);
+        } // end opcuaEnabled
 
         // ========== 待提交质量码 Tab ==========
         PendingQualityPanel pendingQualityPanel = new PendingQualityPanel();

+ 3 - 2
src/resources/config/config.properties

@@ -3,9 +3,10 @@ mes.server_ip=192.168.17.170
 mes.line_sn=CZ060
 # 中转车:false=隐藏UI并跳过中转逻辑;true=恢复中转
 mes.transfer_enabled=false
+# OPC UA 直连采集:false=不连机床、不采参、隐藏CNC监控Tab;true=恢复
+mes.opcua_enabled=false
 
-# OPC UA Configuration (CNC)
-# 现场 3 台 CNC(A/B/C);其余地址保留在历史配置中,不启用
+# OPC UA Configuration (CNC) — 开关关闭时不连接,配置保留便于恢复
 opcua.user=OPCUA
 opcua.password=OPCUA123