From d22349714c8d199c02f336f90fba841ef8f5cd39 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 17:46:23 +0800
Subject: [PATCH] 优化内存后最终版202511211746
---
src/chushihua/lunxun.java | 1397 +++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 892 insertions(+), 505 deletions(-)
diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index ac7e82d..b1dc1b4 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -1,517 +1,904 @@
package chushihua;
import chuankou.Sendmsg;
+import dialog.Errlog;
+import home.Fkj;
import home.MachineConfig;
import publicway.QueryData;
+import xitongshezhi.SystemDebugDialog;
+
+import java.util.Iterator;
+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 Thread pollingThread;
- private static int pollingInterval = 100; // 榛樿杞闂撮殧
-
- // 鍗℃Ы鐩稿叧甯搁噺
- private static final int MIN_SLOT = 1;
- private static final int MAX_SLOT = 60;
-
- // 涓插彛杩炴帴妫�鏌ョ浉鍏�
- private static final int SERIAL_CHECK_INTERVAL = 5000; // 5绉掓鏌ヤ竴娆′覆鍙h繛鎺�
- private static long lastSerialCheckTime = 0;
- private static boolean serialConnected = false;
-
- /**
- * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
- * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
- */
- public static boolean checkSerialConnection() {
- // 閬垮厤棰戠箒妫�鏌ワ紝姣�5绉掓鏌ヤ竴娆�
- long currentTime = System.currentTimeMillis();
- if (currentTime - lastSerialCheckTime < SERIAL_CHECK_INTERVAL) {
- return serialConnected;
- }
-
- lastSerialCheckTime = currentTime;
-
- try {
- // 绠�鍖栨鏌ワ細鐩存帴妫�鏌endmsg鐨勪覆鍙g姸鎬侊紝鑰屼笉鏄彂閫佹祴璇曟寚浠�
- boolean result = Sendmsg.isPortOpen();
-
- if (result) {
- System.out.println("涓插彛杩炴帴姝e父");
- serialConnected = true;
- } else {
- System.err.println("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
- serialConnected = false;
- }
- } catch (Exception e) {
- System.err.println("涓插彛杩炴帴妫�鏌ュ紓甯�: " + e.getMessage());
- serialConnected = false;
- }
-
- return serialConnected;
- }
-
- /**
- * 鍚姩杞鏌ヨ
- * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
- */
- public static boolean startPolling() {
- if (isRunning) {
- System.out.println("杞鏌ヨ宸茬粡鍦ㄨ繍琛屼腑");
- return true;
- }
-
- // 鍚姩鍓嶄弗鏍兼鏌ヤ覆鍙h繛鎺�
- if (!checkSerialConnection()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
- return false;
- }
-
- // 浠庨厤缃腑鑾峰彇杞闂撮殧
- loadPollingIntervalFromConfig();
-
- isRunning = true;
- isPaused = false;
-
- try {
- pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
- pollingThread.setDaemon(true);
- pollingThread.start();
-
- System.out.println("杞鏌ヨ宸插惎鍔紝闂撮殧: " + pollingInterval + "ms");
- return true;
- } catch (Exception e) {
- System.err.println("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
- isRunning = false;
- return false;
- }
- }
-
- /**
- * 鍋滄杞鏌ヨ
- * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
- */
- public static boolean stopPolling() {
- if (!isRunning) {
- System.out.println("杞鏌ヨ鏈湪杩愯");
- return false;
- }
-
- isRunning = false;
- isPaused = false;
- if (pollingThread != null) {
- pollingThread.interrupt();
- try {
- pollingThread.join(1000); // 绛夊緟绾跨▼缁撴潫锛屾渶澶�1绉�
- } catch (InterruptedException e) {
- System.err.println("鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
- Thread.currentThread().interrupt();
- }
- pollingThread = null;
- }
-
- System.out.println("杞鏌ヨ宸插仠姝�");
- return true;
- }
-
- /**
- * 鏆傚仠杞鏌ヨ
- * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
- */
- public static boolean pausePolling() {
- if (!isRunning) {
- System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
- return false;
- }
-
- if (isPaused) {
- System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
- return false;
- }
-
- isPaused = true;
- System.out.println("杞鏌ヨ宸叉殏鍋�");
- return true;
- }
-
- /**
- * 鎭㈠杞鏌ヨ
- * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
- */
- public static boolean resumePolling() {
- if (!isRunning) {
- System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟仮澶�");
- return false;
- }
-
- if (!isPaused) {
- System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
- return false;
- }
-
- // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
- if (!checkSerialConnection()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
- return false;
- }
-
- isPaused = false;
- synchronized (lunxun.class) {
- lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
- }
- System.out.println("杞鏌ヨ宸叉仮澶�");
- return true;
- }
-
- /**
- * 妫�鏌ヨ疆璇㈢姸鎬�
- * @return true-姝e湪杩愯, false-宸插仠姝�
- */
- public static boolean isPolling() {
- return isRunning;
- }
-
- /**
- * 妫�鏌ユ槸鍚︽殏鍋�
- * @return true-宸叉殏鍋�, false-鏈殏鍋�
- */
- public static boolean isPaused() {
- return isPaused;
- }
-
- /**
- * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
- * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
- */
- public static boolean isSerialConnected() {
- return serialConnected;
- }
-
- /**
- * 璁剧疆杞闂撮殧
- * @param interval 杞闂撮殧锛堟绉掞級
- */
- public static void setPollingInterval(int interval) {
- if (interval < 10) {
- System.err.println("杞闂撮殧涓嶈兘灏忎簬10ms");
- return;
- }
-
- pollingInterval = interval;
- System.out.println("杞闂撮殧宸茶缃负: " + interval + "ms");
-
- // 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
- if (isRunning) {
- restartPolling();
- }
- }
-
- /**
- * 鑾峰彇褰撳墠杞闂撮殧
- * @return 杞闂撮殧锛堟绉掞級
- */
- public static int getPollingInterval() {
- return pollingInterval;
- }
-
- /**
- * 閲嶆柊鍚姩杞鏌ヨ
- * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
- */
- public static boolean restartPolling() {
- stopPolling();
-
- // 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
-
- return startPolling();
- }
-
- /**
- * 浠庨厤缃腑鍔犺浇杞闂撮殧
- */
- private static void loadPollingIntervalFromConfig() {
- try {
- Chushihua configSystem = Chushihua.getInstance();
- if (configSystem.isInitialized()) {
- MachineConfig machineConfig = configSystem.getMachineConfig();
- pollingInterval = machineConfig.getPollingInterval();
- System.out.println("浠庨厤缃姞杞借疆璇㈤棿闅�: " + pollingInterval + "ms");
- } else {
- System.out.println("閰嶇疆绯荤粺鏈垵濮嬪寲锛屼娇鐢ㄩ粯璁よ疆璇㈤棿闅�: " + pollingInterval + "ms");
- }
- } catch (Exception e) {
- System.err.println("鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + e.getMessage());
- System.out.println("浣跨敤榛樿杞闂撮殧: " + pollingInterval + "ms");
- }
- }
-
- /**
- * 杞浠诲姟鍐呴儴绫�
- * 浼樺寲鍐呭瓨浣跨敤锛氶伩鍏嶅湪寰幆涓垱寤烘柊瀵硅薄
- */
- private static class PollingTask implements Runnable {
- private int currentSlot = MIN_SLOT;
-
- @Override
- public void run() {
- System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
-
- while (isRunning && !Thread.currentThread().isInterrupted()) {
- try {
- // 妫�鏌ユ槸鍚︽殏鍋�
- if (isPaused) {
- synchronized (lunxun.class) {
- while (isPaused && isRunning) {
- lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
- }
- }
- continue;
- }
-
- // 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
- if (currentSlot % 10 == 0 && !checkSerialConnection()) {
- System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
- pausePolling();
- continue;
- }
-
- // 鍙戦�佸綋鍓嶅崱妲界殑鏌ヨ鎸囦护
- sendQueryToSlot(currentSlot);
-
- // 绉诲姩鍒颁笅涓�涓崱妲�
- currentSlot++;
- if (currentSlot > MAX_SLOT) {
- currentSlot = MIN_SLOT; // 寰幆鍥炲埌绗竴涓崱妲�
-
- // 鍦ㄥ惊鐜洖鍒扮涓�涓崱妲藉悗锛岀瓑寰呴棿闅旀椂闂�
- // 閬垮厤杩炵画鍙戦�佷袱涓浉閭诲懆鏈熺殑绗竴涓崱妲�
- Thread.sleep(pollingInterval);
- continue; // 璺宠繃鏈寰幆鐨勫悗缁瓑寰�
- }
-
- // 绛夊緟鎸囧畾鐨勯棿闅旀椂闂�
- Thread.sleep(pollingInterval);
-
- } catch (InterruptedException e) {
- System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- System.err.println("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
-
- // 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
- try {
- Thread.sleep(1000);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
- }
- }
- }
-
- System.out.println("杞鏌ヨ绾跨▼缁撴潫杩愯");
- }
-
- /**
- * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠�
- * 浼樺寲锛氶伩鍏嶉噸澶嶅垱寤哄璞★紝浣跨敤灞�閮ㄥ彉閲�
- */
- private void sendQueryToSlot(int slotNumber) {
- try {
- // 鐢熸垚鏌ヨ鎸囦护
- String queryCommand = QueryData.queryData(slotNumber);
-
- if (queryCommand != null && !queryCommand.trim().isEmpty()) {
- // 鍙戦�佸埌涓插彛
- boolean sendResult = Sendmsg.sendMessage(queryCommand);
-
- if (sendResult) {
- // 鍙湪璋冭瘯鏃惰緭鍑猴紝閬垮厤棰戠箒鎵撳嵃
- if (slotNumber == 1 || slotNumber % 20 == 0) {
- System.out.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber);
- }
- } else {
- System.err.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
- // 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
- serialConnected = false;
- }
- } else {
- System.err.println("鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
- }
-
- } catch (Exception e) {
- System.err.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
- // 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
- serialConnected = false;
- }
- }
- }
-
- /**
- * 绔嬪嵆鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠わ紙涓嶇瓑寰呰疆璇級
- * @param slotNumber 鍗℃Ы缂栧彿 (1-60)
- * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
- */
- public static boolean sendImmediateQuery(int slotNumber) {
- if (slotNumber < MIN_SLOT || slotNumber > MAX_SLOT) {
- System.err.println("鍗℃Ы缂栧彿蹇呴』鍦�" + MIN_SLOT + "-" + MAX_SLOT + "涔嬮棿");
- return false;
- }
-
- // 妫�鏌ヤ覆鍙h繛鎺�
- if (!checkSerialConnection()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
- return false;
- }
-
- try {
- // 鐢熸垚鏌ヨ鎸囦护
- String queryCommand = QueryData.queryData(slotNumber);
-
- if (queryCommand != null && !queryCommand.trim().isEmpty()) {
- // 鍙戦�佸埌涓插彛
- boolean sendResult = Sendmsg.sendMessage(queryCommand);
-
- if (sendResult) {
- System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
- return true;
- } else {
- System.err.println("绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
- return false;
- }
- } else {
- System.err.println("绔嬪嵆鏌ヨ澶辫触 - 鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
- return false;
- }
-
- } catch (Exception e) {
- System.err.println("绔嬪嵆鏌ヨ鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
- return false;
- }
- }
-
- /**
- * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�
- * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
- */
- public static int sendImmediateQueryToAll() {
- // 妫�鏌ヤ覆鍙h繛鎺�
- if (!checkSerialConnection()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
- return 0;
- }
-
- int successCount = 0;
-
- System.out.println("寮�濮嬫壒閲忔煡璇㈡墍鏈夊崱妲�...");
-
- for (int slot = MIN_SLOT; slot <= MAX_SLOT; slot++) {
- if (sendImmediateQuery(slot)) {
- successCount++;
- }
-
- // 灏忛棿闅旈伩鍏嶄覆鍙f嫢鍫�
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- break;
- }
- }
-
- System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + MAX_SLOT);
- return successCount;
- }
-
- /**
- * 鎵嬪姩璁剧疆涓插彛杩炴帴鐘舵�侊紙鐢ㄤ簬澶栭儴妫�娴嬪埌涓插彛鐘舵�佸彉鍖栨椂璋冪敤锛�
- * @param connected 涓插彛杩炴帴鐘舵��
- */
- public static void setSerialConnected(boolean connected) {
- serialConnected = connected;
- lastSerialCheckTime = System.currentTimeMillis();
-
- if (connected) {
- System.out.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 宸茶繛鎺�");
- } else {
- System.err.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 鏈繛鎺�");
- // 濡傛灉涓插彛鏂紑涓旇疆璇㈡鍦ㄨ繍琛岋紝鑷姩鏆傚仠杞
- if (isRunning && !isPaused) {
- pausePolling();
- }
- }
- }
-
- /**
- * 鑾峰彇杞鐘舵�佷俊鎭�
- * @return 鐘舵�佷俊鎭瓧绗︿覆
- */
- public static String getPollingStatus() {
- String status;
- if (!isRunning) {
- status = "宸插仠姝�";
- } else if (isPaused) {
- status = "宸叉殏鍋�";
- } else {
- status = "杩愯涓�";
- }
-
- String serialStatus = serialConnected ? "宸茶繛鎺�" : "鏈繛鎺�";
-
- return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鍗℃Ы鑼冨洿: %d-%d",
- status, serialStatus, pollingInterval, MIN_SLOT, MAX_SLOT);
- }
-
- /**
- * 鐩存帴璁剧疆杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
- * @param paused true-鏆傚仠杞, false-鎭㈠杞
- * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触锛堝杞鏈繍琛岋級
- */
- public static boolean setPollingPaused(boolean paused) {
- if (!isRunning) {
- System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
- return false;
- }
-
- if (paused) {
- // 璇锋眰鏆傚仠
- if (!isPaused) {
- isPaused = true;
- System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鏆傚仠");
- return true;
- } else {
- System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
- return false;
- }
- } else {
- // 璇锋眰鎭㈠
- if (isPaused) {
- // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
- if (!checkSerialConnection()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
- return false;
- }
-
- isPaused = false;
- synchronized (lunxun.class) {
- lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
- }
- System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鎭㈠");
- return true;
- } else {
- System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
- return false;
- }
- }
- }
+ 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; // 榛樿杞闂撮殧
+ public static boolean sendChaxunzhiling=true;//鏄惁鍚戜覆鍙e彂閫佹煡璇㈡寚浠�
+
+ // 鍗℃Ы鐩稿叧甯搁噺
+ private static final int MIN_SLOT = 1;
+ private static final int MAX_SLOT = 60;
+
+ // 涓插彛杩炴帴妫�鏌ョ浉鍏�
+ private static final int SERIAL_CHECK_INTERVAL = 5000; // 5绉掓鏌ヤ竴娆′覆鍙h繛鎺�
+ private static long lastSerialCheckTime = 0;
+ private static boolean serialConnected = false;
+
+ // 鎬ц兘浼樺寲锛氭煡璇㈡寚浠ょ紦瀛�
+ private static final Map<Integer, String> queryCommandCache = new ConcurrentHashMap<>();
+
+ // 鍐呭瓨浼樺寲锛氱紦瀛樺ぇ灏忛檺鍒跺拰娓呯悊鏈哄埗
+ private static final int MAX_CACHE_SIZE = 100;
+ private static final long CACHE_CLEANUP_INTERVAL = 60000; // 1鍒嗛挓娓呯悊涓�娆�
+ private static long lastCleanupTime = 0;
+
+ // 閿欒鏃ュ織闄愭祦鏈哄埗
+ private static final Map<String, Long> lastErrorLogTime = new ConcurrentHashMap<>();
+ private static final long ERROR_LOG_INTERVAL = 5000; // 鐩稿悓閿欒5绉掑唴鍙褰曚竴娆�
+
+ // 璋冭瘯妯″紡鎺у埗
+ public static boolean DEBUG_ENABLED = false;
+
+ // 鏂板锛氫笉鍚岀姸鎬佸崱妲界殑鏌ヨ棰戠巼鎺у埗
+ private static final int NO_CARD_QUERY_INTERVAL = 100; // 鏃犲崱鍗℃Ы鏌ヨ闂撮殧锛�100ms
+ private static final int HAS_CARD_QUERY_INTERVAL = 10000; // 鏈夊崱鍗℃Ы鏌ヨ闂撮殧锛�10绉�
+ private static final Map<Integer, Long> lastQueryTimeMap = new ConcurrentHashMap<>(); // 璁板綍姣忎釜鍗℃Ы鐨勬渶鍚庢煡璇㈡椂闂�
+
+ public static boolean isSendChaxunzhiling() {
+ return sendChaxunzhiling;
+ }
+
+ public static void setSendChaxunzhiling(boolean sendChaxunzhiling) {
+ lunxun.sendChaxunzhiling = sendChaxunzhiling;
+ }
+
+ /**
+ * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬� - 浼樺寲鐗堟湰锛屾坊鍔犻噸璇曟満鍒�
+ * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
+ */
+ public static boolean checkSerialConnection() {
+ // 閬垮厤棰戠箒妫�鏌ワ紝姣�5绉掓鏌ヤ竴娆�
+ long currentTime = System.currentTimeMillis();
+ if (currentTime - lastSerialCheckTime < SERIAL_CHECK_INTERVAL) {
+ return serialConnected;
+ }
+
+ lastSerialCheckTime = currentTime;
+
+ try {
+ // 绠�鍖栨鏌ワ細鐩存帴妫�鏌endmsg鐨勪覆鍙g姸鎬侊紝鑰屼笉鏄彂閫佹祴璇曟寚浠�
+ boolean result = Sendmsg.isPortOpen();
+
+ if (result) {
+ if (DEBUG_ENABLED) {
+ //System.out.println("涓插彛杩炴帴姝e父");
+ }
+ serialConnected = true;
+ } else {
+ logErrorWithRateLimit("serial_connection_failed", "涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
+ serialConnected = false;
+ }
+ } catch (Exception e) {
+ logErrorWithRateLimit("serial_connection_exception", "涓插彛杩炴帴妫�鏌ュ紓甯�: " + e.getMessage());
+ serialConnected = false;
+ }
+
+ return serialConnected;
+ }
+
+ /**
+ * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
+ */
+ 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() {
+ if (isRunning) {
+ //System.out.println("杞鏌ヨ宸茬粡鍦ㄨ繍琛屼腑");
+ return true;
+ }
+
+ // 鍚姩鍓嶄弗鏍兼鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ logErrorWithRateLimit("start_polling_serial_failed", "涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
+ return false;
+ }
+
+ // 鍚姩鍓嶅厛娓呯悊涓�娆″唴瀛�
+ performCleanup();
+
+ // 浠庨厤缃腑鑾峰彇杞闂撮殧
+ loadPollingIntervalFromConfig();
+
+ // 鍒濆鍖栨渶鍚庢煡璇㈡椂闂磋褰�
+ initializeLastQueryTimes();
+
+ isRunning = true;
+ isPaused = false;
+ shouldStop.set(false);
+
+ try {
+ pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
+ pollingThread.setDaemon(true);
+ pollingThread.start();
+ return true;
+ } catch (Exception e) {
+ logErrorWithRateLimit("start_polling_thread_exception", "鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ isRunning = false;
+ shouldStop.set(true);
+ return false;
+ }
+ }
+
+ /**
+ * 鍒濆鍖栨渶鍚庢煡璇㈡椂闂磋褰�
+ */
+ private static void initializeLastQueryTimes() {
+ lastQueryTimeMap.clear();
+ for (int i = MIN_SLOT; i <= MAX_SLOT; i++) {
+ lastQueryTimeMap.put(i, 0L); // 鍒濆鍖栦负0锛岃〃绀轰粠鏈煡璇㈣繃
+ }
+ }
+
+ /**
+ * 鍋滄杞鏌ヨ - 淇鐗堟湰
+ * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
+ */
+ public static boolean stopPolling() {
+ if (!isRunning) {
+ //System.out.println("杞鏌ヨ鏈湪杩愯");
+ return false;
+ }
+
+ shouldStop.set(true);
+ isRunning = false;
+ isPaused = false;
+
+ if (pollingThread != null) {
+ pollingThread.interrupt();
+ try {
+ pollingThread.join(3000); // 绛夊緟3绉�
+ // 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
+ if (pollingThread.isAlive()) {
+ logErrorWithRateLimit("polling_thread_stop_timeout", "杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
+ // 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
+ pollingThread.setDaemon(true);
+ }
+ } catch (InterruptedException e) {
+ logErrorWithRateLimit("stop_polling_interrupted", "鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
+ Thread.currentThread().interrupt();
+ } catch (Exception e) {
+ logErrorWithRateLimit("stop_polling_exception", "鍋滄杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ } finally {
+ pollingThread = null;
+ }
+ }
+
+ shouldStop.set(false);
+ //System.out.println("杞鏌ヨ宸插仠姝� - 涓插彛鏁版嵁鎺ユ敹涓嶅彈褰卞搷");
+ return true;
+ }
+
+ /**
+ * 鏆傚仠杞鏌ヨ
+ * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
+ */
+ public static boolean pausePolling() {
+ if (!isRunning) {
+ if (DEBUG_ENABLED) {
+ //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
+ }
+ return false;
+ }
+
+ if (isPaused) {
+ if (DEBUG_ENABLED) {
+ //System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+ }
+ return false;
+ }
+
+ isPaused = true;
+ //System.out.println("杞鏌ヨ宸叉殏鍋� - 浠呭仠姝㈠彂閫佹煡璇㈡寚浠�");
+ return true;
+ }
+
+ /**
+ * 鎭㈠杞鏌ヨ
+ * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
+ */
+ public static boolean resumePolling() {
+ if (!isRunning) {
+ //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟仮澶�");
+ return false;
+ }
+
+ if (!isPaused) {
+ //System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
+ return false;
+ }
+
+ // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ logErrorWithRateLimit("resume_polling_serial_failed", "涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+ return false;
+ }
+
+ isPaused = false;
+ synchronized (lunxun.class) {
+ lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
+ }
+ //System.out.println("杞鏌ヨ宸叉仮澶�");
+ return true;
+ }
+
+ /**
+ * 妫�鏌ヨ疆璇㈢姸鎬�
+ * @return true-姝e湪杩愯, false-宸插仠姝�
+ */
+ public static boolean isPolling() {
+ return isRunning;
+ }
+
+ /**
+ * 妫�鏌ユ槸鍚︽殏鍋�
+ * @return true-宸叉殏鍋�, false-鏈殏鍋�
+ */
+ public static boolean isPaused() {
+ return isPaused;
+ }
+
+ /**
+ * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+ * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
+ */
+ public static boolean isSerialConnected() {
+ return serialConnected;
+ }
+
+ /**
+ * 璁剧疆杞闂撮殧
+ * @param interval 杞闂撮殧锛堟绉掞級
+ */
+ public static void setPollingInterval(int interval) {
+ if (interval < 10) {
+ logErrorWithRateLimit("polling_interval_too_small", "杞闂撮殧涓嶈兘灏忎簬10ms");
+ return;
+ }
+
+ pollingInterval = interval;
+ //System.out.println("杞闂撮殧宸茶缃负: " + interval + "ms");
+
+ // 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
+ if (isRunning) {
+ restartPolling();
+ }
+ }
+
+ /**
+ * 鑾峰彇褰撳墠杞闂撮殧
+ * @return 杞闂撮殧锛堟绉掞級
+ */
+ public static int getPollingInterval() {
+ return pollingInterval;
+ }
+
+ /**
+ * 閲嶆柊鍚姩杞鏌ヨ
+ * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
+ */
+ public static boolean restartPolling() {
+ stopPolling();
+
+ // 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+
+ return startPolling();
+ }
+
+ /**
+ * 浠庨厤缃腑鍔犺浇杞闂撮殧
+ */
+ private static void loadPollingIntervalFromConfig() {
+ try {
+ Chushihua configSystem = Chushihua.getInstance();
+ if (configSystem.isInitialized()) {
+ MachineConfig machineConfig = configSystem.getMachineConfig();
+ pollingInterval = machineConfig.getPollingInterval();
+ //System.out.println("浠庨厤缃姞杞借疆璇㈤棿闅�: " + pollingInterval + "ms");
+ } else {
+ //System.out.println("閰嶇疆绯荤粺鏈垵濮嬪寲锛屼娇鐢ㄩ粯璁よ疆璇㈤棿闅�: " + pollingInterval + "ms");
+ }
+ } catch (Exception e) {
+ logErrorWithRateLimit("load_polling_interval_failed", "鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + e.getMessage());
+ //System.out.println("浣跨敤榛樿杞闂撮殧: " + pollingInterval + "ms");
+ }
+ }
+
+ /**
+ * 鑾峰彇缂撳瓨鐨勬煡璇㈡寚浠�
+ */
+ 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 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("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
+
+ while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
+ try {
+
+// 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;
+ }
+
+ // 瀹氭湡娓呯悊缂撳瓨锛堟瘡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;
+ }
+
+ // 鏂板锛氭牴鎹崱妲界姸鎬佸拰鏌ヨ棰戠巼鍐冲畾鏄惁鍙戦�佹煡璇�
+ boolean sentQuery = false;
+ long currentTime = System.currentTimeMillis();
+
+ // 閬嶅巻鍗℃Ы锛屽鎵鹃渶瑕佹煡璇㈢殑鍗℃Ы
+ 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);
+
+ // 纭畾鏌ヨ闂撮殧锛氬彧鏈塰asCard="1"鐨勫崱妲戒娇鐢�10绉掗棿闅旓紝鍏朵粬鎯呭喌锛堝寘鎷�"-1"锛夐兘浣跨敤100ms闂撮殧
+ int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
+
+ // 妫�鏌ユ槸鍚﹁揪鍒版煡璇㈡椂闂�
+ if (lastQueryTime == null || currentTime - lastQueryTime >= queryInterval) {
+ if (sendQueryToSlot(slotNumber)) {
+ // 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+ lastQueryTimeMap.put(slotNumber, currentTime);
+ sentQuery = true;
+ consecutiveFailures = 0;
+
+ // 鎴愬姛鍙戦�佹煡璇㈠悗锛岀瓑寰�100ms鍐嶇户缁笅涓�涓煡璇�
+ Thread.sleep(100);
+
+ 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;
+ }
+ }
+ }
+ }
+ }
+
+ // 鏇存柊褰撳墠绱㈠紩
+ currentIndex = (currentIndex + 1) % slotArray.length;
+
+ // 濡傛灉娌℃湁鍙戦�佹煡璇紝绛夊緟涓�娈垫椂闂村啀缁х画
+ 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("杞鏌ヨ绾跨▼缁撴潫杩愯");
+ }
+
+ /**
+ * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
+ * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
+ */
+ private boolean sendQueryToSlot(int slotNumber) {
+ try {
+ // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
+ String queryCommand = getCachedQueryCommand(slotNumber);
+// System.out.println("鎸囦护鏄細"+queryCommand);
+ if (DEBUG_ENABLED) {
+ SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
+ }
+
+ 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;
+ }
+
+ } catch (Exception e) {
+ logErrorWithRateLimit("send_query_exception", "鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ // 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
+ serialConnected = false;
+ return false;
+ }
+ }
+ }
+
+ /**
+ * 绔嬪嵆鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠わ紙涓嶇瓑寰呰疆璇級
+ * @param slotNumber 鍗℃Ы缂栧彿 (1-60)
+ * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
+ */
+ public static boolean sendImmediateQuery(int slotNumber) {
+ if (slotNumber < MIN_SLOT || slotNumber > MAX_SLOT) {
+ logErrorWithRateLimit("invalid_slot_number", "鍗℃Ы缂栧彿蹇呴』鍦�" + MIN_SLOT + "-" + MAX_SLOT + "涔嬮棿");
+ return false;
+ }
+
+ // 妫�鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ logErrorWithRateLimit("immediate_query_serial_failed", "涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
+ return false;
+ }
+
+ try {
+ // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
+ String queryCommand = getCachedQueryCommand(slotNumber);
+
+ if (queryCommand != null && !queryCommand.trim().isEmpty()) {
+ // 鍙戦�佸埌涓插彛
+ boolean sendResult = Sendmsg.sendMessage(queryCommand);
+
+ if (sendResult) {
+ // 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+ lastQueryTimeMap.put(slotNumber, System.currentTimeMillis());
+
+ if (DEBUG_ENABLED) {
+ //System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
+ }
+ return true;
+ } else {
+ logErrorWithRateLimit("immediate_query_send_failed", "绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
+ return false;
+ }
+ } else {
+ logErrorWithRateLimit("immediate_query_empty_command", "绔嬪嵆鏌ヨ澶辫触 - 鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+ return false;
+ }
+
+ } catch (Exception e) {
+ logErrorWithRateLimit("immediate_query_exception", "绔嬪嵆鏌ヨ鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ return false;
+ }
+ }
+
+ /**
+ * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�- 浼樺寲鐗堟湰
+ * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
+ */
+ public static int sendImmediateQueryToAll() {
+ // 妫�鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ logErrorWithRateLimit("batch_query_serial_failed", "涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
+ return 0;
+ }
+
+ int successCount = 0;
+ int batchSize = 5; // 姣忔壒娆″彂閫�5涓煡璇�
+ int totalSlots = MAX_SLOT - MIN_SLOT + 1;
+ long currentTime = System.currentTimeMillis();
+
+ //System.out.println("寮�濮嬫壒閲忔煡璇㈡墍鏈夊崱妲�...");
+
+ for (int batchStart = MIN_SLOT; batchStart <= MAX_SLOT; batchStart += batchSize) {
+ if (shouldStop.get()) {
+ break;
+ }
+
+ int batchEnd = Math.min(batchStart + batchSize - 1, MAX_SLOT);
+
+ // 鎵规鍐呮煡璇�
+ for (int slot = batchStart; slot <= batchEnd; slot++) {
+ if (sendImmediateQuery(slot)) {
+ successCount++;
+ // 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+ lastQueryTimeMap.put(slot, currentTime);
+ }
+ }
+
+ // 鎵规闂撮棿闅旓紝閬垮厤涓插彛鎷ュ牭
+ if (batchEnd < MAX_SLOT) {
+ try {
+ Thread.sleep(50);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ break;
+ }
+ }
+ }
+
+ //System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + totalSlots);
+ return successCount;
+ }
+
+ /**
+ * 鎵嬪姩璁剧疆涓插彛杩炴帴鐘舵�侊紙鐢ㄤ簬澶栭儴妫�娴嬪埌涓插彛鐘舵�佸彉鍖栨椂璋冪敤锛�
+ * @param connected 涓插彛杩炴帴鐘舵��
+ */
+ public static void setSerialConnected(boolean connected) {
+ serialConnected = connected;
+ lastSerialCheckTime = System.currentTimeMillis();
+
+ if (connected) {
+ // //System.out.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 宸茶繛鎺�");
+ } else {
+ logErrorWithRateLimit("serial_disconnected_external", "涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 鏈繛鎺�");
+ // 濡傛灉涓插彛鏂紑涓旇疆璇㈡鍦ㄨ繍琛岋紝鑷姩鏆傚仠杞
+ if (isRunning && !isPaused) {
+ pausePolling();
+ }
+ }
+ }
+
+ /**
+ * 鑾峰彇杞鐘舵�佷俊鎭�
+ * @return 鐘舵�佷俊鎭瓧绗︿覆
+ */
+ public static String getPollingStatus() {
+ String status;
+ if (!isRunning) {
+ status = "宸插仠姝�";
+ } else if (isPaused) {
+ status = "宸叉殏鍋�";
+ } else {
+ status = "杩愯涓�";
+ }
+
+ String serialStatus = serialConnected ? "宸茶繛鎺�" : "鏈繛鎺�";
+ int cacheSize = queryCommandCache.size();
+
+ // 缁熻涓嶅悓鐘舵�佺殑鍗℃Ы鏁伴噺
+ int noCardCount = 0;
+ int hasCardCount = 0;
+ Fkj[] slotArray = SlotManager.getSlotArray();
+ if (slotArray != null) {
+ for (Fkj slot : slotArray) {
+ if (slot != null && "1".equals(slot.getHasCard())) {
+ hasCardCount++;
+ } else {
+ noCardCount++;
+ }
+ }
+ }
+
+ return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鎸囦护缂撳瓨: %d, 鍗℃Ы鑼冨洿: %d-%d, 鏃犲崱: %d(100ms), 鏈夊崱: %d(10s)\n%s",
+ status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT,
+ noCardCount, hasCardCount, getMemoryStatus());
+ }
+
+ /**
+ * 鐩存帴璁剧疆杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
+ * @param paused true-鏆傚仠杞, false-鎭㈠杞
+ * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触锛堝杞鏈繍琛岋級
+ */
+ public static boolean setPollingPaused(boolean paused) {
+ if (!isRunning) {
+ //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
+ return false;
+ }
+
+ if (paused) {
+ // 璇锋眰鏆傚仠
+ if (!isPaused) {
+ isPaused = true;
+ //System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鏆傚仠");
+ return true;
+ } else {
+ //System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+ return false;
+ }
+ } else {
+ // 璇锋眰鎭㈠
+ if (isPaused) {
+ // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ logErrorWithRateLimit("external_resume_serial_failed", "涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+ return false;
+ }
+
+ isPaused = false;
+ synchronized (lunxun.class) {
+ lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
+ }
+ //System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鎭㈠");
+ return true;
+ } else {
+ //System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
+ return false;
+ }
+ }
+ }
+
+ /**
+ * 鑾峰彇鎬ц兘缁熻淇℃伅
+ */
+ public static String getPerformanceStats() {
+ long currentTime = System.currentTimeMillis();
+ int overdueNoCard = 0;
+ int overdueHasCard = 0;
+
+ Fkj[] slotArray = SlotManager.getSlotArray();
+ if (slotArray != null) {
+ for (int i = 0; i < slotArray.length; i++) {
+ Fkj slot = slotArray[i];
+ if (slot != null) {
+ int slotNumber = i + 1;
+ Long lastQueryTime = lastQueryTimeMap.get(slotNumber);
+ if (lastQueryTime != null) {
+ String hasCard = slot.getHasCard();
+ int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
+ if (currentTime - lastQueryTime > queryInterval) {
+ if ("1".equals(hasCard)) {
+ overdueHasCard++;
+ } else {
+ overdueNoCard++;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return String.format("鏌ヨ鎸囦护缂撳瓨澶у皬: %d, 杞闂撮殧: %dms, 瓒呮椂鏃犲崱: %d, 瓒呮椂鏈夊崱: %d",
+ queryCommandCache.size(), pollingInterval, overdueNoCard, overdueHasCard);
+ }
+
+ /**
+ * 璁剧疆鏃犲崱鍗℃Ы鏌ヨ闂撮殧
+ * @param interval 鏌ヨ闂撮殧锛堟绉掞級
+ */
+ public static void setNoCardQueryInterval(int interval) {
+ if (interval < 10) {
+ logErrorWithRateLimit("no_card_interval_too_small", "鏃犲崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬10ms");
+ return;
+ }
+ // 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+ //System.out.println("鏃犲崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+ }
+
+ /**
+ * 璁剧疆鏈夊崱鍗℃Ы鏌ヨ闂撮殧
+ * @param interval 鏌ヨ闂撮殧锛堟绉掞級
+ */
+ public static void setHasCardQueryInterval(int interval) {
+ if (interval < 1000) {
+ logErrorWithRateLimit("has_card_interval_too_small", "鏈夊崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬1000ms");
+ return;
+ }
+ // 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+ //System.out.println("鏈夊崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+ }
+
+ public static boolean isDEBUG_ENABLED() {
+ return DEBUG_ENABLED;
+ }
+
+ public static void setDEBUG_ENABLED(boolean dEBUG_ENABLED) {
+ DEBUG_ENABLED = dEBUG_ENABLED;
+ }
+
+ // ==================== 鏂板鍐呭瓨浼樺寲鏂规硶 ====================
+
+ /**
+ * 娓呯悊鏃х紦瀛� - 闃叉鍐呭瓨鏃犻檺澧為暱
+ */
+ private static void cleanupOldCache() {
+ long currentTime = System.currentTimeMillis();
+ if (currentTime - lastCleanupTime < CACHE_CLEANUP_INTERVAL) {
+ return;
+ }
+
+ lastCleanupTime = currentTime;
+
+ // 娓呯悊闀挎椂闂存湭浣跨敤鐨勬煡璇㈡椂闂磋褰�
+ long cleanupThreshold = currentTime - 300000; // 5鍒嗛挓鏈娇鐢�
+ lastQueryTimeMap.entrySet().removeIf(entry ->
+ currentTime - entry.getValue() > cleanupThreshold
+ );
+
+ // 闄愬埗鏌ヨ鎸囦护缂撳瓨澶у皬
+ if (queryCommandCache.size() > MAX_CACHE_SIZE) {
+ Iterator<Map.Entry<Integer, String>> iterator = queryCommandCache.entrySet().iterator();
+ int itemsToRemove = queryCommandCache.size() - MAX_CACHE_SIZE;
+ for (int i = 0; i < itemsToRemove && iterator.hasNext(); i++) {
+ iterator.next();
+ iterator.remove();
+ }
+ }
+
+ // 娓呯悊閿欒鏃ュ織闄愭祦璁板綍
+ lastErrorLogTime.entrySet().removeIf(entry ->
+ currentTime - entry.getValue() > 300000 // 5鍒嗛挓
+ );
+ }
+
+ /**
+ * 闄愭祦閿欒鏃ュ織 - 闃叉澶ч噺閲嶅鏃ュ織鍗犵敤鍐呭瓨
+ */
+ private static void logErrorWithRateLimit(String errorKey, String message) {
+ long currentTime = System.currentTimeMillis();
+ Long lastTime = lastErrorLogTime.get(errorKey);
+
+ if (lastTime == null || currentTime - lastTime > ERROR_LOG_INTERVAL) {
+ Errlog.logOperation(message);
+ lastErrorLogTime.put(errorKey, currentTime);
+
+ // 娓呯悊杩囨湡鐨勯敊璇褰�
+ if (lastErrorLogTime.size() > 50) {
+ lastErrorLogTime.entrySet().removeIf(entry ->
+ currentTime - entry.getValue() > 300000 // 5鍒嗛挓
+ );
+ }
+ }
+ }
+
+ /**
+ * 鑾峰彇鍐呭瓨鐘舵�佷俊鎭�
+ */
+ public static String getMemoryStatus() {
+ Runtime runtime = Runtime.getRuntime();
+ long totalMemory = runtime.totalMemory();
+ long freeMemory = runtime.freeMemory();
+ long usedMemory = totalMemory - freeMemory;
+ long maxMemory = runtime.maxMemory();
+
+ return String.format("鍐呭瓨浣跨敤: %.2fMB/%.2fMB (鏈�澶�: %.2fMB), 缂撳瓨: 鏃堕棿璁板綍=%d, 鎸囦护缂撳瓨=%d, 閿欒璁板綍=%d",
+ usedMemory / (1024.0 * 1024.0),
+ totalMemory / (1024.0 * 1024.0),
+ maxMemory / (1024.0 * 1024.0),
+ lastQueryTimeMap.size(),
+ queryCommandCache.size(),
+ lastErrorLogTime.size());
+ }
+
+ /**
+ * 鎵嬪姩瑙﹀彂鍐呭瓨娓呯悊
+ */
+ public static void performCleanup() {
+ // 娓呯悊鏌ヨ鏃堕棿璁板綍涓暱鏃堕棿鏈煡璇㈢殑鍗℃Ы
+ long cleanupThreshold = System.currentTimeMillis() - 3600000; // 1灏忔椂
+ lastQueryTimeMap.entrySet().removeIf(entry ->
+ entry.getValue() < cleanupThreshold
+ );
+
+ // 娓呯┖鏌ヨ鎸囦护缂撳瓨
+ queryCommandCache.clear();
+
+ // 娓呯┖閿欒鏃ュ織闄愭祦璁板綍
+ lastErrorLogTime.clear();
+
+ // 寤鸿绯荤粺杩涜鍨冨溇鍥炴敹锛堜絾涓嶅己鍒讹級
+ System.gc();
+
+ if (DEBUG_ENABLED) {
+ SystemDebugDialog.appendAsciiData("鎵ц鍐呭瓨娓呯悊瀹屾垚\n");
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3