张世豪
昨天 7be65a0428a4527889b6955c56aafdb81dda28a8
src/home/CardMachineUI.java
@@ -13,7 +13,6 @@
import xitongshezhi.AdminLoginDialog;
import xitongshezhi.CardPickupDialog;
import xitongshezhi.ConfigSet;
import xitongshezhi.Fkj;
import chuankou.SerialPortService;
import chuankou.Sendmsg;
@@ -77,17 +76,27 @@
   private JPanel commFaultPanel;
   public CardMachineUI() {
       try {
      try {
           // 先初始化系统配置和组件
           initializeSystem();
           // 初始化串口解析器
           initializeSerialParser();
           
           // 然后初始化UI
           initializeUI();
           initializeSlots();
           startUIUpdates(); // UI刷新定时器
           startSerialStatusMonitoring(); // 串口状态监控
           startSerialStatusMonitoring(); // 串口状态监控
           setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
           addWindowListener(new java.awt.event.WindowAdapter() {
               @Override
               public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                   dispose(); // 调用我们修改过的dispose方法
               }
           });
           
           System.out.println("主界面初始化完成");
           //System.out.println("主界面初始化完成");
       } catch (Exception e) {
           e.printStackTrace();
           JOptionPane.showMessageDialog(null, 
@@ -110,7 +119,8 @@
         if (serialConnected && lunxun.isPolling() && lunxun.isPaused()) {
            // 只有在不在设置页面时才自动恢复
            if (!isInConfigPage()) {
               lunxun.resumePolling();
//               lunxun.resumePolling();
//               System.out.println("尝试自动启动轮询功能");
            }
         }
      });
@@ -133,7 +143,7 @@
        try {
            // 初始化卡槽管理器 - 必须首先执行
            slotManager = new SlotManager();
            System.out.println("卡槽管理器初始化完成");
            //System.out.println("卡槽管理器初始化完成");
            // 直接使用已经初始化的配置系统
            Chushihua configSystem = Chushihua.getInstance();
@@ -156,9 +166,9 @@
                PICKUP_PASSWORD = configSystem.getMachineConfig().getFetchCardPassword();
            }
            System.out.println("系统配置初始化完成");
            System.out.println("管理员密码长度: " + (ADMIN_PASSWORD != null ? ADMIN_PASSWORD.length() : "null"));
            System.out.println("取卡密码长度: " + (PICKUP_PASSWORD != null ? PICKUP_PASSWORD.length() : "null"));
            //System.out.println("系统配置初始化完成");
            //System.out.println("管理员密码长度: " + (ADMIN_PASSWORD != null ? ADMIN_PASSWORD.length() : "null"));
            //System.out.println("取卡密码长度: " + (PICKUP_PASSWORD != null ? PICKUP_PASSWORD.length() : "null"));
        } catch (Exception e) {
            System.err.println("系统初始化异常: " + e.getMessage());
@@ -171,7 +181,7 @@
    }
   private void initializeUI() {
      setTitle("UWB人员定位卡发卡机管理系统");
      setTitle("智能人脸发卡机管理系统");
      setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
@@ -288,8 +298,9 @@
       refreshBtn.addActionListener(e -> {
           updateCardSlotsDisplay();
           updateStatistics();
           lunxun.resumePolling();
           JOptionPane.showMessageDialog(CardMachineUI.this, 
                   "手动刷新完成,共刷新 " + TOTAL_SLOTS + " 个卡槽",
                   "手动刷新完成,启动轮询,共刷新 " + TOTAL_SLOTS + " 个卡槽",
                   "刷新完成", JOptionPane.INFORMATION_MESSAGE);
       });
@@ -504,7 +515,7 @@
       cardSlotsPanel.revalidate();
       cardSlotsPanel.repaint();
       
//       System.out.println("卡槽显示更新完成: " + updatedSlots + " 个成功, " + errorSlots + " 个失败");
//       //System.out.println("卡槽显示更新完成: " + updatedSlots + " 个成功, " + errorSlots + " 个失败");
   }
   private Color brighterColor(Color color) {
@@ -585,40 +596,74 @@
       boolean pickupSuccess = CardPickupDialog.showCardPickup(this, slotId, status);
       
       if (pickupSuccess) {
           // 取卡成功,界面已经在对话框中更新,这里不需要额外操作
           System.out.println("卡槽 " + slotId + " 取卡成功");
           // 取卡成功,调用changgehaska方法改变卡槽属性
           SlotManager.changgehaska(slotId,1); // "1"表示管理员操作
           //System.out.println("卡槽 " + slotId + " 取卡成功,已更新卡槽状态");
       }
   }
   
   public void dispose() {
      // 停止UI刷新定时器
      if (uiUpdateTimer != null) {
         uiUpdateTimer.stop();
      }
      // 停止串口状态监控
      if (serialStatusTimer != null) {
         serialStatusTimer.stop();
      }
      // 停止轮询查询
      if (lunxun.isPolling()) {
         lunxun.stopPolling();
         System.out.println("应用程序关闭,轮询查询已停止");
      }
      // 停止串口协议解析器(新增)
      if (serialProtocolParser != null) {
         serialProtocolParser.stop();
      }
      // 停止串口服务
      if (serialPortService != null) {
         serialPortService.stopCapture();
         serialPortService.close();
      }
      super.dispose();
      int result = JOptionPane.showConfirmDialog(this,
             "确定要退出程序吗?",
             "确认退出",
             JOptionPane.YES_NO_OPTION);
         if (result != JOptionPane.YES_OPTION) {
             return; // 用户取消退出
         }
       // 停止UI刷新定时器
       if (uiUpdateTimer != null) {
           uiUpdateTimer.stop();
       }
       // 停止串口状态监控
       if (serialStatusTimer != null) {
           serialStatusTimer.stop();
       }
       // 停止轮询查询
       if (lunxun.isPolling()) {
           lunxun.stopPolling();
           //System.out.println("应用程序关闭,轮询查询已停止");
       }
       // 停止串口协议解析器(新增)
       if (serialProtocolParser != null) {
           serialProtocolParser.stop();
       }
       // 停止串口服务
       if (serialPortService != null) {
           serialPortService.stopCapture();
           serialPortService.close();
       }
       super.dispose();
       // 新增:确保程序完全退出前释放单实例锁
       try {
           // 通过反射调用 Homein 的清理方法
           Class<?> homeinClass = Class.forName("home.Homein");
           java.lang.reflect.Method cleanupMethod = homeinClass.getDeclaredMethod("cleanupSingleInstanceLock");
           cleanupMethod.setAccessible(true);
           cleanupMethod.invoke(null);
           //System.out.println("单实例锁已释放");
       } catch (Exception e) {
           System.err.println("释放单实例锁时发生异常: " + e.getMessage());
           // 如果反射失败,尝试直接关闭端口
           try {
               java.net.ServerSocket socket = new java.net.ServerSocket();
               socket.setReuseAddress(true);
               socket.bind(new java.net.InetSocketAddress(9999));
               socket.close();
               //System.out.println("通过直接绑定方式释放端口");
           } catch (Exception ex) {
               System.err.println("直接释放端口也失败: " + ex.getMessage());
           }
       }
       // 新增:确保程序完全退出
       System.exit(0);
   }
   
   /**
@@ -784,7 +829,7 @@
           if (lunxun.checkSerialConnection()) {
               boolean resumed = lunxun.setPollingPaused(false);
               if (resumed) {
                   System.out.println("切换到主页面,轮询查询已恢复");
                   //System.out.println("切换到主页面,轮询查询已恢复");
               }
           } else {
               System.err.println("切换到主页面,串口未连接,保持轮询暂停");
@@ -795,12 +840,15 @@
   /**
    * 启动UI刷新定时器 - 每3秒刷新一次
    */
   // 在startUIUpdates()方法中调用
   private void startUIUpdates() {
      uiUpdateTimer = new Timer(3000, e -> {
         updateCardSlotsDisplay();
         updateStatistics();
      });
      uiUpdateTimer.start();
       uiUpdateTimer = new Timer(3000, e -> {
           ensurePollingRunning(); // 确保轮询运行
           ensureSerialParserRunning(); // 确保串口解析器运行
           updateCardSlotsDisplay();
           updateStatistics();
       });
       uiUpdateTimer.start();
   }
   
   /**
@@ -828,5 +876,42 @@
   public SlotManager getSlotManager() {
       return slotManager;
   }
   /**
    * 检查并确保轮询查询正常运行
    */
   private void ensurePollingRunning() {
       if (!lunxun.isPolling() && lunxun.checkSerialConnection()) {
           // 如果轮询未运行但串口已连接,自动启动轮询
           boolean started = lunxun.startPolling();
           if (started) {
               System.out.println("检测到轮询未运行,已自动启动");
           }
       }
   }
   /**
    * 确保串口解析器正常运行
    */
   private void ensureSerialParserRunning() {
       // 如果串口解析器未运行但串口已连接,自动启动
       if (serialProtocolParser != null && !serialProtocolParser.isRunning() && lunxun.checkSerialConnection()) {
           serialProtocolParser.start();
           //System.out.println("检测到串口解析器未运行,已自动启动");
       }
   }
   /**
    * 初始化串口解析器
    */
   private void initializeSerialParser() {
       try {
           serialProtocolParser = new SerialProtocolParser();
           serialProtocolParser.start();
           //System.out.println("串口协议解析器已启动");
       } catch (Exception e) {
           System.err.println("初始化串口解析器失败: " + e.getMessage());
       }
   }
      
}