From 2b756769ea4adad21332d8a294871712cd42cc3f Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 26 十一月 2025 13:57:24 +0800
Subject: [PATCH] 优化了轮询逻辑注意有打印输出

---
 src/chushihua/lunxun.java |  318 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 168 insertions(+), 150 deletions(-)

diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index b1dc1b4..c3ffeb7 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -5,6 +5,7 @@
 import home.Fkj;
 import home.MachineConfig;
 import publicway.QueryData;
+import publicway.TimestampUtil;
 import xitongshezhi.SystemDebugDialog;
 
 import java.util.Iterator;
@@ -24,8 +25,10 @@
 	private static volatile boolean isPaused = false;
 	private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
 	private static Thread pollingThread;
-	private static int pollingInterval = 100; // 榛樿杞闂撮殧
+	private static int pollingInterval =50; // 榛樿杞闂撮殧
 	public static boolean sendChaxunzhiling=true;//鏄惁鍚戜覆鍙e彂閫佹煡璇㈡寚浠�
+	// 娣诲姞闈欐�佸彉閲忔帶鍒朵汉鑴告娴�
+	public static boolean ishaveface = false; // 鏄惁妫�娴嬪埌浜鸿劯锛岄粯璁ゆ病鏈変汉鑴�
 
 	// 鍗℃Ы鐩稿叧甯搁噺
 	private static final int MIN_SLOT = 1;
