From 03b0fb0ba2de86bcfff277778826547c0e37a93f Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 20 十一月 2025 23:52:51 +0800
Subject: [PATCH] 优化修改

---
 src/chushihua/lunxunzaixian.java |  966 ++++++++++++++++++++++++++++----------------------------
 1 files changed, 490 insertions(+), 476 deletions(-)

diff --git a/src/chushihua/lunxunzaixian.java b/src/chushihua/lunxunzaixian.java
index 29bc8d6..2edfc76 100644
--- a/src/chushihua/lunxunzaixian.java
+++ b/src/chushihua/lunxunzaixian.java
@@ -13,480 +13,494 @@
  * 涓庡師鏈夌殑杞鏌ヨ绫讳簰琛ワ紝涓撴敞浜庡湪绾胯澶囩殑鎸佺画鐩戞帶
  */
 public class lunxunzaixian {
-    private static final AtomicBoolean isRunning = new AtomicBoolean(false);
-    private static final AtomicBoolean isPaused = new AtomicBoolean(false);
-    private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
-    private static Thread onlinePollingThread;
-    
-    // 鍙厤缃殑杞鍙傛暟锛堜笉鍐嶆槸final锛�
-    private static int cycleInterval = 60000; // 瀹屾暣杞鍛ㄦ湡闂撮殧锛�60绉�
-    private static int slotInterval = 200;    // 鍗℃Ы闂存煡璇㈤棿闅旓細200姣
-    
-    // 鍗℃Ы鐩稿叧甯搁噺
-    private static final int MIN_SLOT = 1;
-    private static final int MAX_SLOT = 60;
-    
-    // 鎬ц兘浼樺寲閰嶇疆
-    private static final int BATCH_SIZE = 5; // 鎵归噺鏌ヨ澶у皬
-    private static int consecutiveFailures = 0;
-    private static final int MAX_CONSECUTIVE_FAILURES = 3;
-    
-    /**
-     * 鍚姩鍦ㄧ嚎杞 - 浼樺寲鐗堟湰
-     * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
-     */
-    public static boolean startOnlinePolling() {
-        if (isRunning.get()) {
-            SystemDebugDialog.appendAsciiData("Online polling is already in progress");
-            return true;
-        }
-        
-        // 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩鍦ㄧ嚎杞");
-            return false;
-        }
-        
-        isRunning.set(true);
-        isPaused.set(false);
-        shouldStop.set(false);
-        consecutiveFailures = 0;
-        
-        try {
-            onlinePollingThread = new Thread(new OnlinePollingTask(), "Online-Polling-Thread");
-            onlinePollingThread.setDaemon(true);
-            onlinePollingThread.start();
-            //System.out.println("鍦ㄧ嚎杞宸插惎鍔紝鍛ㄦ湡闂撮殧: " + cycleInterval + "ms, 鍗℃Ы闂撮殧: " + slotInterval + "ms");
-            return true;
-        } catch (Exception e) {
-            System.err.println("鍚姩鍦ㄧ嚎杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
-            isRunning.set(false);
-            shouldStop.set(true);
-            return false;
-        }
-    }
-    
-    /**
-     * 鍋滄鍦ㄧ嚎杞 - 淇鐗堟湰
-     * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
-     */
-    public static boolean stopOnlinePolling() {
-        if (!isRunning.get()) {
-            //System.out.println("鍦ㄧ嚎杞鏈湪杩愯");
-            return false;
-        }
-        
-        shouldStop.set(true);
-        isRunning.set(false);
-        isPaused.set(false);
-        
-        if (onlinePollingThread != null) {
-            onlinePollingThread.interrupt();
-            try {
-                onlinePollingThread.join(3000); // 绛夊緟3绉�
-                // 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
-                if (onlinePollingThread.isAlive()) {
-                    System.err.println("鍦ㄧ嚎杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
-                    // 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
-                    onlinePollingThread.setDaemon(true);
-                }
-            } catch (InterruptedException e) {
-                System.err.println("鍋滄鍦ㄧ嚎杞鏃惰涓柇: " + e.getMessage());
-                Thread.currentThread().interrupt();
-            } catch (Exception e) {
-                System.err.println("鍋滄鍦ㄧ嚎杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
-            } finally {
-                onlinePollingThread = null;
-            }
-        }
-        
-        shouldStop.set(false);
-        //System.out.println("鍦ㄧ嚎杞宸插仠姝�");
-        return true;
-    }
-    
-    /**
-     * 鏆傚仠鍦ㄧ嚎杞
-     * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
-     */
-    public static boolean pauseOnlinePolling() {
-        if (!isRunning.get()) {
-            //System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曟殏鍋�");
-            return false;
-        }
-        
-        if (isPaused.get()) {
-            //System.out.println("鍦ㄧ嚎杞宸茬粡澶勪簬鏆傚仠鐘舵��");
-            return false;
-        }
-        
-        isPaused.set(true);
-        //System.out.println("鍦ㄧ嚎杞宸叉殏鍋�");
-        return true;
-    }
-    
-    /**
-     * 鎭㈠鍦ㄧ嚎杞
-     * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
-     */
-    public static boolean resumeOnlinePolling() {
-        if (!isRunning.get()) {
-            //System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曟仮澶�");
-            return false;
-        }
-        
-        if (!isPaused.get()) {
-            //System.out.println("鍦ㄧ嚎杞鏈浜庢殏鍋滅姸鎬�");
-            return false;
-        }
-        
-        // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠鍦ㄧ嚎杞");
-            return false;
-        }
-        
-        isPaused.set(false);
-        synchronized (lunxunzaixian.class) {
-            lunxunzaixian.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
-        }
-        //System.out.println("鍦ㄧ嚎杞宸叉仮澶�");
-        return true;
-    }
-    
-    /**
-     * 妫�鏌ュ湪绾胯疆璇㈢姸鎬�
-     * @return true-姝e湪杩愯, false-宸插仠姝�
-     */
-    public static boolean isOnlinePolling() {
-        return isRunning.get();
-    }
-    
-    /**
-     * 妫�鏌ユ槸鍚︽殏鍋�
-     * @return true-宸叉殏鍋�, false-鏈殏鍋�
-     */
-    public static boolean isOnlinePaused() {
-        return isPaused.get();
-    }
-    
-    /**
-     * 閲嶆柊鍚姩鍦ㄧ嚎杞
-     * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
-     */
-    public static boolean restartOnlinePolling() {
-        stopOnlinePolling();
-        
-        // 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
-        try {
-            Thread.sleep(200);
-        } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-        }
-        
-        return startOnlinePolling();
-    }
-    
-    /**
-     * 璁剧疆杞闂撮殧鍙傛暟
-     * @param cycleMs 瀹屾暣杞鍛ㄦ湡闂撮殧锛堟绉掞級
-     * @param slotMs 鍗℃Ы闂存煡璇㈤棿闅旓紙姣锛�
-     */
-    public static void setPollingIntervals(int cycleMs, int slotMs) {
-        cycleInterval = Math.max(cycleMs, 1000); // 鏈�灏�1绉�
-        slotInterval = Math.max(slotMs, 50);     // 鏈�灏�50姣
-        //System.out.println("鍦ㄧ嚎杞闂撮殧宸茶缃� - 鍛ㄦ湡闂撮殧: " + cycleInterval + "ms, 鍗℃Ы闂撮殧: " + slotInterval + "ms");
-        
-        // 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
-        if (isRunning.get()) {
-            restartOnlinePolling();
-        }
-    }
-    
-    /**
-     * 鑾峰彇褰撳墠杞闂撮殧閰嶇疆
-     * @return 闂撮殧閰嶇疆瀛楃涓�
-     */
-    public static String getPollingIntervals() {
-        return String.format("鍛ㄦ湡闂撮殧: %dms, 鍗℃Ы闂撮殧: %dms", cycleInterval, slotInterval);
-    }
-    
-    /**
-     * 鑾峰彇鍦ㄧ嚎杞缁熻淇℃伅
-     * @return 缁熻淇℃伅瀛楃涓�
-     */
-    public static String getOnlinePollingStats() {
-        int totalSlots = MAX_SLOT - MIN_SLOT + 1;
-        int cardSlots = countCardSlots();
-        
-        String status;
-        if (!isRunning.get()) {
-            status = "宸插仠姝�";
-        } else if (isPaused.get()) {
-            status = "宸叉殏鍋�";
-        } else {
-            status = "杩愯涓�";
-        }
-        
-        return String.format("鍦ㄧ嚎杞鐘舵��: %s, 鏈夊崱鍗℃Ы: %d/%d, 鍛ㄦ湡闂撮殧: %ds, 鍗℃Ы闂撮殧: %dms", 
-                           status, cardSlots, totalSlots, cycleInterval/1000, slotInterval);
-    }
-    
-    /**
-     * 缁熻鏈夊崱鐨勫崱妲芥暟閲�
-     * @return 鏈夊崱鐨勫崱妲芥暟閲�
-     */
-    private static int countCardSlots() {
-        if (SlotManager.slotArray == null) {
-            return 0;
-        }
-        
-        int count = 0;
-        for (Fkj slot : SlotManager.slotArray) {
-            if (slot != null && "1".equals(slot.getHasCard())) {
-                count++;
-            }
-        }
-        return count;
-    }
-    
-    /**
-     * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
-     */
-    private static boolean checkSerialConnectionWithRetry() {
-        for (int i = 0; i < 3; i++) {
-            if (lunxun.checkSerialConnection()) {
-                return true;
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                return false;
-            }
-        }
-        return false;
-    }
-    
-    /**
-     * 鍦ㄧ嚎杞浠诲姟鍐呴儴绫� - 浼樺寲鐗堟湰
-     * 涓撻棬杞鏈夊崱鐨勫崱妲斤紝浼樺寲璧勬簮浣跨敤
-     */
-    private static class OnlinePollingTask implements Runnable {
-        @Override
-        public void run() {
-            //System.out.println("鍦ㄧ嚎杞绾跨▼寮�濮嬭繍琛�");
-            
-            while (isRunning.get() && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
-                try {
-                    // 妫�鏌ユ槸鍚︽殏鍋�
-                    if (isPaused.get()) {
-                        synchronized (lunxunzaixian.class) {
-                            while (isPaused.get() && isRunning.get() && !shouldStop.get()) {
-                                lunxunzaixian.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
-                            }
-                        }
-                        continue;
-                    }
-                    
-                    // 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
-                    if (!checkSerialConnectionWithRetry()) {
-                        System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滃湪绾胯疆璇�");
-                        pauseOnlinePolling();
-                        continue;
-                    }
-                    
-                    // 鎵ц涓�杞湁鍗″崱妲界殑杞
-                    if (pollCardSlotsOptimized()) {
-                        consecutiveFailures = 0; // 閲嶇疆杩炵画澶辫触璁℃暟
-                    } else {
-                        consecutiveFailures++;
-                        if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
-                            System.err.println("杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滃湪绾胯疆璇�");
-                            pauseOnlinePolling();
-                        }
-                    }
-                    
-                    // 绛夊緟瀹屾暣鍛ㄦ湡闂撮殧
-                    Thread.sleep(cycleInterval);
-                    
-                } catch (InterruptedException e) {
-                    //System.out.println("鍦ㄧ嚎杞绾跨▼琚腑鏂�");
-                    Thread.currentThread().interrupt();
-                    break;
-                } catch (Exception e) {
-                    System.err.println("鍦ㄧ嚎杞杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
-                    consecutiveFailures++;
-                    
-                    // 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
-                    try {
-                        Thread.sleep(5000);
-                    } catch (InterruptedException ie) {
-                        Thread.currentThread().interrupt();
-                        break;
-                    }
-                }
-            }
-            
-            //System.out.println("鍦ㄧ嚎杞绾跨▼缁撴潫杩愯");
-        }
-        
-        /**
-         * 浼樺寲鐗堟湰锛氭壒閲忚疆璇㈡湁鍗″崱妲�
-         * @return true-杞鎴愬姛, false-杞澶辫触
-         */
-        private boolean pollCardSlotsOptimized() {
-            if (SlotManager.slotArray == null) {
-                System.err.println("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
-                return false;
-            }
-            
-            List<Integer> cardSlots = new ArrayList<>();
-            
-            // 绗竴闃舵锛氭敹闆嗘墍鏈夋湁鍗″崱妲�
-            for (int i = 0; i < SlotManager.slotArray.length; i++) {
-                if (!isRunning.get() || Thread.currentThread().isInterrupted() || shouldStop.get()) {
-                    break;
-                }
-                
-                Fkj slot = SlotManager.slotArray[i];
-                if (slot != null && "1".equals(slot.getHasCard())) {
-                    cardSlots.add(i + 1);
-                }
-            }
-            
-            if (cardSlots.isEmpty()) {
-                if (lunxun.DEBUG_ENABLED) {
-                    //System.out.println("娌℃湁鎵惧埌鏈夊崱鐨勫崱妲�");
-                }
-                return true;
-            }
-            
-            int polledCount = 0;
-            int totalCardSlots = cardSlots.size();
-            
-            // 绗簩闃舵锛氭壒閲忔煡璇㈡湁鍗″崱妲�
-            for (int i = 0; i < cardSlots.size(); i += BATCH_SIZE) {
-                if (!isRunning.get() || Thread.currentThread().isInterrupted() || shouldStop.get()) {
-                    break;
-                }
-                
-                int end = Math.min(i + BATCH_SIZE, cardSlots.size());
-                List<Integer> batch = cardSlots.subList(i, end);
-                
-                // 鎵规鍐呮煡璇�
-                for (int slotNumber : batch) {
-                    if (sendQueryToCardSlot(slotNumber)) {
-                        polledCount++;
-                    }
-                }
-                
-                // 鎵规闂撮棿闅�
-                if (end < cardSlots.size()) {
-                    try {
-                        Thread.sleep(slotInterval * BATCH_SIZE);
-                    } catch (InterruptedException e) {
-                        Thread.currentThread().interrupt();
-                        break;
-                    }
-                }
-            }
-            
-            if (polledCount > 0 && lunxun.DEBUG_ENABLED) {
-                //System.out.println("鍦ㄧ嚎杞瀹屾垚锛屾垚鍔熸煡璇� " + polledCount + "/" + totalCardSlots + " 涓湁鍗″崱妲�");
-            }
-            
-            return polledCount > 0;
-        }
-        
-        /**
-         * 鍚戞寚瀹氭湁鍗″崱妲藉彂閫佹煡璇㈡寚浠�
-         * @param slotNumber 鍗℃Ы缂栧彿
-         * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
-         */
-        private boolean sendQueryToCardSlot(int slotNumber) {
-            try {
-                // 浣跨敤鍘熸湁鐨勭珛鍗虫煡璇㈠姛鑳�
-                boolean result = lunxun.sendImmediateQuery(slotNumber);
-                
-                if (result) {
-                    // 璁板綍璋冭瘯淇℃伅锛堝噺灏戣緭鍑洪鐜囷級
-                    if (lunxun.DEBUG_ENABLED && (slotNumber == 1 || slotNumber % 10 == 0)) {
-                        //System.out.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber);
-                    }
-                    return true;
-                } else {
-                    System.err.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber + " 澶辫触");
-                    return false;
-                }
-                
-            } catch (Exception e) {
-                System.err.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
-                return false;
-            }
-        }
-    }
-    
-    /**
-     * 鎵嬪姩瑙﹀彂绔嬪嵆杞锛堜笉绛夊緟鍛ㄦ湡锛�- 浼樺寲鐗堟湰
-     * @return 鎴愬姛鏌ヨ鐨勫崱妲芥暟閲�
-     */
-    public static int triggerImmediatePolling() {
-        if (!isRunning.get() || isPaused.get()) {
-            System.err.println("鍦ㄧ嚎杞鏈繍琛屾垨宸叉殏鍋滐紝鏃犳硶绔嬪嵆杞");
-            return 0;
-        }
-        
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎵ц绔嬪嵆杞");
-            return 0;
-        }
-        
-        //System.out.println("寮�濮嬬珛鍗宠疆璇㈡湁鍗″崱妲�...");
-        
-        OnlinePollingTask task = new OnlinePollingTask();
-        
-        // 浣跨敤鏂扮殑鎵归噺杞鏂规硶
-        int cardSlotCount = countCardSlots();
-        
-        // 鍦ㄦ柊绾跨▼涓墽琛岀珛鍗宠疆璇紝閬垮厤闃诲褰撳墠绾跨▼
-        Thread immediateThread = new Thread(() -> {
-            try {
-                task.pollCardSlotsOptimized();
-            } catch (Exception e) {
-                System.err.println("绔嬪嵆杞杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
-            }
-        }, "Immediate-Online-Polling");
-        
-        immediateThread.setDaemon(true);
-        immediateThread.start();
-        
-        return cardSlotCount;
-    }
-    
-    /**
-     * 璁剧疆鍦ㄧ嚎杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
-     * @param paused true-鏆傚仠杞, false-鎭㈠杞
-     * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触
-     */
-    public static boolean setOnlinePollingPaused(boolean paused) {
-        if (!isRunning.get()) {
-            //System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
-            return false;
-        }
-        
-        if (paused) {
-            return pauseOnlinePolling();
-        } else {
-            return resumeOnlinePolling();
-        }
-    }
-    
-    /**
-     * 鑾峰彇鎬ц兘缁熻淇℃伅
-     */
-    public static String getPerformanceStats() {
-        return String.format("鎵归噺澶у皬: %d, 鍛ㄦ湡闂撮殧: %dms, 鍗℃Ы闂撮殧: %dms", 
-                           BATCH_SIZE, cycleInterval, slotInterval);
-    }
+	private static final AtomicBoolean isRunning = new AtomicBoolean(false);
+	private static final AtomicBoolean isPaused = new AtomicBoolean(false);
+	private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
+	private static Thread onlinePollingThread;
+
+	// 鍙厤缃殑杞鍙傛暟锛堜笉鍐嶆槸final锛�
+	private static int cycleInterval = 120000; // 瀹屾暣杞鍛ㄦ湡闂撮殧锛�60绉�
+	private static int slotInterval = 200;    // 鍗℃Ы闂存煡璇㈤棿闅旓細500姣
+
+	// 鍗℃Ы鐩稿叧甯搁噺
+	private static final int MIN_SLOT = 1;
+	private static final int MAX_SLOT = 60;
+
+	// 鎬ц兘浼樺寲閰嶇疆
+	private static final int BATCH_SIZE = 5; // 鎵归噺鏌ヨ澶у皬
+	private static int consecutiveFailures = 0;
+	private static final int MAX_CONSECUTIVE_FAILURES = 3;
+
+	/**
+	 * 鍚姩鍦ㄧ嚎杞 - 浼樺寲鐗堟湰
+	 * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
+	 */
+	public static boolean startOnlinePolling() {
+		if (isRunning.get()) {
+			SystemDebugDialog.appendAsciiData("Online polling is already in progress");
+			return true;
+		}
+
+		// 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+		if (!checkSerialConnectionWithRetry()) {
+			System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩鍦ㄧ嚎杞");
+			return false;
+		}
+
+		isRunning.set(true);
+		isPaused.set(false);
+		shouldStop.set(false);
+		consecutiveFailures = 0;
+
+		try {
+			onlinePollingThread = new Thread(new OnlinePollingTask(), "Online-Polling-Thread");
+			onlinePollingThread.setDaemon(true);
+			onlinePollingThread.start();
+			//System.out.println("鍦ㄧ嚎杞宸插惎鍔紝鍛ㄦ湡闂撮殧: " + cycleInterval + "ms, 鍗℃Ы闂撮殧: " + slotInterval + "ms");
+			return true;
+		} catch (Exception e) {
+			System.err.println("鍚姩鍦ㄧ嚎杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+			isRunning.set(false);
+			shouldStop.set(true);
+			return false;
+		}
+	}
+
+	/**
+	 * 鍋滄鍦ㄧ嚎杞 - 淇鐗堟湰
+	 * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
+	 */
+	public static boolean stopOnlinePolling() {
+		if (!isRunning.get()) {
+			//System.out.println("鍦ㄧ嚎杞鏈湪杩愯");
+			return false;
+		}
+
+		shouldStop.set(true);
+		isRunning.set(false);
+		isPaused.set(false);
+
+		if (onlinePollingThread != null) {
+			onlinePollingThread.interrupt();
+			try {
+				onlinePollingThread.join(3000); // 绛夊緟3绉�
+				// 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
+				if (onlinePollingThread.isAlive()) {
+					System.err.println("鍦ㄧ嚎杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
+					// 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
+					onlinePollingThread.setDaemon(true);
+				}
+			} catch (InterruptedException e) {
+				System.err.println("鍋滄鍦ㄧ嚎杞鏃惰涓柇: " + e.getMessage());
+				Thread.currentThread().interrupt();
+			} catch (Exception e) {
+				System.err.println("鍋滄鍦ㄧ嚎杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+			} finally {
+				onlinePollingThread = null;
+			}
+		}
+
+		shouldStop.set(false);
+		//System.out.println("鍦ㄧ嚎杞宸插仠姝�");
+		return true;
+	}
+
+	/**
+	 * 鏆傚仠鍦ㄧ嚎杞
+	 * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
+	 */
+	public static boolean pauseOnlinePolling() {
+		if (!isRunning.get()) {
+			//System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曟殏鍋�");
+			return false;
+		}
+
+		if (isPaused.get()) {
+			//System.out.println("鍦ㄧ嚎杞宸茬粡澶勪簬鏆傚仠鐘舵��");
+			return false;
+		}
+
+		isPaused.set(true);
+		//System.out.println("鍦ㄧ嚎杞宸叉殏鍋�");
+		return true;
+	}
+
+	/**
+	 * 鎭㈠鍦ㄧ嚎杞
+	 * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
+	 */
+	public static boolean resumeOnlinePolling() {
+		if (!isRunning.get()) {
+			//System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曟仮澶�");
+			return false;
+		}
+
+		if (!isPaused.get()) {
+			//System.out.println("鍦ㄧ嚎杞鏈浜庢殏鍋滅姸鎬�");
+			return false;
+		}
+
+		// 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
+		if (!checkSerialConnectionWithRetry()) {
+			System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠鍦ㄧ嚎杞");
+			return false;
+		}
+
+		isPaused.set(false);
+		synchronized (lunxunzaixian.class) {
+			lunxunzaixian.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
+		}
+		//System.out.println("鍦ㄧ嚎杞宸叉仮澶�");
+		return true;
+	}
+
+	/**
+	 * 妫�鏌ュ湪绾胯疆璇㈢姸鎬�
+	 * @return true-姝e湪杩愯, false-宸插仠姝�
+	 */
+	public static boolean isOnlinePolling() {
+		return isRunning.get();
+	}
+
+	/**
+	 * 妫�鏌ユ槸鍚︽殏鍋�
+	 * @return true-宸叉殏鍋�, false-鏈殏鍋�
+	 */
+	public static boolean isOnlinePaused() {
+		return isPaused.get();
+	}
+
+	/**
+	 * 閲嶆柊鍚姩鍦ㄧ嚎杞
+	 * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
+	 */
+	public static boolean restartOnlinePolling() {
+		stopOnlinePolling();
+
+		// 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
+		try {
+			Thread.sleep(200);
+		} catch (InterruptedException e) {
+			Thread.currentThread().interrupt();
+		}
+
+		return startOnlinePolling();
+	}
+
+	/**
+	 * 璁剧疆杞闂撮殧鍙傛暟
+	 * @param cycleMs 瀹屾暣杞鍛ㄦ湡闂撮殧锛堟绉掞級
+	 * @param slotMs 鍗℃Ы闂存煡璇㈤棿闅旓紙姣锛�
+	 */
+	public static void setPollingIntervals(int cycleMs, int slotMs) {
+		cycleInterval = Math.max(cycleMs, 1000); // 鏈�灏�1绉�
+		slotInterval = Math.max(slotMs, 50);     // 鏈�灏�50姣
+		//System.out.println("鍦ㄧ嚎杞闂撮殧宸茶缃� - 鍛ㄦ湡闂撮殧: " + cycleInterval + "ms, 鍗℃Ы闂撮殧: " + slotInterval + "ms");
+
+		// 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
+		if (isRunning.get()) {
+			restartOnlinePolling();
+		}
+	}
+
+	/**
+	 * 鑾峰彇褰撳墠杞闂撮殧閰嶇疆
+	 * @return 闂撮殧閰嶇疆瀛楃涓�
+	 */
+	public static String getPollingIntervals() {
+		return String.format("鍛ㄦ湡闂撮殧: %dms, 鍗℃Ы闂撮殧: %dms", cycleInterval, slotInterval);
+	}
+
+	/**
+	 * 鑾峰彇鍦ㄧ嚎杞缁熻淇℃伅
+	 * @return 缁熻淇℃伅瀛楃涓�
+	 */
+	public static String getOnlinePollingStats() {
+		int totalSlots = MAX_SLOT - MIN_SLOT + 1;
+		int cardSlots = countCardSlots();
+
+		String status;
+		if (!isRunning.get()) {
+			status = "宸插仠姝�";
+		} else if (isPaused.get()) {
+			status = "宸叉殏鍋�";
+		} else {
+			status = "杩愯涓�";
+		}
+
+		return String.format("鍦ㄧ嚎杞鐘舵��: %s, 鏈夊崱鍗℃Ы: %d/%d, 鍛ㄦ湡闂撮殧: %ds, 鍗℃Ы闂撮殧: %dms", 
+				status, cardSlots, totalSlots, cycleInterval/1000, slotInterval);
+	}
+
+	/**
+	 * 缁熻鏈夊崱鐨勫崱妲芥暟閲�
+	 * @return 鏈夊崱鐨勫崱妲芥暟閲�
+	 */
+	private static int countCardSlots() {
+		if (SlotManager.slotArray == null) {
+			return 0;
+		}
+
+		int count = 0;
+		for (Fkj slot : SlotManager.slotArray) {
+			if (slot != null && "1".equals(slot.getHasCard())) {
+				count++;
+			}
+		}
+		return count;
+	}
+
+	/**
+	 * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
+	 */
+	private static boolean checkSerialConnectionWithRetry() {
+		for (int i = 0; i < 3; i++) {
+			if (lunxun.checkSerialConnection()) {
+				return true;
+			}
+			try {
+				Thread.sleep(1000);
+			} catch (InterruptedException e) {
+				Thread.currentThread().interrupt();
+				return false;
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * 鍦ㄧ嚎杞浠诲姟鍐呴儴绫� - 浼樺寲鐗堟湰
+	 * 涓撻棬杞鏈夊崱鐨勫崱妲斤紝浼樺寲璧勬簮浣跨敤
+	 */
+	private static class OnlinePollingTask implements Runnable {
+		@Override
+		public void run() {
+			//System.out.println("鍦ㄧ嚎杞绾跨▼寮�濮嬭繍琛�");
+
+			while (isRunning.get() && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
+				try {
+					// 妫�鏌ユ槸鍚︽殏鍋�
+					if (isPaused.get()) {
+						synchronized (lunxunzaixian.class) {
+							while (isPaused.get() && isRunning.get() && !shouldStop.get()) {
+								lunxunzaixian.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
+							}
+						}
+						continue;
+					}
+
+					// 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+					if (!checkSerialConnectionWithRetry()) {
+						System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滃湪绾胯疆璇�");
+						pauseOnlinePolling();
+						continue;
+					}
+
+					// 鎵ц涓�杞湁鍗″崱妲界殑杞
+					if (pollCardSlotsOptimized()) {
+						consecutiveFailures = 0; // 閲嶇疆杩炵画澶辫触璁℃暟
+					} else {
+						consecutiveFailures++;
+						if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
+							System.err.println("lunxunzaixian杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滃湪绾胯疆璇�");
+							pauseOnlinePolling();
+						}
+					}
+
+					// 绛夊緟瀹屾暣鍛ㄦ湡闂撮殧
+					Thread.sleep(cycleInterval);
+
+				} catch (InterruptedException e) {
+					//System.out.println("鍦ㄧ嚎杞绾跨▼琚腑鏂�");
+					Thread.currentThread().interrupt();
+					break;
+				} catch (Exception e) {
+					System.err.println("鍦ㄧ嚎杞杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
+					consecutiveFailures++;
+
+					// 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
+					try {
+						Thread.sleep(5000);
+					} catch (InterruptedException ie) {
+						Thread.currentThread().interrupt();
+						break;
+					}
+				}
+			}
+
+			//System.out.println("鍦ㄧ嚎杞绾跨▼缁撴潫杩愯");
+		}
+
+		/**
+		 * 浼樺寲鐗堟湰锛氭壒閲忚疆璇㈡湁鍗″崱妲�
+		 * @return true-杞鎴愬姛, false-杞澶辫触
+		 */
+		private boolean pollCardSlotsOptimized() {
+		    if (SlotManager.slotArray == null) {
+		        System.err.println("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
+		        return false;
+		    }
+		    
+		    List<Integer> cardSlots = new ArrayList<>();
+		    
+		    // 绗竴闃舵锛氭敹闆嗘墍鏈夋湁鍗″崱妲�
+		    for (int i = 0; i < SlotManager.slotArray.length; i++) {
+		        if (!isRunning.get() || Thread.currentThread().isInterrupted() || shouldStop.get()) {
+		            break;
+		        }
+		        
+		        Fkj slot = SlotManager.slotArray[i];
+		        if (slot != null && "1".equals(slot.getHasCard())) {
+		            cardSlots.add(i + 1);
+		        }
+		    }
+		    
+		    if (cardSlots.isEmpty()) {
+		        if (lunxun.DEBUG_ENABLED) {
+		            //System.out.println("娌℃湁鎵惧埌鏈夊崱鐨勫崱妲�");
+		        }
+		        return true;
+		    }
+		    
+		    int polledCount = 0;
+		    int totalCardSlots = cardSlots.size();
+		    
+		    // 绗簩闃舵锛氭壒閲忔煡璇㈡湁鍗″崱妲� - 淇锛氭瘡涓崱妲介棿閮芥湁闂撮殧
+		    for (int i = 0; i < cardSlots.size(); i += BATCH_SIZE) {
+		        if (!isRunning.get() || Thread.currentThread().isInterrupted() || shouldStop.get()) {
+		            break;
+		        }
+		        
+		        int end = Math.min(i + BATCH_SIZE, cardSlots.size());
+		        List<Integer> batch = cardSlots.subList(i, end);
+		        
+		        // 鎵规鍐呮煡璇� - 淇锛氭瘡涓崱妲介棿娣诲姞闂撮殧
+		        for (int j = 0; j < batch.size(); j++) {
+		            int slotNumber = batch.get(j);
+		            
+		            if (sendQueryToCardSlot(slotNumber)) {
+		                polledCount++;
+		            }
+		            
+		            // 閲嶈淇锛氭瘡涓崱妲芥煡璇㈠悗绛夊緟鎸囧畾闂撮殧锛堟渶鍚庝竴涓崱妲介櫎澶栵級
+		            if (j < batch.size() - 1) {
+		                try {
+		                    Thread.sleep(slotInterval); // 鍗℃Ы闂撮棿闅�
+		                } catch (InterruptedException e) {
+		                    Thread.currentThread().interrupt();
+		                    break;
+		                }
+		            }
+		        }
+		        
+		        // 鎵规闂撮棿闅旓紙淇濇寔鍘熸湁閫昏緫锛�
+		        if (end < cardSlots.size()) {
+		            try {
+		                Thread.sleep(slotInterval * BATCH_SIZE);
+		            } catch (InterruptedException e) {
+		                Thread.currentThread().interrupt();
+		                break;
+		            }
+		        }
+		    }
+		    
+		    if (polledCount > 0 && lunxun.DEBUG_ENABLED) {
+		        System.out.println("鍦ㄧ嚎杞瀹屾垚锛屾垚鍔熸煡璇� " + polledCount + "/" + totalCardSlots + " 涓湁鍗″崱妲�");
+		    }
+		    
+		    return polledCount > 0;
+		}
+
+
+
+		/**
+		 * 鍚戞寚瀹氭湁鍗″崱妲藉彂閫佹煡璇㈡寚浠�
+		 * @param slotNumber 鍗℃Ы缂栧彿
+		 * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
+		 */
+		private boolean sendQueryToCardSlot(int slotNumber) {
+			try {
+				// 浣跨敤鍘熸湁鐨勭珛鍗虫煡璇㈠姛鑳�
+				boolean result = lunxun.sendImmediateQuery(slotNumber);
+
+				if (result) {
+					// 璁板綍璋冭瘯淇℃伅锛堝噺灏戣緭鍑洪鐜囷級
+					if (lunxun.DEBUG_ENABLED && (slotNumber == 1 || slotNumber % 10 == 0)) {
+						//System.out.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber);
+					}
+					return true;
+				} else {
+					System.err.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber + " 澶辫触");
+					return false;
+				}
+
+			} catch (Exception e) {
+				System.err.println("鍦ㄧ嚎杞 - 鏌ヨ鏈夊崱鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+				return false;
+			}
+		}
+	}
+
+	/**
+	 * 鎵嬪姩瑙﹀彂绔嬪嵆杞锛堜笉绛夊緟鍛ㄦ湡锛�- 浼樺寲鐗堟湰
+	 * @return 鎴愬姛鏌ヨ鐨勫崱妲芥暟閲�
+	 */
+	public static int triggerImmediatePolling() {
+		if (!isRunning.get() || isPaused.get()) {
+			System.err.println("鍦ㄧ嚎杞鏈繍琛屾垨宸叉殏鍋滐紝鏃犳硶绔嬪嵆杞");
+			return 0;
+		}
+
+		if (!checkSerialConnectionWithRetry()) {
+			System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎵ц绔嬪嵆杞");
+			return 0;
+		}
+
+		//System.out.println("寮�濮嬬珛鍗宠疆璇㈡湁鍗″崱妲�...");
+
+		OnlinePollingTask task = new OnlinePollingTask();
+
+		// 浣跨敤鏂扮殑鎵归噺杞鏂规硶
+		int cardSlotCount = countCardSlots();
+
+		// 鍦ㄦ柊绾跨▼涓墽琛岀珛鍗宠疆璇紝閬垮厤闃诲褰撳墠绾跨▼
+		Thread immediateThread = new Thread(() -> {
+			try {
+				task.pollCardSlotsOptimized();
+			} catch (Exception e) {
+				System.err.println("绔嬪嵆杞杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
+			}
+		}, "Immediate-Online-Polling");
+
+		immediateThread.setDaemon(true);
+		immediateThread.start();
+
+		return cardSlotCount;
+	}
+
+	/**
+	 * 璁剧疆鍦ㄧ嚎杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
+	 * @param paused true-鏆傚仠杞, false-鎭㈠杞
+	 * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触
+	 */
+	public static boolean setOnlinePollingPaused(boolean paused) {
+		if (!isRunning.get()) {
+			//System.out.println("鍦ㄧ嚎杞鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
+			return false;
+		}
+
+		if (paused) {
+			return pauseOnlinePolling();
+		} else {
+			return resumeOnlinePolling();
+		}
+	}
+
+	/**
+	 * 鑾峰彇鎬ц兘缁熻淇℃伅
+	 */
+	public static String getPerformanceStats() {
+		return String.format("鎵归噺澶у皬: %d, 鍛ㄦ湡闂撮殧: %dms, 鍗℃Ы闂撮殧: %dms", 
+				BATCH_SIZE, cycleInterval, slotInterval);
+	}
 }
\ No newline at end of file

--
Gitblit v1.9.3