From a6077217e25f5804027194a5c2848e773eda1abd Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 15:47:17 +0800
Subject: [PATCH] 修改
---
src/chushihua/lunxun.java | 1386 +++++++++++++++++++++++++++++++--------------------------
1 files changed, 756 insertions(+), 630 deletions(-)
diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index 8d08dff..217dab3 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -1,9 +1,10 @@
package chushihua;
import chuankou.Sendmsg;
+import dialog.Errlog;
+import home.Fkj;
import home.MachineConfig;
import publicway.QueryData;
-import xitongshezhi.Fkj;
import xitongshezhi.SystemDebugDialog;
import java.util.Map;
@@ -14,637 +15,762 @@
* 杞鏌ヨ绫� - 浼樺寲鐗堟湰
* 鐢ㄤ簬瀹氭椂鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠�
* 鏀寔鏆傚仠鍜屾仮澶嶅姛鑳斤紝妫�鏌ヤ覆鍙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; // 榛樿杞闂撮殧
-
- // 鍗℃Ы鐩稿叧甯搁噺
- 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<>();
-
- // 璋冭瘯妯″紡鎺у埗
- public static boolean DEBUG_ENABLED = 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) {
- if (DEBUG_ENABLED) {
- //System.out.println("涓插彛杩炴帴姝e父");
- }
- serialConnected = true;
- } else {
- System.err.println("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
- serialConnected = false;
- }
- } catch (Exception e) {
- System.err.println("涓插彛杩炴帴妫�鏌ュ紓甯�: " + 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()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
- return false;
- }
-
- // 浠庨厤缃腑鑾峰彇杞闂撮殧
- loadPollingIntervalFromConfig();
-
- 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) {
- System.err.println("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
- isRunning = false;
- shouldStop.set(true);
- return false;
- }
- }
-
- /**
- * 鍋滄杞鏌ヨ - 淇鐗堟湰
- * @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()) {
- 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;
- }
- }
-
- 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()) {
- 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(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) {
- System.err.println("鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + 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; // 鏈�澶ц繛缁け璐ユ鏁�
-
- @Override
- public void run() {
- //System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
-
- while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
- try {
- // 妫�鏌ユ槸鍚︽殏鍋�
- if (isPaused) {
- synchronized (lunxun.class) {
- while (isPaused && isRunning && !shouldStop.get()) {
- lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
- }
- }
- continue;
- }
-
- // 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
- if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
- System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
- pausePolling();
- continue;
- }
-
- // 鑾峰彇鍗℃Ы鏁扮粍
- Fkj[] slotArray = SlotManager.getSlotArray();
- if (slotArray == null || slotArray.length == 0) {
- System.err.println("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
- Thread.sleep(pollingInterval);
- continue;
- }
-
- // 閬嶅巻鎵�鏈夊崱妲斤紝鍙粰 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("lunxun杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
- pausePolling();
- break;
- }
- }
- }
- }
-
- // 瀵逛簬涓嶉渶瑕佸彂閫佹煡璇㈢殑鍗℃Ы锛屾甯告洿鏂扮储寮�
- currentIndex = (currentIndex + 1) % slotArray.length;
- checkedSlots++;
- }
-
- // 濡傛灉娌℃湁鎵惧埌闇�瑕佹煡璇㈢殑鍗℃Ы锛岀瓑寰呬竴娈垫椂闂村啀缁х画
- if (!sentQuery) {
- Thread.sleep(pollingInterval * 2); // 娌℃湁鏌ヨ鏃剁瓑寰呮椂闂村姞鍊�
- }
-
- } catch (InterruptedException e) {
- //System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- System.err.println("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + 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);
-
- if (queryCommand != null && !queryCommand.trim().isEmpty()) {
- // 鍙戦�佸埌涓插彛
- boolean sendResult = Sendmsg.sendMessage(queryCommand);
-
- if (sendResult) {
- // 鍙湪璋冭瘯鏃惰緭鍑猴紝閬垮厤棰戠箒鎵撳嵃
- if (DEBUG_ENABLED) {
- SystemDebugDialog.appendAsciiData(String.format("Slot %d Send query (hasCard !=1)\n", slotNumber));
- }
- return true;
- } else {
- 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;
- }
- }
- }
-
- /**
- * 绔嬪嵆鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠わ紙涓嶇瓑寰呰疆璇級
- * @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 (!checkSerialConnectionWithRetry()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
- return false;
- }
-
- try {
- // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
- String queryCommand = getCachedQueryCommand(slotNumber);
-
- if (queryCommand != null && !queryCommand.trim().isEmpty()) {
- // 鍙戦�佸埌涓插彛
- boolean sendResult = Sendmsg.sendMessage(queryCommand);
-
- if (sendResult) {
- if (DEBUG_ENABLED) {
- //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 (!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 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++;
- }
- }
-
- // 鎵规闂撮棿闅旓紝閬垮厤涓插彛鎷ュ牭
- 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 {
- 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 ? "宸茶繛鎺�" : "鏈繛鎺�";
- int cacheSize = queryCommandCache.size();
-
- return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鎸囦护缂撳瓨: %d, 鍗℃Ы鑼冨洿: %d-%d",
- status, serialStatus, pollingInterval, cacheSize, 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 (!checkSerialConnectionWithRetry()) {
- System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
- 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() {
- return String.format("鏌ヨ鎸囦护缂撳瓨澶у皬: %d, 杞闂撮殧: %dms",
- queryCommandCache.size(), pollingInterval);
- }
+ 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彂閫佹煡璇㈡寚浠�
+
+ public static boolean isSendChaxunzhiling() {
+ return sendChaxunzhiling;
+ }
+
+ public static void setSendChaxunzhiling(boolean sendChaxunzhiling) {
+ lunxun.sendChaxunzhiling = sendChaxunzhiling;
+ }
+
+ // 鍗℃Ы鐩稿叧甯搁噺
+ 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<>();
+
+ // 璋冭瘯妯″紡鎺у埗
+ 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<>(); // 璁板綍姣忎釜鍗℃Ы鐨勬渶鍚庢煡璇㈡椂闂�
+
+ /**
+ * 妫�鏌ヤ覆鍙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 {
+ Errlog.logOperation("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
+ serialConnected = false;
+ }
+ } catch (Exception e) {
+ Errlog.logOperation("涓插彛杩炴帴妫�鏌ュ紓甯�: " + 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()) {
+ Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
+ return false;
+ }
+
+ // 浠庨厤缃腑鑾峰彇杞闂撮殧
+ 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) {
+ Errlog.logOperation("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + 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()) {
+ Errlog.logOperation("杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
+ // 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
+ pollingThread.setDaemon(true);
+ }
+ } catch (InterruptedException e) {
+ Errlog.logOperation("鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
+ Thread.currentThread().interrupt();
+ } catch (Exception e) {
+ Errlog.logOperation("鍋滄杞绾跨▼鏃跺彂鐢熷紓甯�: " + 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()) {
+ Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+ 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) {
+ Errlog.logOperation("杞闂撮殧涓嶈兘灏忎簬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) {
+ Errlog.logOperation("鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + 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; // 鏈�澶ц繛缁け璐ユ鏁�
+
+ @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()) {
+ Errlog.logOperation("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
+ pausePolling();
+ continue;
+ }
+
+ // 鑾峰彇鍗℃Ы鏁扮粍
+ Fkj[] slotArray = SlotManager.getSlotArray();
+ if (slotArray == null || slotArray.length == 0) {
+ Errlog.logOperation("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
+ 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 = "鏃犲崱";
+ }
+ SystemDebugDialog.appendAsciiData(
+ String.format("Slot %d (%s) 鏌ヨ鎴愬姛锛岄棿闅�: %dms\n",
+ slotNumber, status, queryInterval));
+ }
+ } else {
+ consecutiveFailures++;
+ if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
+ Errlog.logOperation("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) {
+ Errlog.logOperation("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + 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 {
+ Errlog.logOperation("鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+ return false;
+ }
+
+ } catch (Exception e) {
+ Errlog.logOperation("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + 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) {
+ Errlog.logOperation("鍗℃Ы缂栧彿蹇呴』鍦�" + MIN_SLOT + "-" + MAX_SLOT + "涔嬮棿");
+ return false;
+ }
+
+ // 妫�鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
+ 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 {
+ Errlog.logOperation("绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
+ return false;
+ }
+ } else {
+ Errlog.logOperation("绔嬪嵆鏌ヨ澶辫触 - 鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+ return false;
+ }
+
+ } catch (Exception e) {
+ Errlog.logOperation("绔嬪嵆鏌ヨ鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ return false;
+ }
+ }
+
+ /**
+ * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�- 浼樺寲鐗堟湰
+ * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
+ */
+ public static int sendImmediateQueryToAll() {
+ // 妫�鏌ヤ覆鍙h繛鎺�
+ if (!checkSerialConnectionWithRetry()) {
+ Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
+ 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 {
+ Errlog.logOperation("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 鏈繛鎺�");
+ // 濡傛灉涓插彛鏂紑涓旇疆璇㈡鍦ㄨ繍琛岋紝鑷姩鏆傚仠杞
+ 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)",
+ status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT,
+ noCardCount, hasCardCount);
+ }
+
+ /**
+ * 鐩存帴璁剧疆杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
+ * @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()) {
+ Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+ 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) {
+ Errlog.logOperation("鏃犲崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬10ms");
+ return;
+ }
+ // 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+ //System.out.println("鏃犲崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+ }
+
+ /**
+ * 璁剧疆鏈夊崱鍗℃Ы鏌ヨ闂撮殧
+ * @param interval 鏌ヨ闂撮殧锛堟绉掞級
+ */
+ public static void setHasCardQueryInterval(int interval) {
+ if (interval < 1000) {
+ Errlog.logOperation("鏈夊崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬1000ms");
+ return;
+ }
+ // 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+ //System.out.println("鏈夊崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+ }
public static boolean isDEBUG_ENABLED() {
return DEBUG_ENABLED;
--
Gitblit v1.9.3