@@ -368,177 +371,192 @@
 	 * 浼樺寲锛氬唴瀛樼鐞嗗拰閲嶇敤瀵硅薄
 	 */
 	private static class PollingTask implements Runnable {
-		private int currentIndex = 0; // 褰撳墠绱㈠紩锛岀敤浜庨亶鍘唖lotArray
-		private int consecutiveFailures = 0; // 杩炵画澶辫触娆℃暟
-		private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
-		private final StringBuilder debugBuilder = new StringBuilder(100); // 閲嶇敤 StringBuilder
+	    private int currentIndex = 0; // 褰撳墠绱㈠紩锛岀敤浜庨亶鍘唖lotArray
+	    private int consecutiveFailures = 0; // 杩炵画澶辫触娆℃暟
+	    private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
+	    private final StringBuilder debugBuilder = new StringBuilder(100); // 閲嶇敤 StringBuilder
 
-		@Override
-		public void run() {
-			//System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
+	    @Override
+	    public void run() {
+	        //System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
 
-			while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
-				try {
+	        while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
+	            try {
+	                // 妫�鏌ユ槸鍚︽殏鍋�
+	                if (isPaused) {
+	                    synchronized (lunxun.class) {
+	                        while (isPaused && isRunning && !shouldStop.get()) {
+	                            lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
+	                        }
+	                    }
+	                    continue;
+	                }
 
-//					System.out.println("鏌ヨ涓�.....绾跨▼");
-					// 妫�鏌ユ槸鍚︽殏鍋�
-					if (isPaused) {
-						synchronized (lunxun.class) {
-							while (isPaused && isRunning && !shouldStop.get()) {
-								lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
-							}
-						}
-						continue;
-					}
+	                // 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
+	                if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
+	                    logErrorWithRateLimit("serial_disconnected", "涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
+	                    pausePolling();
+	                    continue;
+	                }
 
-					// 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
-					if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
-						logErrorWithRateLimit("serial_disconnected", "涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
-						pausePolling();
-						continue;
-					}
+	                // 瀹氭湡娓呯悊缂撳瓨锛堟瘡100娆″惊鐜竻鐞嗕竴娆★級
+	                if (currentIndex % 100 == 0) {
+	                    cleanupOldCache();
+	                }
 
-					// 瀹氭湡娓呯悊缂撳瓨锛堟瘡100娆″惊鐜竻鐞嗕竴娆★級
-					if (currentIndex % 100 == 0) {
-						cleanupOldCache();
-					}
+	                // 鑾峰彇鍗℃Ы鏁扮粍
+	                Fkj[] slotArray = SlotManager.getSlotArray();
+	                if (slotArray == null || slotArray.length == 0) {
+	                    logErrorWithRateLimit("slot_array_not_initialized", "鍗℃Ы鏁扮粍鏈垵濮嬪寲");
+	                    Thread.sleep(pollingInterval);
+	                    continue;
+	                }
 
-					// 鑾峰彇鍗℃Ы鏁扮粍
-					Fkj[] slotArray = SlotManager.getSlotArray();
-					if (slotArray == null || slotArray.length == 0) {
-						logErrorWithRateLimit("slot_array_not_initialized", "鍗℃Ы鏁扮粍鏈垵濮嬪寲");
-						Thread.sleep(pollingInterval);
-						continue;
-					}
+	                // 閬嶅巻鎵�鏈夊崱妲�
+	                for (int i = 0; i < slotArray.length; i++) {
+	                    if (!isRunning || shouldStop.get()) {
+	                        break;
+	                    }
+	                    
+	                    int slotIndex = (currentIndex + i) % slotArray.length;
+	                    Fkj slot = slotArray[slotIndex];
+	                    if (slot != null) {
+	                        String hasCard = slot.getHasCard();
+	                        int slotNumber = slotIndex + 1;
 
-					// 鏂板锛氭牴鎹崱妲界姸鎬佸拰鏌ヨ棰戠巼鍐冲畾鏄惁鍙戦�佹煡璇�
-					boolean sentQuery = false;
-					long currentTime = System.currentTimeMillis();
+	                        // 妫�鏌ユ槸鍚﹀洜涓轰汉鑴告娴嬮渶瑕佽烦杩囨湁鍗″崱妲�
+	                        if (ishaveface && "1".equals(hasCard)) {
+	                            // 璺宠繃鏈夊崱鍗℃Ы锛岀珛鍗崇户缁笅涓�涓紝涓嶇瓑寰�
+	                            if (DEBUG_ENABLED) {
+	                                debugBuilder.setLength(0);
+	                                debugBuilder.append("妫�娴嬪埌浜鸿劯锛岃烦杩囨湁鍗″崱妲� ").append(slotNumber).append(" 鐨勬煡璇n");
+	                                SystemDebugDialog.appendAsciiData(debugBuilder.toString());
+	                            }
+	                            continue; // 绔嬪嵆缁х画涓嬩竴涓崱妲斤紝涓嶇瓑寰�
+	                        }
 
-					// 閬嶅巻鍗℃Ы锛屽鎵鹃渶瑕佹煡璇㈢殑鍗℃Ы
-					for (int i = 0; i < slotArray.length && !sentQuery; i++) {
-						int slotIndex = (currentIndex + i) % slotArray.length;
-						Fkj slot = slotArray[slotIndex];
-						if (slot != null) {
-							String hasCard = slot.getHasCard();
-							int slotNumber = slotIndex + 1;
-							Long lastQueryTime = lastQueryTimeMap.get(slotNumber);
+	                        // 鍙戦�佹煡璇㈡寚浠ゅ埌褰撳墠鍗℃Ы
+	                        boolean sendSuccess = sendQueryToSlot(slotNumber);
+	                        
+	                        if (sendSuccess) {
+	                            // 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+	                            lastQueryTimeMap.put(slotNumber, System.currentTimeMillis());
+	                            consecutiveFailures = 0;
 
-							// 纭畾鏌ヨ闂撮殧锛氬彧鏈塰asCard="1"鐨勫崱妲戒娇鐢�10绉掗棿闅旓紝鍏朵粬鎯呭喌锛堝寘鎷�"-1"锛夐兘浣跨敤100ms闂撮殧
-							int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
+	                            if (DEBUG_ENABLED) {
+	                                String status;
+	                                if ("1".equals(hasCard)) {
+	                                    status = "鏈夊崱";
+	                                } else if ("-1".equals(hasCard)) {
+	                                    status = "鏈煡";
+	                                } else {
+	                                    status = "鏃犲崱";
+	                                }
+	                                
+	                                debugBuilder.setLength(0);
+	                                debugBuilder.append("Slot ").append(slotNumber)
+	                                           .append(" (").append(status).append(") 鏌ヨ鎴愬姛\n");
+	                                SystemDebugDialog.appendAsciiData(debugBuilder.toString());
+	                            }
+	                        } else {
+	                            consecutiveFailures++;
+	                            if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
+	                                logErrorWithRateLimit("consecutive_failures", "lunxun杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
+	                                pausePolling();
+	                                break;
+	                            }
+	                        }
 
-							// 妫�鏌ユ槸鍚﹁揪鍒版煡璇㈡椂闂�
-							if (lastQueryTime == null || currentTime - lastQueryTime >= queryInterval) {
-								if (sendQueryToSlot(slotNumber)) {
-									// 鏇存柊鏈�鍚庢煡璇㈡椂闂�
-									lastQueryTimeMap.put(slotNumber, currentTime);
-									sentQuery = true;
-									consecutiveFailures = 0;
+	                        // 鍙戦�佹煡璇㈡寚浠ゅ悗绛夊緟50ms锛岀劧鍚庣户缁笅涓�涓崱妲�
+	                        Thread.sleep(50);
+	                    }
+	                }
 
-									// 鎴愬姛鍙戦�佹煡璇㈠悗锛岀瓑寰�100ms鍐嶇户缁笅涓�涓煡璇�
-									Thread.sleep(100);
+	                // 鏇存柊褰撳墠绱㈠紩
+	                currentIndex = (currentIndex + 1) % slotArray.length;
 
-									if (DEBUG_ENABLED) {
-										String status;
-										if ("1".equals(hasCard)) {
-											status = "鏈夊崱";
-										} else if ("-1".equals(hasCard)) {
-											status = "鏈煡";
-										} else {
-											status = "鏃犲崱";
-										}
-										
-										// 浣跨敤閲嶇敤鐨� StringBuilder 鏋勫缓璋冭瘯淇℃伅
-										debugBuilder.setLength(0);
-										debugBuilder.append("Slot ").append(slotNumber)
-										           .append(" (").append(status).append(") 鏌ヨ鎴愬姛锛岄棿闅�: ")
-										           .append(queryInterval).append("ms\n");
-										SystemDebugDialog.appendAsciiData(debugBuilder.toString());
-									}
-								} else {
-									consecutiveFailures++;
-									if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
-										logErrorWithRateLimit("consecutive_failures", "lunxun杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
-										pausePolling();
-										break;
-									}
-								}
-							}
-						}
-					}
+	            } catch (InterruptedException e) {
+	                //System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
+	                Thread.currentThread().interrupt();
+	                break;
+	            } catch (Exception e) {
+	                logErrorWithRateLimit("polling_exception", "杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
+	                consecutiveFailures++;
 
-					// 鏇存柊褰撳墠绱㈠紩
-					currentIndex = (currentIndex + 1) % slotArray.length;
+	                // 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
+	                try {
+	                    Thread.sleep(1000);
+	                } catch (InterruptedException ie) {
+	                    Thread.currentThread().interrupt();
+	                    break;
+	                }
+	            }
+	        }
 
-					// 濡傛灉娌℃湁鍙戦�佹煡璇紝绛夊緟涓�娈垫椂闂村啀缁х画
-					if (!sentQuery) {
-						Thread.sleep(pollingInterval);
-					}
-
-				} catch (InterruptedException e) {
-					//System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
-					Thread.currentThread().interrupt();
-					break;
-				} catch (Exception e) {
-					logErrorWithRateLimit("polling_exception", "杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
-					consecutiveFailures++;
-
-					// 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
-					try {
-						Thread.sleep(1000);
-					} catch (InterruptedException ie) {
-						Thread.currentThread().interrupt();
-						break;
-					}
-				}
-			}
-
-			//System.out.println("杞鏌ヨ绾跨▼缁撴潫杩愯");
+	        //System.out.println("杞鏌ヨ绾跨▼缁撴潫杩愯");
+	    }
+		// 娣诲姞鑾峰彇鍜岃缃汉鑴告娴嬬姸鎬佺殑鏂规硶
+		public static boolean isIshaveface() {
+		    return ishaveface;
 		}
 
+		public static void setIshaveface(boolean ishaveface) {
+		    lunxun.ishaveface = ishaveface;
+		    if (DEBUG_ENABLED) {
+		        if (ishaveface) {
+		            SystemDebugDialog.appendAsciiData("妫�娴嬪埌浜鸿劯锛岃疆璇㈠皢鍙煡璇㈡棤鍗″崱妲絓n");
+		        } else {
+		            SystemDebugDialog.appendAsciiData("鏈娴嬪埌浜鸿劯锛岃疆璇㈡仮澶嶆甯告煡璇㈡ā寮廫n");
+		        }
+		    }
+		}
 		/**
 		 * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
 		 * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
 		 */
-		private boolean sendQueryToSlot(int slotNumber) {
-			try {
-				// 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�            	
-				String queryCommand = getCachedQueryCommand(slotNumber);
-//				 System.out.println("鎸囦护鏄細"+queryCommand);
-				if (DEBUG_ENABLED) {
-					SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
-				}
+		/**
+	     * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
+	     * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
+	     */
+	    private boolean sendQueryToSlot(int slotNumber) {
+	        try {
+	            // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�            	
+	            String queryCommand = getCachedQueryCommand(slotNumber);
+	            System.out.println(slotNumber+"鍙戦�佷簡鎸囦护鏄細"+queryCommand+"鏃堕棿"+TimestampUtil.getTimestamp());
+	            
+	            if (DEBUG_ENABLED) {
+	                SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
+	            }
 
-				if (queryCommand != null && !queryCommand.trim().isEmpty()) {
-					// 鍙戦�佸埌涓插彛
+	            if (queryCommand != null && !queryCommand.trim().isEmpty()) {
+	                // 鍙戦�佸埌涓插彛
+	                if(sendChaxunzhiling) {
+	                    boolean sendResult = Sendmsg.sendMessage(queryCommand);
+	                    if (sendResult) {
+	                        return true;
+	                    } else {
+	                        if (DEBUG_ENABLED) {
+	                            SystemDebugDialog.appendAsciiData(slotNumber+" Send query command to card slot err");
+	                        }
+	                        // 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
+	                        serialConnected = false;
+	                        return false;
+	                    }
+	                } else {
+	                    return false;
+	                }
+	            } else {
+	                logErrorWithRateLimit("empty_query_command", "鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+	                return false;
+	            }
 
-					if(sendChaxunzhiling) {
-						boolean sendResult = Sendmsg.sendMessage(queryCommand);
-						if (sendResult) {
-							return true;
-						} else {
-							if (DEBUG_ENABLED) {
-								SystemDebugDialog.appendAsciiData(slotNumber+" Send query command to card slot err");
-							}
-							// 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
-							serialConnected = false;
-							return false;
-						}
-					}else {
-						return false;
-					}
-				} else {
-					logErrorWithRateLimit("empty_query_command", "鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
-					return false;
-				}
-
-			} catch (Exception e) {
-				logErrorWithRateLimit("send_query_exception", "鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
-				// 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
-				serialConnected = false;
-				return false;
-			}
-		}
+	        } catch (Exception e) {
+	            logErrorWithRateLimit("send_query_exception", "鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+	            // 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
+	            serialConnected = false;
+	            return false;
+	        }
+	    }
 	}
 
 	/**

--
Gitblit v1.10.0