From a6077217e25f5804027194a5c2848e773eda1abd Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 15:47:17 +0800
Subject: [PATCH] 修改

---
 src/chuankou/Sendmsg.java |  185 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 150 insertions(+), 35 deletions(-)

diff --git a/src/chuankou/Sendmsg.java b/src/chuankou/Sendmsg.java
index d97f1d8..0b51605 100644
--- a/src/chuankou/Sendmsg.java
+++ b/src/chuankou/Sendmsg.java
@@ -2,6 +2,15 @@
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
+import javax.swing.SwingWorker;
+
+import chushihua.lunxun;
+import dialog.Charulog;
+import dialog.Dingshidialog;
+import dialog.Errlog;
+import publicway.OpenDoor;
+import xitongshezhi.SystemDebugDialog;
+
 /**
  * 涓插彛娑堟伅鍙戦�佸伐鍏风被
  * 鎻愪緵楂樻�ц兘鐨勪覆鍙f秷鎭彂閫佸姛鑳斤紝閫傚悎楂橀璋冪敤
@@ -11,11 +20,83 @@
     private static volatile SerialPortService serialService = null;
     private static volatile boolean isPortOpen = false;
     
-    // 璋冭瘯妯″紡寮�鍏�
-    private static final boolean DEBUG_MODE = false;
+    // 鏀逛负闈瀎inal锛屾敮鎸佸姩鎬佹帶鍒�
+    private static boolean DEBUG_MODE = false;
     
     // 鏃ユ湡鏍煎紡鍖�
     private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
+   
+    
+    /**鍙戝崱鏈嶅姟鍣ㄦ帶鍒舵墦寮�鏌愪釜鏌滈棬璋冪敤鎸囦护
+     * @param int slotId鏌滈棬缂栧彿1-60
+     * @param int type 1鏄湇鍔″櫒鍙戝崱锛�2鏄鐞嗗憳鍙戝崱*/
+    public static boolean opendoorzhiling(int slotId,int type) {
+    	lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
+    	// 璋冪敤OpenDoor鐢熸垚寮�闂ㄦ寚浠�
+    	String command = OpenDoor.openOneDoor(slotId, type);
+    	boolean sendResult = Sendmsg.sendMessage(command);
+    	String mes=command+";type"+type+"鎺у埗鎵撳紑"+slotId+"鏌滈棬";
+    	Charulog.logOperation(mes);
+    	if (lunxun.DEBUG_ENABLED) {
+            SystemDebugDialog.appendAsciiData(mes);
+        }
+    	
+    	lunxun.setSendChaxunzhiling(true);//寮�濮嬫煡璇㈡寚浠�
+		return sendResult;
+    }
+    
+    /**
+     * 鎵撳紑鍏ㄩ儴鍗℃Ы鐨勫叕鐢ㄩ潤鎬佹柟娉�
+     * @param type 鎿嶄綔绫诲瀷锛�1-鏈嶅姟鍣ㄥ彂鍗★紝2-绠$悊鍛樺彂鍗�
+     */
+    public static void openAllSlots(int type) {
+    	lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
+        // 浣跨敤SwingWorker鍦ㄥ悗鍙版墽琛岋紝閬垮厤闃诲UI
+        SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
+            @Override
+            protected Void doInBackground() throws Exception {
+                // 閬嶅巻鎵�鏈夊崱妲斤紙1-60锛�
+                for (int slotId = 1; slotId <= 60; slotId++) {
+                    try {
+                        // 鐢熸垚寮�闂ㄦ寚浠�
+                        String command = OpenDoor.openOneDoor(slotId, type);
+                        
+                        // 鍙戦�佷覆鍙f寚浠�
+                        boolean sent = Sendmsg.sendMessage(command);
+                        
+                        if (!sent) {
+                            Errlog.logOperation("鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotId + " 澶辫触");
+                        }
+                        
+                        // 闂撮殧100ms
+                        Thread.sleep(100);
+                        
+                    } catch (Exception e) {
+                        Errlog.logOperation("澶勭悊鍗℃Ы " + slotId + " 鏃跺彂鐢熼敊璇�: " + e.getMessage());
+                        e.printStackTrace();
+                    }
+                }
+                return null;
+            }
+            
+            @Override
+            protected void done() {
+                // 鍙�夛細瀹屾垚鍚庡彲浠ユ坊鍔犲洖璋冨鐞�
+                System.out.println("鍏ㄩ儴鍗℃Ы寮�闂ㄦ寚浠ゅ彂閫佸畬鎴�");
+                String types="绠$悊鍛�";
+                if(type==1) {
+                	types="鏈嶅姟鍣ㄦ寚浠�";
+                }
+                String message=types+"宸插皢鍏ㄩ儴鍗℃Ы宸茬粡鎵撳紑璇峰彇鍗�";
+                Dingshidialog.showTimedDialog(null,5,message);
+                Charulog.logOperation(message);
+            }
+        };
+        
+        worker.execute();
+        lunxun.setSendChaxunzhiling(true);//寮�濮嬫煡璇㈡寚浠�
+    }
+    
     
     /**
      * 璁剧疆涓插彛鏈嶅姟瀹炰緥
@@ -35,48 +116,65 @@
     }
     
     /**
-     * 鍙戦�佹秷鎭埌涓插彛
-     * @param message 瑕佸彂閫佺殑HEX鏍煎紡娑堟伅
-     * @return 鍙戦�佹垚鍔熻繑鍥瀟rue锛屽け璐ヨ繑鍥瀎alse
+     * 鍙戦�佹秷鎭埌涓插彛锛堝甫閲嶈瘯鏈哄埗锛�
      */
     public static boolean sendMessage(String message) {
         if (!isPortOpen || serialService == null) {
-            if (DEBUG_MODE) {
-                System.err.println("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
-            }
+            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
             return false;
         }
         
         if (message == null || message.trim().isEmpty()) {
-            if (DEBUG_MODE) {
-                System.err.println("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
-            }
+            Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
             return false;
         }
         
         String text = message.trim();
+        int retryCount = 0;
+        final int MAX_RETRY = 2;
         
-        try {
-            // HEX鏍煎紡鍙戦��
-            byte[] data = hexStringToByteArray(text);
-            if (data != null && serialService.send(data)) {
-                // 楂橀璋冪敤鏃堕伩鍏嶉绻佺殑鏃ュ織杈撳嚭锛屽彧鍦ㄨ皟璇曟椂璁板綍
-                if (DEBUG_MODE) {
-                    System.out.println("[" + getCurrentTime() + "] 鍙戦��: " + text.toUpperCase());
+        while (retryCount <= MAX_RETRY) {
+            try {
+                byte[] data = hexStringToByteArray(text);
+                if (data == null) {
+                    Errlog.logOperation("[" + getCurrentTime() + "] HEX杞崲澶辫触锛屾暟鎹�: " + text);
+                    return false;
                 }
-                return true;
-            } else {
-                if (DEBUG_MODE) {
-                    System.err.println("[" + getCurrentTime() + "] 鏁版嵁鍙戦�佸け璐�");
+                
+                boolean sendResult = serialService.send(data);
+                
+                if (sendResult) {
+                    if (DEBUG_MODE) {
+                        System.out.println("[" + getCurrentTime() + "] 鍙戦�佹垚鍔�: " + text.toUpperCase());
+                    }
+                    return true;
+                } else {
+                    retryCount++;
+                    if (retryCount <= MAX_RETRY) {
+                        Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸け璐ワ紝姝e湪閲嶈瘯 (" + retryCount + "/" + MAX_RETRY + ")");
+                        try {
+                            Thread.sleep(50); // 閲嶈瘯鍓嶇瓑寰�
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                            break;
+                        }
+                    } else {
+                        Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鍙戦�佸け璐ワ紝鎸囦护: " + text.toUpperCase());
+                        Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�� - 鎵撳紑: " + isPortOpen + ", 鏈嶅姟: " + (serialService != null));
+                        if (serialService != null) {
+                            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈嶅姟鐘舵�� - 鏄惁鎵撳紑: " + serialService.isOpen());
+                        }
+                        return false;
+                    }
                 }
+            } catch (Exception e) {
+                Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸紓甯革紝鎸囦护: " + text.toUpperCase());
+                e.printStackTrace();
                 return false;
             }
-        } catch (Exception e) {
-            if (DEBUG_MODE) {
-                System.err.println("[" + getCurrentTime() + "] 鏁版嵁鏍煎紡閿欒: " + e.getMessage());
-            }
-            return false;
         }
+        
+        return false;
     }
     
     /**
@@ -84,7 +182,11 @@
      * @return 涓插彛鎵撳紑鐘舵��
      */
     public static boolean isPortOpen() {
-        return isPortOpen && serialService != null;
+        boolean open = isPortOpen && serialService != null;
+        if (!open && DEBUG_MODE) {
+            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�佹鏌�: 鏈墦寮�");
+        }
+        return open;
     }
     
     /**
@@ -100,7 +202,7 @@
         s = s.replaceAll("\\s+", ""); // 绉婚櫎绌烘牸
         int len = s.length();
         if (len % 2 != 0) {
-            throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟");
+            throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟锛屽綋鍓嶉暱搴�: " + len + ", 鏁版嵁: " + s);
         }
         
         byte[] data = new byte[len / 2];
@@ -109,7 +211,7 @@
             int low = Character.digit(s.charAt(i + 1), 16);
             
             if (high == -1 || low == -1) {
-                throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: " + s.charAt(i) + s.charAt(i + 1));
+                throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: '" + s.charAt(i) + s.charAt(i + 1) + "' 鍦ㄤ綅缃� " + i + "-" + (i+1) + ", 瀹屾暣鏁版嵁: " + s);
             }
             
             data[i / 2] = (byte) ((high << 4) + low);
@@ -144,12 +246,25 @@
     
     /**
      * 鍚敤璋冭瘯妯″紡
-     * 娉ㄦ剰锛氳繖浼氫慨鏀归潤鎬乫inal鍙橀噺锛屽疄闄呴」鐩腑涓嶅缓璁繖鏍峰仛
-     * 杩欓噷鍙槸婕旂ず锛屽疄闄呭簲璇ラ�氳繃閰嶇疆鏂囦欢鎺у埗
      */
     public static void enableDebugMode() {
-        // 鍦ㄥ疄闄呴」鐩腑锛屽簲璇ヤ娇鐢ㄥ彲閰嶇疆鐨勬柟寮忚�屼笉鏄慨鏀筬inal鍙橀噺
-        // 杩欓噷鍙槸绀烘剰锛屽疄闄呬娇鐢ㄦ椂闇�瑕侀噸鏂拌璁�
-        System.out.println("璋冭瘯妯″紡宸插惎鐢�");
+        DEBUG_MODE = true;
+        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸插惎鐢�");
+    }
+    
+    /**
+     * 绂佺敤璋冭瘯妯″紡
+     */
+    public static void disableDebugMode() {
+        DEBUG_MODE = false;
+        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸茬鐢�");
+    }
+    
+    /**
+     * 璁剧疆璋冭瘯妯″紡
+     */
+    public static void setDebugMode(boolean debug) {
+        DEBUG_MODE = debug;
+        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡: " + (debug ? "鍚敤" : "绂佺敤"));
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3