From 8f8eed75beb5bb9b66f2a87de856f2dbf11e6ffe Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 20 十一月 2025 20:29:36 +0800
Subject: [PATCH] 修改

---
 src/chushihua/lunxun.java |  262 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 202 insertions(+), 60 deletions(-)

diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index ac7e82d..6ed53dd 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -3,15 +3,22 @@
 import chuankou.Sendmsg;
 import home.MachineConfig;
 import publicway.QueryData;
+import xitongshezhi.Fkj;
+import xitongshezhi.SystemDebugDialog;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
- * 杞鏌ヨ绫�
+ * 杞鏌ヨ绫� - 浼樺寲鐗堟湰
  * 鐢ㄤ簬瀹氭椂鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠�
  * 鏀寔鏆傚仠鍜屾仮澶嶅姛鑳斤紝妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
  */
 public class lunxun {
     private static volatile boolean isRunning = false;
     private static volatile boolean isPaused = false;
+    private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
     private static Thread pollingThread;
     private static int pollingInterval = 100; // 榛樿杞闂撮殧
     
@@ -24,8 +31,14 @@
     private static long lastSerialCheckTime = 0;
     private static boolean serialConnected = false;
     
+    // 鎬ц兘浼樺寲锛氭煡璇㈡寚浠ょ紦瀛�
+    private static final Map<Integer, String> queryCommandCache = new ConcurrentHashMap<>();
+    
+    // 璋冭瘯妯″紡鎺у埗
+    public static  boolean DEBUG_ENABLED = false;
+    
     /**
-     * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+     * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬� - 浼樺寲鐗堟湰锛屾坊鍔犻噸璇曟満鍒�
      * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
      */
     public static boolean checkSerialConnection() {
@@ -42,7 +55,9 @@
             boolean result = Sendmsg.isPortOpen();
             
             if (result) {
-                System.out.println("涓插彛杩炴帴姝e父");
+                if (DEBUG_ENABLED) {
+                    System.out.println("涓插彛杩炴帴姝e父");
+                }
                 serialConnected = true;
             } else {
                 System.err.println("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
@@ -57,7 +72,25 @@
     }
     
     /**
-     * 鍚姩杞鏌ヨ
+     * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
+     */
+    private static boolean checkSerialConnectionWithRetry() {
+        for (int i = 0; i < 3; i++) {
+            if (checkSerialConnection()) {
+                return true;
+            }
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                return false;
+            }
+        }
+        return false;
+    }
+    
+    /**
+     * 鍚姩杞鏌ヨ - 浼樺寲鐗堟湰
      * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
      */
     public static boolean startPolling() {
@@ -67,7 +100,7 @@
         }
         
         // 鍚姩鍓嶄弗鏍兼鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnection()) {
+        if (!checkSerialConnectionWithRetry()) {
             System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
             return false;
         }
@@ -77,23 +110,26 @@
         
         isRunning = true;
         isPaused = false;
+        shouldStop.set(false);
         
         try {
             pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
             pollingThread.setDaemon(true);
-            pollingThread.start();
-            
-            System.out.println("杞鏌ヨ宸插惎鍔紝闂撮殧: " + pollingInterval + "ms");
+            pollingThread.start();            
+            if (DEBUG_ENABLED) {
+                System.out.println("杞鏌ヨ宸插惎鍔紝闂撮殧: " + pollingInterval + "ms");
+            }
             return true;
         } catch (Exception e) {
             System.err.println("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
             isRunning = false;
+            shouldStop.set(true);
             return false;
         }
     }
     
     /**
-     * 鍋滄杞鏌ヨ
+     * 鍋滄杞鏌ヨ - 淇鐗堟湰
      * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
      */
     public static boolean stopPolling() {
@@ -102,19 +138,31 @@
             return false;
         }
         
+        shouldStop.set(true);
         isRunning = false;
         isPaused = false;
+        
         if (pollingThread != null) {
             pollingThread.interrupt();
             try {
-                pollingThread.join(1000); // 绛夊緟绾跨▼缁撴潫锛屾渶澶�1绉�
+                pollingThread.join(3000); // 绛夊緟3绉�
+                // 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
+                if (pollingThread.isAlive()) {
+                    System.err.println("杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
+                    // 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
+                    pollingThread.setDaemon(true);
+                }
             } catch (InterruptedException e) {
                 System.err.println("鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
                 Thread.currentThread().interrupt();
+            } catch (Exception e) {
+                System.err.println("鍋滄杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+            } finally {
+                pollingThread = null;
             }
-            pollingThread = null;
         }
         
+        shouldStop.set(false);
         System.out.println("杞鏌ヨ宸插仠姝�");
         return true;
     }
@@ -125,12 +173,16 @@
      */
     public static boolean pausePolling() {
         if (!isRunning) {
-            System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
+            if (DEBUG_ENABLED) {
+                System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
+            }
             return false;
         }
         
         if (isPaused) {
-            System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+            if (DEBUG_ENABLED) {
+                System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+            }
             return false;
         }
         
@@ -155,7 +207,7 @@
         }
         
         // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnection()) {
+        if (!checkSerialConnectionWithRetry()) {
             System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
             return false;
         }
@@ -228,7 +280,7 @@
         
         // 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
         try {
-            Thread.sleep(100);
+            Thread.sleep(200);
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
         }
@@ -256,22 +308,40 @@
     }
     
     /**
-     * 杞浠诲姟鍐呴儴绫�
+     * 鑾峰彇缂撳瓨鐨勬煡璇㈡寚浠�
+     */
+    private static String getCachedQueryCommand(int slotNumber) {
+        return queryCommandCache.computeIfAbsent(slotNumber, QueryData::queryData);
+    }
+    
+    /**
+     * 娓呯┖鏌ヨ鎸囦护缂撳瓨锛堝綋鏌ヨ閫昏緫鍙樺寲鏃惰皟鐢級
+     */
+    public static void clearQueryCache() {
+        queryCommandCache.clear();
+        System.out.println("鏌ヨ鎸囦护缂撳瓨宸叉竻绌�");
+    }
+    
+    /**
+     * 杞浠诲姟鍐呴儴绫� - 浼樺寲鐗堟湰
      * 浼樺寲鍐呭瓨浣跨敤锛氶伩鍏嶅湪寰幆涓垱寤烘柊瀵硅薄
+     * 娣诲姞鎵归噺澶勭悊鍜屾�ц兘鐩戞帶
      */
     private static class PollingTask implements Runnable {
-        private int currentSlot = MIN_SLOT;
+        private int currentIndex = 0; // 褰撳墠绱㈠紩锛岀敤浜庨亶鍘唖lotArray
+        private int consecutiveFailures = 0; // 杩炵画澶辫触娆℃暟
+        private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
         
         @Override
         public void run() {
             System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
             
-            while (isRunning && !Thread.currentThread().isInterrupted()) {
+            while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
                 try {
                     // 妫�鏌ユ槸鍚︽殏鍋�
                     if (isPaused) {
                         synchronized (lunxun.class) {
-                            while (isPaused && isRunning) {
+                            while (isPaused && isRunning && !shouldStop.get()) {
                                 lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
                             }
                         }
@@ -279,28 +349,61 @@
                     }
                     
                     // 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
-                    if (currentSlot % 10 == 0 && !checkSerialConnection()) {
+                    if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
                         System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
                         pausePolling();
                         continue;
                     }
                     
-                    // 鍙戦�佸綋鍓嶅崱妲界殑鏌ヨ鎸囦护
-                    sendQueryToSlot(currentSlot);
-                    
-                    // 绉诲姩鍒颁笅涓�涓崱妲�
-                    currentSlot++;
-                    if (currentSlot > MAX_SLOT) {
-                        currentSlot = MIN_SLOT; // 寰幆鍥炲埌绗竴涓崱妲�
-                        
-                        // 鍦ㄥ惊鐜洖鍒扮涓�涓崱妲藉悗锛岀瓑寰呴棿闅旀椂闂�
-                        // 閬垮厤杩炵画鍙戦�佷袱涓浉閭诲懆鏈熺殑绗竴涓崱妲�
+                    // 鑾峰彇鍗℃Ы鏁扮粍
+                    Fkj[] slotArray = SlotManager.getSlotArray();
+                    if (slotArray == null || slotArray.length == 0) {
+                        System.err.println("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
                         Thread.sleep(pollingInterval);
-                        continue; // 璺宠繃鏈寰幆鐨勫悗缁瓑寰�
+                        continue;
                     }
                     
-                    // 绛夊緟鎸囧畾鐨勯棿闅旀椂闂�
-                    Thread.sleep(pollingInterval);
+                    // 閬嶅巻鎵�鏈夊崱妲斤紝鍙粰 hasCard != 1 鐨勫崱妲藉彂閫佹煡璇㈡寚浠�
+                    boolean sentQuery = false;
+                    int checkedSlots = 0;
+                    int maxSlotsPerCycle = Math.min(10, slotArray.length); // 姣忓懆鏈熸渶澶氭鏌�10涓崱妲�
+                    
+                    for (int i = 0; i < maxSlotsPerCycle && checkedSlots < slotArray.length; i++) {
+                        Fkj slot = slotArray[currentIndex];
+                        if (slot != null) {
+                            String hasCard = slot.getHasCard();
+                            if (!"1".equals(hasCard)) {
+                                int slotNumber = currentIndex + 1;
+                                if (sendQueryToSlot(slotNumber)) {
+                                    sentQuery = true;
+                                    consecutiveFailures = 0;
+                                    
+                                    // 鍏抽敭淇锛氬湪break鍓嶅厛鏇存柊绱㈠紩
+                                    currentIndex = (currentIndex + 1) % slotArray.length;
+                                    checkedSlots++;
+                                    
+                                    Thread.sleep(pollingInterval);
+                                    break;
+                                } else {
+                                    consecutiveFailures++;
+                                    if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
+                                        System.err.println("杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
+                                        pausePolling();
+                                        break;
+                                    }
+                                }
+                            }
+                        }
+                        
+                        // 瀵逛簬涓嶉渶瑕佸彂閫佹煡璇㈢殑鍗℃Ы锛屾甯告洿鏂扮储寮�
+                        currentIndex = (currentIndex + 1) % slotArray.length;
+                        checkedSlots++;
+                    }
+                    
+                    // 濡傛灉娌℃湁鎵惧埌闇�瑕佹煡璇㈢殑鍗℃Ы锛岀瓑寰呬竴娈垫椂闂村啀缁х画
+                    if (!sentQuery) {
+                        Thread.sleep(pollingInterval * 2); // 娌℃湁鏌ヨ鏃剁瓑寰呮椂闂村姞鍊�
+                    }
                     
                 } catch (InterruptedException e) {
                     System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
@@ -308,6 +411,7 @@
                     break;
                 } catch (Exception e) {
                     System.err.println("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
+                    consecutiveFailures++;
                     
                     // 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
                     try {
@@ -323,13 +427,13 @@
         }
         
         /**
-         * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠�
-         * 浼樺寲锛氶伩鍏嶉噸澶嶅垱寤哄璞★紝浣跨敤灞�閮ㄥ彉閲�
+         * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
+         * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
          */
-        private void sendQueryToSlot(int slotNumber) {
+        private boolean sendQueryToSlot(int slotNumber) {
             try {
-                // 鐢熸垚鏌ヨ鎸囦护
-                String queryCommand = QueryData.queryData(slotNumber);
+                // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
+                String queryCommand = getCachedQueryCommand(slotNumber);
                 
                 if (queryCommand != null && !queryCommand.trim().isEmpty()) {
                     // 鍙戦�佸埌涓插彛
@@ -337,22 +441,28 @@
                     
                     if (sendResult) {
                         // 鍙湪璋冭瘯鏃惰緭鍑猴紝閬垮厤棰戠箒鎵撳嵃
-                        if (slotNumber == 1 || slotNumber % 20 == 0) {
-                            System.out.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber);
+                        if (DEBUG_ENABLED) {
+                            SystemDebugDialog.appendAsciiData(String.format("Slot %d Send query (hasCard !=1)", slotNumber));
                         }
+                        return true;
                     } else {
-                        System.err.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
+                        if (DEBUG_ENABLED) {
+                            SystemDebugDialog.appendAsciiData("Send query command to card slot err");
+                        }
                         // 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
                         serialConnected = false;
+                        return false;
                     }
                 } else {
                     System.err.println("鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+                    return false;
                 }
                 
             } catch (Exception e) {
                 System.err.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
                 // 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
                 serialConnected = false;
+                return false;
             }
         }
     }
@@ -369,21 +479,23 @@
         }
         
         // 妫�鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnection()) {
+        if (!checkSerialConnectionWithRetry()) {
             System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
             return false;
         }
         
         try {
-            // 鐢熸垚鏌ヨ鎸囦护
-            String queryCommand = QueryData.queryData(slotNumber);
+            // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
+            String queryCommand = getCachedQueryCommand(slotNumber);
             
             if (queryCommand != null && !queryCommand.trim().isEmpty()) {
                 // 鍙戦�佸埌涓插彛
                 boolean sendResult = Sendmsg.sendMessage(queryCommand);
                 
                 if (sendResult) {
-                    System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
+                    if (DEBUG_ENABLED) {
+                        System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
+                    }
                     return true;
                 } else {
                     System.err.println("绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
@@ -401,35 +513,48 @@
     }
     
     /**
-     * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�
+     * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�- 浼樺寲鐗堟湰
      * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
      */
     public static int sendImmediateQueryToAll() {
         // 妫�鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnection()) {
+        if (!checkSerialConnectionWithRetry()) {
             System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
             return 0;
         }
         
         int successCount = 0;
+        int batchSize = 5; // 姣忔壒娆″彂閫�5涓煡璇�
+        int totalSlots = MAX_SLOT - MIN_SLOT + 1;
         
         System.out.println("寮�濮嬫壒閲忔煡璇㈡墍鏈夊崱妲�...");
         
-        for (int slot = MIN_SLOT; slot <= MAX_SLOT; slot++) {
-            if (sendImmediateQuery(slot)) {
-                successCount++;
+        for (int batchStart = MIN_SLOT; batchStart <= MAX_SLOT; batchStart += batchSize) {
+            if (shouldStop.get()) {
+                break;
             }
             
-            // 灏忛棿闅旈伩鍏嶄覆鍙f嫢鍫�
-            try {
-                Thread.sleep(10);
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                break;
+            int batchEnd = Math.min(batchStart + batchSize - 1, MAX_SLOT);
+            
+            // 鎵规鍐呮煡璇�
+            for (int slot = batchStart; slot <= batchEnd; slot++) {
+                if (sendImmediateQuery(slot)) {
+                    successCount++;
+                }
+            }
+            
+            // 鎵规闂撮棿闅旓紝閬垮厤涓插彛鎷ュ牭
+            if (batchEnd < MAX_SLOT) {
+                try {
+                    Thread.sleep(50);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    break;
+                }
             }
         }
         
-        System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + MAX_SLOT);
+        System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + totalSlots);
         return successCount;
     }
     
@@ -467,9 +592,10 @@
         }
         
         String serialStatus = serialConnected ? "宸茶繛鎺�" : "鏈繛鎺�";
+        int cacheSize = queryCommandCache.size();
         
-        return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鍗℃Ы鑼冨洿: %d-%d", 
-                           status, serialStatus, pollingInterval, MIN_SLOT, MAX_SLOT);
+        return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鎸囦护缂撳瓨: %d, 鍗℃Ы鑼冨洿: %d-%d", 
+                           status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT);
     }
     
     /**
@@ -497,7 +623,7 @@
             // 璇锋眰鎭㈠
             if (isPaused) {
                 // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
-                if (!checkSerialConnection()) {
+                if (!checkSerialConnectionWithRetry()) {
                     System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
                     return false;
                 }
@@ -514,4 +640,20 @@
             }
         }
     }
+    
+    /**
+     * 鑾峰彇鎬ц兘缁熻淇℃伅
+     */
+    public static String getPerformanceStats() {
+        return String.format("鏌ヨ鎸囦护缂撳瓨澶у皬: %d, 杞闂撮殧: %dms", 
+                           queryCommandCache.size(), pollingInterval);
+    }
+
+	public static boolean isDEBUG_ENABLED() {
+		return DEBUG_ENABLED;
+	}
+
+	public static void setDEBUG_ENABLED(boolean dEBUG_ENABLED) {
+		DEBUG_ENABLED = dEBUG_ENABLED;
+	}
 }
\ No newline at end of file

--
Gitblit v1.9.3