From 03b0fb0ba2de86bcfff277778826547c0e37a93f Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 20 十一月 2025 23:52:51 +0800
Subject: [PATCH] 优化修改
---
src/xitongshezhi/canshushezhi.java | 236 +++----
bin/xitongshezhi/canshushezhi$1.class | 0
bin/home/Homein.class | 0
src/chushihua/lunxunzaixian.java | 966 ++++++++++++++++---------------
src/chuankou/Sendmsg.java | 104 ++-
src/chushihua/lunxun.java | 9
src/home/Homein.java | 20
bin/chushihua/lunxun.class | 0
bin/chushihua/lunxun$PollingTask.class | 0
src/chuankou/SerialPortService.java | 439 +++++++-------
bin/chuankou/SerialPortService.class | 0
bin/chuankou/Sendmsg.class | 0
bin/xitongshezhi/canshushezhi$2.class | 0
bin/xitongshezhi/canshushezhi.class | 0
14 files changed, 905 insertions(+), 869 deletions(-)
diff --git a/bin/chuankou/Sendmsg.class b/bin/chuankou/Sendmsg.class
index 1b419fa..9ae930b 100644
--- a/bin/chuankou/Sendmsg.class
+++ b/bin/chuankou/Sendmsg.class
Binary files differ
diff --git a/bin/chuankou/SerialPortService.class b/bin/chuankou/SerialPortService.class
index 0929610..0f128ec 100644
--- a/bin/chuankou/SerialPortService.class
+++ b/bin/chuankou/SerialPortService.class
Binary files differ
diff --git a/bin/chushihua/lunxun$PollingTask.class b/bin/chushihua/lunxun$PollingTask.class
index 2aa1f2e..f685c69 100644
--- a/bin/chushihua/lunxun$PollingTask.class
+++ b/bin/chushihua/lunxun$PollingTask.class
Binary files differ
diff --git a/bin/chushihua/lunxun.class b/bin/chushihua/lunxun.class
index 1f744aa..2f8cacf 100644
--- a/bin/chushihua/lunxun.class
+++ b/bin/chushihua/lunxun.class
Binary files differ
diff --git a/bin/home/Homein.class b/bin/home/Homein.class
index c6935c0..e2e5dc8 100644
--- a/bin/home/Homein.class
+++ b/bin/home/Homein.class
Binary files differ
diff --git a/bin/xitongshezhi/canshushezhi$1.class b/bin/xitongshezhi/canshushezhi$1.class
index c1c8916..ad7f73c 100644
--- a/bin/xitongshezhi/canshushezhi$1.class
+++ b/bin/xitongshezhi/canshushezhi$1.class
Binary files differ
diff --git a/bin/xitongshezhi/canshushezhi$2.class b/bin/xitongshezhi/canshushezhi$2.class
index 83abcd0..55ec1b2 100644
--- a/bin/xitongshezhi/canshushezhi$2.class
+++ b/bin/xitongshezhi/canshushezhi$2.class
Binary files differ
diff --git a/bin/xitongshezhi/canshushezhi.class b/bin/xitongshezhi/canshushezhi.class
index 3f3fb1a..3c3fa77 100644
--- a/bin/xitongshezhi/canshushezhi.class
+++ b/bin/xitongshezhi/canshushezhi.class
Binary files differ
diff --git a/src/chuankou/Sendmsg.java b/src/chuankou/Sendmsg.java
index d97f1d8..6479155 100644
--- a/src/chuankou/Sendmsg.java
+++ b/src/chuankou/Sendmsg.java
@@ -11,8 +11,8 @@
private static volatile SerialPortService serialService = null;
private static volatile boolean isPortOpen = false;
- // 璋冭瘯妯″紡寮�鍏�
- private static final boolean DEBUG_MODE = false;
+ // 鏀逛负闈瀎inal锛屾敮鎸佸姩鎬佹帶鍒�
+ private static boolean DEBUG_MODE = false;
// 鏃ユ湡鏍煎紡鍖�
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
@@ -35,48 +35,65 @@
}
/**
- * 鍙戦�佹秷鎭埌涓插彛
- * @param message 瑕佸彂閫佺殑HEX鏍煎紡娑堟伅
- * @return 鍙戦�佹垚鍔熻繑鍥瀟rue锛屽け璐ヨ繑鍥瀎alse
+ * 鍙戦�佹秷鎭埌涓插彛锛堝甫閲嶈瘯鏈哄埗锛�
*/
public static boolean sendMessage(String message) {
if (!isPortOpen || serialService == null) {
- if (DEBUG_MODE) {
- System.err.println("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
- }
+ System.err.println("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
return false;
}
if (message == null || message.trim().isEmpty()) {
- if (DEBUG_MODE) {
- System.err.println("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
- }
+ System.err.println("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
return false;
}
String text = message.trim();
+ int retryCount = 0;
+ final int MAX_RETRY = 2;
- try {
- // HEX鏍煎紡鍙戦��
- byte[] data = hexStringToByteArray(text);
- if (data != null && serialService.send(data)) {
- // 楂橀璋冪敤鏃堕伩鍏嶉绻佺殑鏃ュ織杈撳嚭锛屽彧鍦ㄨ皟璇曟椂璁板綍
- if (DEBUG_MODE) {
- System.out.println("[" + getCurrentTime() + "] 鍙戦��: " + text.toUpperCase());
+ while (retryCount <= MAX_RETRY) {
+ try {
+ byte[] data = hexStringToByteArray(text);
+ if (data == null) {
+ System.err.println("[" + getCurrentTime() + "] HEX杞崲澶辫触锛屾暟鎹�: " + text);
+ return false;
}
- return true;
- } else {
- if (DEBUG_MODE) {
- System.err.println("[" + getCurrentTime() + "] 鏁版嵁鍙戦�佸け璐�");
+
+ boolean sendResult = serialService.send(data);
+
+ if (sendResult) {
+ if (DEBUG_MODE) {
+ System.out.println("[" + getCurrentTime() + "] 鍙戦�佹垚鍔�: " + text.toUpperCase());
+ }
+ return true;
+ } else {
+ retryCount++;
+ if (retryCount <= MAX_RETRY) {
+ System.err.println("[" + getCurrentTime() + "] 鍙戦�佸け璐ワ紝姝e湪閲嶈瘯 (" + retryCount + "/" + MAX_RETRY + ")");
+ try {
+ Thread.sleep(50); // 閲嶈瘯鍓嶇瓑寰�
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ break;
+ }
+ } else {
+ System.err.println("[" + getCurrentTime() + "] 涓插彛鍙戦�佸け璐ワ紝鎸囦护: " + text.toUpperCase());
+ System.err.println("[" + getCurrentTime() + "] 涓插彛鐘舵�� - 鎵撳紑: " + isPortOpen + ", 鏈嶅姟: " + (serialService != null));
+ if (serialService != null) {
+ System.err.println("[" + getCurrentTime() + "] 涓插彛鏈嶅姟鐘舵�� - 鏄惁鎵撳紑: " + serialService.isOpen());
+ }
+ return false;
+ }
}
+ } catch (Exception e) {
+ System.err.println("[" + getCurrentTime() + "] 鍙戦�佸紓甯革紝鎸囦护: " + text.toUpperCase());
+ e.printStackTrace();
return false;
}
- } catch (Exception e) {
- if (DEBUG_MODE) {
- System.err.println("[" + getCurrentTime() + "] 鏁版嵁鏍煎紡閿欒: " + e.getMessage());
- }
- return false;
}
+
+ return false;
}
/**
@@ -84,7 +101,11 @@
* @return 涓插彛鎵撳紑鐘舵��
*/
public static boolean isPortOpen() {
- return isPortOpen && serialService != null;
+ boolean open = isPortOpen && serialService != null;
+ if (!open && DEBUG_MODE) {
+ System.err.println("[" + getCurrentTime() + "] 涓插彛鐘舵�佹鏌�: 鏈墦寮�");
+ }
+ return open;
}
/**
@@ -100,7 +121,7 @@
s = s.replaceAll("\\s+", ""); // 绉婚櫎绌烘牸
int len = s.length();
if (len % 2 != 0) {
- throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟");
+ throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟锛屽綋鍓嶉暱搴�: " + len + ", 鏁版嵁: " + s);
}
byte[] data = new byte[len / 2];
@@ -109,7 +130,7 @@
int low = Character.digit(s.charAt(i + 1), 16);
if (high == -1 || low == -1) {
- throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: " + s.charAt(i) + s.charAt(i + 1));
+ throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: '" + s.charAt(i) + s.charAt(i + 1) + "' 鍦ㄤ綅缃� " + i + "-" + (i+1) + ", 瀹屾暣鏁版嵁: " + s);
}
data[i / 2] = (byte) ((high << 4) + low);
@@ -144,12 +165,25 @@
/**
* 鍚敤璋冭瘯妯″紡
- * 娉ㄦ剰锛氳繖浼氫慨鏀归潤鎬乫inal鍙橀噺锛屽疄闄呴」鐩腑涓嶅缓璁繖鏍峰仛
- * 杩欓噷鍙槸婕旂ず锛屽疄闄呭簲璇ラ�氳繃閰嶇疆鏂囦欢鎺у埗
*/
public static void enableDebugMode() {
- // 鍦ㄥ疄闄呴」鐩腑锛屽簲璇ヤ娇鐢ㄥ彲閰嶇疆鐨勬柟寮忚�屼笉鏄慨鏀筬inal鍙橀噺
- // 杩欓噷鍙槸绀烘剰锛屽疄闄呬娇鐢ㄦ椂闇�瑕侀噸鏂拌璁�
- System.out.println("璋冭瘯妯″紡宸插惎鐢�");
+ DEBUG_MODE = true;
+ System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸插惎鐢�");
+ }
+
+ /**
+ * 绂佺敤璋冭瘯妯″紡
+ */
+ public static void disableDebugMode() {
+ DEBUG_MODE = false;
+ System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸茬鐢�");
+ }
+
+ /**
+ * 璁剧疆璋冭瘯妯″紡
+ */
+ public static void setDebugMode(boolean debug) {
+ DEBUG_MODE = debug;
+ System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡: " + (debug ? "鍚敤" : "绂佺敤"));
}
}
\ No newline at end of file
diff --git a/src/chuankou/SerialPortService.java b/src/chuankou/SerialPortService.java
index 8ec5663..7efeb68 100644
--- a/src/chuankou/SerialPortService.java
+++ b/src/chuankou/SerialPortService.java
@@ -10,234 +10,249 @@
public class SerialPortService {
- private SerialPort port;
- private volatile boolean capturing = false;
- private volatile boolean paused = true;
- private Thread readerThread;
- private Consumer<byte[]> responseConsumer;
-
- // 浼樺寲锛氶噸鐢ㄧ紦鍐插尯锛屽噺灏戝唴瀛樺垎閰�
- private byte[] readBuffer = new byte[200];
- private ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
- private Consumer<byte[]> dataReceivedCallback;
-
- // 鏂板锛氬崗璁В鏋愬櫒寮曠敤
- private SerialProtocolParser protocolParser = new SerialProtocolParser();
-
- // 鏂板锛氭暟鎹潯鏁拌鏁板櫒
- public static int receivedDataCount = 0;
+ private SerialPort port;
+ private volatile boolean capturing = false;
+ private volatile boolean paused = true;
+ private Thread readerThread;
+ private Consumer<byte[]> responseConsumer;
- // 鍏朵粬鐜版湁鏂规硶淇濇寔涓嶅彉...
-
- /**
- * 鑾峰彇涓插彛鎺ユ敹鐨勬暟鎹潯鏁�
- * 褰撴潯鏁拌秴杩�1涓囨椂鑷姩浠�1寮�濮嬮噸鏂拌鏁�
- * @return 鏁版嵁鏉℃暟瀛楃涓�
- */
- public static String getReceivedDataCount() {
- receivedDataCount++;
- if (receivedDataCount > 10000) {
- receivedDataCount = 1;
- }
- return String.valueOf(receivedDataCount);
- }
-
- public static void setReceivedDataCount(int receivedDataCount) {
- SerialPortService.receivedDataCount = receivedDataCount;
- }
-
- /**
- * 鑾峰彇鍗忚瑙f瀽鍣ㄥ疄渚�
- */
- public SerialProtocolParser getProtocolParser() {
- return protocolParser;
- }
+ // 浼樺寲锛氶噸鐢ㄧ紦鍐插尯锛屽噺灏戝唴瀛樺垎閰�
+ private byte[] readBuffer = new byte[200];
+ private ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
+ private Consumer<byte[]> dataReceivedCallback;
+
+ // 鏂板锛氬崗璁В鏋愬櫒寮曠敤
+ private SerialProtocolParser protocolParser = new SerialProtocolParser();
+
+ // 鏂板锛氭暟鎹潯鏁拌鏁板櫒
+ public static int receivedDataCount = 0;
+
+ // 鍏朵粬鐜版湁鏂规硶淇濇寔涓嶅彉...
/**
- * 閲嶇疆鏁版嵁鏉℃暟璁℃暟鍣�
- */
- public void resetReceivedDataCount() {
- receivedDataCount = 0;
- }
+ * 鑾峰彇涓插彛鎺ユ敹鐨勬暟鎹潯鏁�
+ * 褰撴潯鏁拌秴杩�1涓囨椂鑷姩浠�1寮�濮嬮噸鏂拌鏁�
+ * @return 鏁版嵁鏉℃暟瀛楃涓�
+ */
+ public static String getReceivedDataCount() {
+ receivedDataCount++;
+ if (receivedDataCount > 10000) {
+ receivedDataCount = 1;
+ }
+ return String.valueOf(receivedDataCount);
+ }
- // 浠ヤ笅涓哄師鏈変唬鐮侊紝淇濇寔涓嶅彉...
- public InputStream getInputStream() {
- if (port != null && port.isOpen()) {
- return port.getInputStream();
- }
- return null;
- }
+ public static void setReceivedDataCount(int receivedDataCount) {
+ SerialPortService.receivedDataCount = receivedDataCount;
+ }
- public OutputStream getOutputStream() {
- if (port != null && port.isOpen()) {
- return port.getOutputStream();
- }
- return null;
- }
-
- public void setComPortTimeouts(int timeoutMode, int readTimeout, int writeTimeout) {
- if (port != null && port.isOpen()) {
- port.setComPortTimeouts(timeoutMode, readTimeout, writeTimeout);
- }
- }
-
- /**
- * 璁剧疆鍗忚瑙f瀽鍣�
- */
- public void setProtocolParser(SerialProtocolParser parser) {
- this.protocolParser = parser;
- }
-
- /**
- * 鍚敤璋冭瘯杈撳嚭锛屽皢鎺ユ敹鍒扮殑鏁版嵁鎵撳嵃鍒版帶鍒跺彴
- */
- public void enableDebugOutput() {
- //System.out.println("涓插彛璋冭瘯杈撳嚭宸插惎鐢� - 寮�濮嬬洃鍚覆鍙f暟鎹�...");
- }
+ /**
+ * 鑾峰彇鍗忚瑙f瀽鍣ㄥ疄渚�
+ */
+ public SerialProtocolParser getProtocolParser() {
+ return protocolParser;
+ }
- /**
- * 鑾峰彇褰撳墠璋冭瘯鐘舵��
- */
- public boolean isDebugEnabled() {
- return capturing;
- }
-
- public void startCapture() {
- if (dataReceivedCallback != null) {
- startCapture(dataReceivedCallback);
- } else {
- System.err.println("No data received callback set. Please call startCapture(Consumer<byte[]> onReceived) first.");
- }
- }
+ /**
+ * 閲嶇疆鏁版嵁鏉℃暟璁℃暟鍣�
+ */
+ public void resetReceivedDataCount() {
+ receivedDataCount = 0;
+ }
- /**
- * 鎵撳紑涓插彛
- */
- public boolean open(String portName, int baud) {
- if (port != null && port.isOpen()) {
- return true;
- }
+ // 浠ヤ笅涓哄師鏈変唬鐮侊紝淇濇寔涓嶅彉...
+ public InputStream getInputStream() {
+ if (port != null && port.isOpen()) {
+ return port.getInputStream();
+ }
+ return null;
+ }
- port = SerialPort.getCommPort(portName);
- port.setComPortParameters(baud, 8, 1, SerialPort.NO_PARITY);
- port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 1, 0);
- protocolParser.start();
- return port.openPort();
- }
-
- public void setResponseConsumer(Consumer<byte[]> consumer) {
- this.responseConsumer = consumer;
- }
+ public OutputStream getOutputStream() {
+ if (port != null && port.isOpen()) {
+ return port.getOutputStream();
+ }
+ return null;
+ }
- /**
- * 鍏抽棴涓插彛
- */
- public void close() {
- stopCapture();
- if (port != null && port.isOpen()) {
- port.closePort();
- }
- port = null;
- }
+ public void setComPortTimeouts(int timeoutMode, int readTimeout, int writeTimeout) {
+ if (port != null && port.isOpen()) {
+ port.setComPortTimeouts(timeoutMode, readTimeout, writeTimeout);
+ }
+ }
- /**
- * 鍚姩鏁版嵁鎺ユ敹绾跨▼
- */
- public void startCapture(Consumer<byte[]> onReceived) {
- this.dataReceivedCallback = onReceived;
- if (capturing || port == null || !port.isOpen()) return;
- capturing = true;
- paused = false;
+ /**
+ * 璁剧疆鍗忚瑙f瀽鍣�
+ */
+ public void setProtocolParser(SerialProtocolParser parser) {
+ this.protocolParser = parser;
+ }
- readerThread = new Thread(() -> {
- buffer.reset();
- long lastReceivedTime = 0;
-
- while (capturing && port.isOpen()) {
- long currentTime = System.currentTimeMillis();
-
- if (buffer.size() > 0 && (currentTime - lastReceivedTime) >= 20) {
- byte[] data = buffer.toByteArray();
- SystemDebugDialog.appendHexData(data);
-
- // 鏂板锛氬皢鏁版嵁浼犻�掔粰鍗忚瑙f瀽鍣�
- if (protocolParser != null) {
- protocolParser.receiveData(data);
- }
-
- if (!paused) {
- onReceived.accept(data);
- if (responseConsumer != null) {
- responseConsumer.accept(data);
- }
- }
- buffer.reset();
- }
+ /**
+ * 鍚敤璋冭瘯杈撳嚭锛屽皢鎺ユ敹鍒扮殑鏁版嵁鎵撳嵃鍒版帶鍒跺彴
+ */
+ public void enableDebugOutput() {
+ //System.out.println("涓插彛璋冭瘯杈撳嚭宸插惎鐢� - 寮�濮嬬洃鍚覆鍙f暟鎹�...");
+ }
- int len = port.readBytes(readBuffer, readBuffer.length);
- currentTime = System.currentTimeMillis();
+ /**
+ * 鑾峰彇褰撳墠璋冭瘯鐘舵��
+ */
+ public boolean isDebugEnabled() {
+ return capturing;
+ }
- if (len > 0) {
- buffer.write(readBuffer, 0, len);
- lastReceivedTime = currentTime;
- }
-
- if (len <= 0 && buffer.size() == 0) {
- try { Thread.sleep(1); } catch (InterruptedException ignore) {}
- }
- }
-
- if (buffer.size() > 0) {
- byte[] data = buffer.toByteArray();
- SystemDebugDialog.appendHexData(data);
-
- // 鏂板锛氬皢鏁版嵁浼犻�掔粰鍗忚瑙f瀽鍣�
- if (protocolParser != null) {
- protocolParser.receiveData(data);
- }
-
- if (!paused) {
- onReceived.accept(data);
- if (responseConsumer != null) {
- responseConsumer.accept(data);
- }
- }
- }
- });
- readerThread.setDaemon(true);
- readerThread.start();
- }
+ public void startCapture() {
+ if (dataReceivedCallback != null) {
+ startCapture(dataReceivedCallback);
+ } else {
+ System.err.println("No data received callback set. Please call startCapture(Consumer<byte[]> onReceived) first.");
+ }
+ }
- /**
- * 鍋滄鏁版嵁鎺ユ敹绾跨▼
- */
- public void stopCapture() {
- capturing = false;
- if (readerThread != null) {
- try { readerThread.join(500); } catch (InterruptedException ignore) {}
- readerThread = null;
- }
- }
+ /**
+ * 鎵撳紑涓插彛
+ */
+ public boolean open(String portName, int baud) {
+ if (port != null && port.isOpen()) {
+ return true;
+ }
- public void setPaused(boolean paused) {
- this.paused = paused;
- }
+ port = SerialPort.getCommPort(portName);
+ port.setComPortParameters(baud, 8, 1, SerialPort.NO_PARITY);
+ port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 1, 0);
+ protocolParser.start();
+ return port.openPort();
+ }
- public boolean isPaused() {
- return paused;
- }
+ public void setResponseConsumer(Consumer<byte[]> consumer) {
+ this.responseConsumer = consumer;
+ }
- public boolean isOpen() {
- return port != null && port.isOpen();
- }
+ /**
+ * 鍏抽棴涓插彛
+ */
+ public void close() {
+ stopCapture();
+ if (port != null && port.isOpen()) {
+ port.closePort();
+ }
+ port = null;
+ }
- /**
- * 鍙戦�佹暟鎹�
- */
- public boolean send(byte[] data) {
- if (!isOpen()) {
- return false;
- }
- return port != null && port.isOpen() && port.writeBytes(data, data.length) > 0;
- }
+ /**
+ * 鍚姩鏁版嵁鎺ユ敹绾跨▼
+ */
+ public void startCapture(Consumer<byte[]> onReceived) {
+ this.dataReceivedCallback = onReceived;
+ if (capturing || port == null || !port.isOpen()) return;
+ capturing = true;
+ paused = false;
+
+ readerThread = new Thread(() -> {
+ buffer.reset();
+ long lastReceivedTime = 0;
+
+ while (capturing && port.isOpen()) {
+ long currentTime = System.currentTimeMillis();
+
+ if (buffer.size() > 0 && (currentTime - lastReceivedTime) >= 20) {
+ byte[] data = buffer.toByteArray();
+ SystemDebugDialog.appendHexData(data);
+
+ // 鏂板锛氬皢鏁版嵁浼犻�掔粰鍗忚瑙f瀽鍣�
+ if (protocolParser != null) {
+ protocolParser.receiveData(data);
+ }
+
+ if (!paused) {
+ onReceived.accept(data);
+ if (responseConsumer != null) {
+ responseConsumer.accept(data);
+ }
+ }
+ buffer.reset();
+ }
+
+ int len = port.readBytes(readBuffer, readBuffer.length);
+ currentTime = System.currentTimeMillis();
+
+ if (len > 0) {
+ buffer.write(readBuffer, 0, len);
+ lastReceivedTime = currentTime;
+ }
+
+ if (len <= 0 && buffer.size() == 0) {
+ try { Thread.sleep(1); } catch (InterruptedException ignore) {}
+ }
+ }
+
+ if (buffer.size() > 0) {
+ byte[] data = buffer.toByteArray();
+ SystemDebugDialog.appendHexData(data);
+
+ // 鏂板锛氬皢鏁版嵁浼犻�掔粰鍗忚瑙f瀽鍣�
+ if (protocolParser != null) {
+ protocolParser.receiveData(data);
+ }
+
+ if (!paused) {
+ onReceived.accept(data);
+ if (responseConsumer != null) {
+ responseConsumer.accept(data);
+ }
+ }
+ }
+ });
+ readerThread.setDaemon(true);
+ readerThread.start();
+ }
+
+ /**
+ * 鍋滄鏁版嵁鎺ユ敹绾跨▼
+ */
+ public void stopCapture() {
+ capturing = false;
+ if (readerThread != null) {
+ try { readerThread.join(500); } catch (InterruptedException ignore) {}
+ readerThread = null;
+ }
+ }
+
+ public void setPaused(boolean paused) {
+ this.paused = paused;
+ }
+
+ public boolean isPaused() {
+ return paused;
+ }
+
+ public boolean isOpen() {
+ return port != null && port.isOpen();
+ }
+ /**
+ * 鍙戦�佹暟鎹紙浼樺寲鐗堟湰锛�
+ */
+ public boolean send(byte[] data) {
+ if (!isOpen()) {
+ return false;
+ }
+
+ // 娣诲姞鍙戦�佸墠鐨勪覆鍙g姸鎬佹鏌�
+ if (port == null || !port.isOpen()) {
+ return false;
+ }
+
+ try {
+ // 娣诲姞灏忓欢杩燂紝閬垮厤杩炵画鍙戦��
+ Thread.sleep(2);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return false;
+ }
+
+ int result = port.writeBytes(data, data.length);
+ return result > 0;
+ }
+
}
\ No newline at end of file
diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index f7a66ee..8d08dff 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -115,10 +115,7 @@
try {
pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
pollingThread.setDaemon(true);
- pollingThread.start();
- if (DEBUG_ENABLED) {
- //System.out.println("杞鏌ヨ宸插惎鍔紝闂撮殧: " + pollingInterval + "ms");
- }
+ pollingThread.start();
return true;
} catch (Exception e) {
System.err.println("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
@@ -387,7 +384,7 @@
} else {
consecutiveFailures++;
if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
- System.err.println("杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
+ System.err.println("lunxun杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
pausePolling();
break;
}
@@ -442,7 +439,7 @@
if (sendResult) {
// 鍙湪璋冭瘯鏃惰緭鍑猴紝閬垮厤棰戠箒鎵撳嵃
if (DEBUG_ENABLED) {
- SystemDebugDialog.appendAsciiData(String.format("Slot %d Send query (hasCard !=1)", slotNumber));
+ SystemDebugDialog.appendAsciiData(String.format("Slot %d Send query (hasCard !=1)\n", slotNumber));
}
return true;
} else {
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
diff --git a/src/home/Homein.java b/src/home/Homein.java
index cecbe3d..447b82e 100644
--- a/src/home/Homein.java
+++ b/src/home/Homein.java
@@ -129,21 +129,13 @@
if (serialConnected) {
// 4. 涓插彛杩炴帴鎴愬姛鍚庯紝鍚姩杞
- boolean pollingStarted = startPollingService();
+ startPollingService();
+ showMainInterface();
+ //鍚姩杞鍗$姸鎬佺粰鏈嶅姟鍣ㄥ彂鏁版嵁
+ lunxunkazhuangtai.startPolling();
+ //鍚姩鍦ㄧ嚎鐨勫崱鐘舵�佽疆璇�
+ lunxunzaixian.startOnlinePolling();
- if (pollingStarted) {
- showMainInterface();
- //鍚姩杞鍗$姸鎬佺粰鏈嶅姟鍣ㄥ彂鏁版嵁
- lunxunkazhuangtai.startPolling();
- //鍚姩鍦ㄧ嚎鐨勫崱鐘舵�佽疆璇�
- lunxunzaixian.startOnlinePolling();
- } else {
- System.err.println("杞鏈嶅姟鍚姩澶辫触");
- JOptionPane.showMessageDialog(null,
- "杞鏈嶅姟鍚姩澶辫触锛屽簲鐢ㄧ▼搴忔棤娉曟甯歌繍琛�",
- "璀﹀憡",
- JOptionPane.WARNING_MESSAGE);
- }
} else {
System.err.println("涓插彛杩炴帴澶辫触");
// 涓插彛杩炴帴澶辫触宸茬粡鐢盨erialPortConnectionDialog澶勭悊锛岀洿鎺ラ��鍑�
diff --git a/src/xitongshezhi/canshushezhi.java b/src/xitongshezhi/canshushezhi.java
index c894d24..0b41142 100644
--- a/src/xitongshezhi/canshushezhi.java
+++ b/src/xitongshezhi/canshushezhi.java
@@ -17,12 +17,14 @@
private static final int SCREEN_WIDTH = 600;
private static final int SCREEN_HEIGHT = 1024;
- // 棰滆壊甯搁噺 - 浣跨敤涓嶉�忔槑棰滆壊
+ // 棰滆壊瀹氫箟 - 浣跨敤mimaguanli.java涓殑棰滆壊鏂规
+ private static final Color BACKGROUND_COLOR = new Color(15, 28, 48);
+ private static final Color CARD_COLOR = new Color(26, 43, 68);
private static final Color PRIMARY_COLOR = new Color(52, 152, 219);
private static final Color SECONDARY_COLOR = new Color(46, 204, 113);
- private static final Color DARK_COLOR = new Color(15, 28, 48);
- private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68);
private static final Color TEXT_COLOR = new Color(224, 224, 224);
+ private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255);
+ private static final Color FIELD_BACKGROUND = new Color(240, 240, 240);
// UI缁勪欢
private JPanel mainPanel;
@@ -58,10 +60,10 @@
e.printStackTrace();
}
- // 鍒涘缓涓婚潰鏉� - 涓嶉�忔槑鑳屾櫙
+ // 鍒涘缓涓婚潰鏉� - 浣跨敤mimaguanli鐨勮儗鏅壊
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
- mainPanel.setBackground(DARK_COLOR);
+ mainPanel.setBackground(BACKGROUND_COLOR);
mainPanel.setOpaque(true);
mainPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
@@ -82,14 +84,18 @@
titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22));
titleLabel.setForeground(TEXT_COLOR);
- // 鍏抽棴鎸夐挳
+ // 鍏抽棴鎸夐挳 - 浣跨敤mimaguanli鐨勬寜閽牱寮�
JButton backButton = new JButton("鍏抽棴");
backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
backButton.setBackground(PRIMARY_COLOR);
backButton.setForeground(Color.WHITE);
backButton.setOpaque(true);
backButton.setFocusPainted(false);
- backButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
+ backButton.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(new Color(40, 120, 180), 1),
+ BorderFactory.createEmptyBorder(8, 16, 8, 16)
+ ));
+ backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
backButton.addActionListener(e -> dispose());
// 鍏抽棴鎸夐挳鎮仠鏁堟灉
@@ -130,12 +136,14 @@
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
- // 鍩烘湰鍙傛暟璁剧疆鍗$墖
- settingsPanel.add(createBasicSettingsCard());
- settingsPanel.add(Box.createRigidArea(new Dimension(0, 12)));
+ // 鑷畾涔夋粴鍔ㄦ潯
+ JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
+ verticalScrollBar.setBackground(CARD_COLOR);
+ verticalScrollBar.setForeground(PRIMARY_COLOR);
+ verticalScrollBar.setUnitIncrement(20);
- // 璁惧鍙傛暟璁剧疆鍗$墖
- settingsPanel.add(createDeviceSettingsCard());
+ // 鍚堝苟鐨勫弬鏁拌缃崱鐗�
+ settingsPanel.add(createMergedSettingsCard());
settingsPanel.add(Box.createRigidArea(new Dimension(0, 12)));
// 淇濆瓨鎸夐挳
@@ -148,172 +156,148 @@
return container;
}
- private JPanel createBasicSettingsCard() {
+ private JPanel createMergedSettingsCard() {
JPanel card = new JPanel();
card.setLayout(new BorderLayout());
- card.setBackground(DARK_LIGHT_COLOR);
+ card.setBackground(CARD_COLOR);
card.setOpaque(true);
card.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(52, 152, 219, 128)),
- BorderFactory.createEmptyBorder(15, 15, 15, 15)
+ BorderFactory.createLineBorder(new Color(52, 152, 219, 100), 1),
+ BorderFactory.createEmptyBorder(20, 20, 20, 20)
));
- // 鍗″ご
- JPanel cardHeader = new JPanel(new BorderLayout());
- cardHeader.setOpaque(false);
-
- JLabel titleLabel = new JLabel("鍩烘湰鍙傛暟璁剧疆");
- titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
- titleLabel.setForeground(TEXT_COLOR);
-
- cardHeader.add(titleLabel, BorderLayout.WEST);
- card.add(cardHeader, BorderLayout.NORTH);
-
- // 琛ㄥ崟鍐呭 - 浣跨敤缃戞牸甯冨眬瀹炵幇鏍囩鍦ㄥ乏锛屾枃鏈鍦ㄥ彸
- // 璋冩暣琛岄珮涓�80鍍忕礌锛屼负60鍍忕礌楂樼殑缁勪欢鎻愪緵瓒冲鐨勯棿璺�
- JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 20));
+ // 琛ㄥ崟鍐呭 - 浣跨敤姘村钩甯冨眬瀹炵幇鏍囩鍦ㄥ乏锛屾枃鏈鍦ㄥ彸
+ JPanel formPanel = new JPanel();
+ formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS));
formPanel.setOpaque(false);
- formPanel.setBorder(new EmptyBorder(15, 0, 0, 0));
// 鍙戝崱鏈虹紪鍙�
- JLabel deviceIdLabel = new JLabel("鍙戝崱鏈虹紪鍙�:");
- deviceIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
- deviceIdLabel.setForeground(TEXT_COLOR);
- formPanel.add(deviceIdLabel);
-
- deviceIdField = new JTextField();
- styleFormField(deviceIdField);
- formPanel.add(deviceIdField);
+ formPanel.add(createHorizontalField("鍙戝崱鏈虹紪鍙�:", deviceIdField = new JTextField()));
+ formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
// 鏈嶅姟鍣ㄥ湴鍧�
- JLabel serverAddressLabel = new JLabel("鏈嶅姟鍣ㄥ湴鍧�:");
- serverAddressLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
- serverAddressLabel.setForeground(TEXT_COLOR);
- formPanel.add(serverAddressLabel);
-
- serverAddressField = new JTextField();
- styleFormField(serverAddressField);
- formPanel.add(serverAddressField);
+ formPanel.add(createHorizontalField("鏈嶅姟鍣ㄥ湴鍧�:", serverAddressField = new JTextField()));
+ formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
// 鏈嶅姟鍣ㄧ鍙�
- JLabel serverPortLabel = new JLabel("鏈嶅姟鍣ㄧ鍙�:");
- serverPortLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
- serverPortLabel.setForeground(TEXT_COLOR);
- formPanel.add(serverPortLabel);
+ formPanel.add(createHorizontalField("鏈嶅姟鍣ㄧ鍙�:", serverPortField = new JTextField()));
+ formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
- serverPortField = new JTextField();
- styleFormField(serverPortField);
- formPanel.add(serverPortField);
+ // 鍗℃Ы鎬绘暟
+ formPanel.add(createHorizontalField("鍗℃Ы鎬绘暟:", slotCountField = new JTextField()));
+ formPanel.add(Box.createRigidArea(new Dimension(0, 18)));
+
+ // 璇诲崱鍙锋ā寮�
+ formPanel.add(createHorizontalComboBoxField("璇诲崱鍙锋ā寮�:", readModeComboBox = new JComboBox<>(new String[]{"涓诲姩妯″紡", "琚姩妯″紡"})));
card.add(formPanel, BorderLayout.CENTER);
return card;
}
- private JPanel createDeviceSettingsCard() {
- JPanel card = new JPanel();
- card.setLayout(new BorderLayout());
- card.setBackground(DARK_LIGHT_COLOR);
- card.setOpaque(true);
- card.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(52, 152, 219, 128)),
- BorderFactory.createEmptyBorder(15, 15, 15, 15)
+ private JPanel createHorizontalField(String labelText, JTextField textField) {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBackground(CARD_COLOR);
+ panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
+
+ // 鏍囩
+ JLabel label = new JLabel(labelText);
+ label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
+ label.setForeground(TEXT_COLOR);
+ label.setPreferredSize(new Dimension(100, 30));
+
+ // 鏂囨湰妗嗘牱寮� - 浣跨敤mimaguanli鐨勬牱寮�
+ textField.setBackground(FIELD_BACKGROUND);
+ textField.setForeground(Color.BLACK);
+ textField.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(new Color(200, 200, 200), 1),
+ BorderFactory.createEmptyBorder(10, 12, 10, 12)
));
+ textField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ textField.setOpaque(true);
+ textField.setPreferredSize(new Dimension(280, 42));
- // 鍗″ご
- JPanel cardHeader = new JPanel(new BorderLayout());
- cardHeader.setOpaque(false);
+ // 鍒涘缓姘村钩甯冨眬瀹瑰櫒
+ JPanel horizontalPanel = new JPanel(new BorderLayout());
+ horizontalPanel.setBackground(CARD_COLOR);
+ horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
- JLabel titleLabel = new JLabel("璁惧鍙傛暟璁剧疆");
- titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
- titleLabel.setForeground(TEXT_COLOR);
+ horizontalPanel.add(label, BorderLayout.WEST);
+ horizontalPanel.add(textField, BorderLayout.CENTER);
- cardHeader.add(titleLabel, BorderLayout.WEST);
- card.add(cardHeader, BorderLayout.NORTH);
+ // 娣诲姞闂磋窛
+ horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
- // 琛ㄥ崟鍐呭 - 浣跨敤缃戞牸甯冨眬瀹炵幇鏍囩鍦ㄥ乏锛岃緭鍏ョ粍浠跺湪鍙�
- // 璋冩暣琛岄珮涓�80鍍忕礌锛屼负60鍍忕礌楂樼殑缁勪欢鎻愪緵瓒冲鐨勯棿璺�
- JPanel formPanel = new JPanel(new GridLayout(2, 2, 10, 20));
- formPanel.setOpaque(false);
- formPanel.setBorder(new EmptyBorder(15, 0, 0, 0));
+ return horizontalPanel;
+ }
+
+ private JPanel createHorizontalComboBoxField(String labelText, JComboBox<String> comboBox) {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBackground(CARD_COLOR);
+ panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
- // 鍗℃Ы鎬绘暟
- JLabel slotCountLabel = new JLabel("鍗℃Ы鎬绘暟:");
- slotCountLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
- slotCountLabel.setForeground(TEXT_COLOR);
- formPanel.add(slotCountLabel);
+ // 鏍囩
+ JLabel label = new JLabel(labelText);
+ label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
+ label.setForeground(TEXT_COLOR);
+ label.setPreferredSize(new Dimension(100, 30));
- slotCountField = new JTextField();
- styleFormField(slotCountField);
- formPanel.add(slotCountField);
+ // 缁勫悎妗嗘牱寮� - 浣跨敤mimaguanli鐨勬牱寮�
+ comboBox.setBackground(FIELD_BACKGROUND);
+ comboBox.setForeground(Color.BLACK);
+ comboBox.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(new Color(200, 200, 200), 1),
+ BorderFactory.createEmptyBorder(10, 12, 10, 12)
+ ));
+ comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ comboBox.setOpaque(true);
+ comboBox.setPreferredSize(new Dimension(280, 42));
- // 璇诲崱鍙锋ā寮�
- JLabel readModeLabel = new JLabel("璇诲崱鍙锋ā寮�:");
- readModeLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13));
- readModeLabel.setForeground(TEXT_COLOR);
- formPanel.add(readModeLabel);
+ // 鍒涘缓姘村钩甯冨眬瀹瑰櫒
+ JPanel horizontalPanel = new JPanel(new BorderLayout());
+ horizontalPanel.setBackground(CARD_COLOR);
+ horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50));
- readModeComboBox = new JComboBox<>(new String[]{"涓诲姩妯″紡", "琚姩妯″紡"});
- styleComboBox(readModeComboBox);
- formPanel.add(readModeComboBox);
+ horizontalPanel.add(label, BorderLayout.WEST);
+ horizontalPanel.add(comboBox, BorderLayout.CENTER);
- card.add(formPanel, BorderLayout.CENTER);
+ // 娣诲姞闂磋窛
+ horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
- return card;
+ return horizontalPanel;
}
private JPanel createSaveButton() {
- JPanel buttonPanel = new JPanel(new BorderLayout());
+ JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setOpaque(false);
+ buttonPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
JButton saveButton = new JButton("淇濆瓨鎵�鏈夎缃�");
- saveButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
- saveButton.setBackground(SECONDARY_COLOR);
+ saveButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ saveButton.setBackground(PRIMARY_COLOR);
saveButton.setForeground(Color.WHITE);
saveButton.setOpaque(true);
saveButton.setFocusPainted(false);
- saveButton.setPreferredSize(new Dimension(0, 60)); // 楂樺害鏀逛负60鍍忕礌
- saveButton.setBorder(BorderFactory.createEmptyBorder(12, 20, 12, 20));
+ saveButton.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(new Color(40, 120, 180), 1),
+ BorderFactory.createEmptyBorder(8, 16, 8, 16)
+ ));
+ saveButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
saveButton.addActionListener(e -> saveSettings());
// 鎸夐挳鎮仠鏁堟灉
saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
- saveButton.setBackground(brighterColor(SECONDARY_COLOR));
+ saveButton.setBackground(brighterColor(PRIMARY_COLOR));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
- saveButton.setBackground(SECONDARY_COLOR);
+ saveButton.setBackground(PRIMARY_COLOR);
}
});
- buttonPanel.add(saveButton, BorderLayout.CENTER);
+ buttonPanel.add(saveButton);
return buttonPanel;
- }
-
- private void styleFormField(JTextField field) {
- field.setPreferredSize(new Dimension(200, 60)); // 楂樺害鏀逛负60鍍忕礌
- field.setBackground(Color.WHITE); // 涓嶉�忔槑鐧借壊鑳屾櫙
- field.setForeground(Color.BLACK); // 榛戣壊鏂囧瓧
- field.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(200, 200, 200)),
- BorderFactory.createEmptyBorder(8, 12, 8, 12)
- ));
- field.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
- field.setCaretColor(Color.BLACK);
- field.setOpaque(true); // 纭繚涓嶉�忔槑
- }
-
- private void styleComboBox(JComboBox<String> comboBox) {
- comboBox.setPreferredSize(new Dimension(200, 60)); // 楂樺害鏀逛负60鍍忕礌
- comboBox.setBackground(Color.WHITE); // 涓嶉�忔槑鐧借壊鑳屾櫙
- comboBox.setForeground(Color.BLACK); // 榛戣壊鏂囧瓧
- comboBox.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(200, 200, 200)),
- BorderFactory.createEmptyBorder(8, 12, 8, 12)
- ));
- comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
- comboBox.setOpaque(true); // 纭繚涓嶉�忔槑
}
private void initializeData() {
--
Gitblit v1.9.3