From d22349714c8d199c02f336f90fba841ef8f5cd39 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 17:46:23 +0800
Subject: [PATCH] 优化内存后最终版202511211746
---
src/home/CardMachineUI.java | 119 ++++++++++++++++++++++++++++++++---------------------------
1 files changed, 65 insertions(+), 54 deletions(-)
diff --git a/src/home/CardMachineUI.java b/src/home/CardMachineUI.java
index 12e7f49..caa3f4a 100644
--- a/src/home/CardMachineUI.java
+++ b/src/home/CardMachineUI.java
@@ -13,7 +13,6 @@
import xitongshezhi.AdminLoginDialog;
import xitongshezhi.CardPickupDialog;
import xitongshezhi.ConfigSet;
-import xitongshezhi.Fkj;
import chuankou.SerialPortService;
import chuankou.Sendmsg;
@@ -77,9 +76,12 @@
private JPanel commFaultPanel;
public CardMachineUI() {
- try {
+ try {
// 鍏堝垵濮嬪寲绯荤粺閰嶇疆鍜岀粍浠�
initializeSystem();
+
+ // 鍒濆鍖栦覆鍙hВ鏋愬櫒
+ initializeSerialParser();
// 鐒跺悗鍒濆鍖朥I
initializeUI();
@@ -87,7 +89,7 @@
startUIUpdates(); // UI鍒锋柊瀹氭椂鍣�
startSerialStatusMonitoring(); // 涓插彛鐘舵�佺洃鎺�
- System.out.println("涓荤晫闈㈠垵濮嬪寲瀹屾垚");
+ //System.out.println("涓荤晫闈㈠垵濮嬪寲瀹屾垚");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
@@ -110,7 +112,8 @@
if (serialConnected && lunxun.isPolling() && lunxun.isPaused()) {
// 鍙湁鍦ㄤ笉鍦ㄨ缃〉闈㈡椂鎵嶈嚜鍔ㄦ仮澶�
if (!isInConfigPage()) {
- lunxun.resumePolling();
+// lunxun.resumePolling();
+// System.out.println("灏濊瘯鑷姩鍚姩杞鍔熻兘");
}
}
});
@@ -133,7 +136,7 @@
try {
// 鍒濆鍖栧崱妲界鐞嗗櫒 - 蹇呴』棣栧厛鎵ц
slotManager = new SlotManager();
- System.out.println("鍗℃Ы绠$悊鍣ㄥ垵濮嬪寲瀹屾垚");
+ //System.out.println("鍗℃Ы绠$悊鍣ㄥ垵濮嬪寲瀹屾垚");
// 鐩存帴浣跨敤宸茬粡鍒濆鍖栫殑閰嶇疆绯荤粺
Chushihua configSystem = Chushihua.getInstance();
@@ -156,9 +159,9 @@
PICKUP_PASSWORD = configSystem.getMachineConfig().getFetchCardPassword();
}
- System.out.println("绯荤粺閰嶇疆鍒濆鍖栧畬鎴�");
- System.out.println("绠$悊鍛樺瘑鐮侀暱搴�: " + (ADMIN_PASSWORD != null ? ADMIN_PASSWORD.length() : "null"));
- System.out.println("鍙栧崱瀵嗙爜闀垮害: " + (PICKUP_PASSWORD != null ? PICKUP_PASSWORD.length() : "null"));
+ //System.out.println("绯荤粺閰嶇疆鍒濆鍖栧畬鎴�");
+ //System.out.println("绠$悊鍛樺瘑鐮侀暱搴�: " + (ADMIN_PASSWORD != null ? ADMIN_PASSWORD.length() : "null"));
+ //System.out.println("鍙栧崱瀵嗙爜闀垮害: " + (PICKUP_PASSWORD != null ? PICKUP_PASSWORD.length() : "null"));
} catch (Exception e) {
System.err.println("绯荤粺鍒濆鍖栧紓甯�: " + e.getMessage());
@@ -171,7 +174,7 @@
}
private void initializeUI() {
- setTitle("UWB浜哄憳瀹氫綅鍗″彂鍗℃満绠$悊绯荤粺");
+ setTitle("鏅鸿兘浜鸿劯鍙戝崱鏈虹鐞嗙郴缁�");
setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
@@ -288,8 +291,9 @@
refreshBtn.addActionListener(e -> {
updateCardSlotsDisplay();
updateStatistics();
+ lunxun.resumePolling();
JOptionPane.showMessageDialog(CardMachineUI.this,
- "鎵嬪姩鍒锋柊瀹屾垚锛屽叡鍒锋柊 " + TOTAL_SLOTS + " 涓崱妲�",
+ "鎵嬪姩鍒锋柊瀹屾垚锛屽惎鍔ㄨ疆璇紝鍏卞埛鏂� " + TOTAL_SLOTS + " 涓崱妲�",
"鍒锋柊瀹屾垚", JOptionPane.INFORMATION_MESSAGE);
});
@@ -466,9 +470,6 @@
cardSlotsPanel.removeAll();
slotButtons.clear();
- int updatedSlots = 0;
- int errorSlots = 0;
-
for (int i = 1; i <= TOTAL_SLOTS; i++) {
final int slotId = i;
SlotStatus status = getSlotStatusFromManager(slotId);
@@ -476,10 +477,8 @@
// 娣诲姞璋冭瘯淇℃伅
Fkj slotInfo = slotManager.getSlotInfo(slotId);
if (slotInfo != null) {
- updatedSlots++;
} else {
System.err.println("鍗℃Ы " + slotId + " - 鑾峰彇淇℃伅澶辫触");
- errorSlots++;
}
JButton slotButton = new JButton(String.valueOf(slotId));
@@ -509,7 +508,7 @@
cardSlotsPanel.revalidate();
cardSlotsPanel.repaint();
- System.out.println("鍗℃Ы鏄剧ず鏇存柊瀹屾垚: " + updatedSlots + " 涓垚鍔�, " + errorSlots + " 涓け璐�");
+// //System.out.println("鍗℃Ы鏄剧ず鏇存柊瀹屾垚: " + updatedSlots + " 涓垚鍔�, " + errorSlots + " 涓け璐�");
}
private Color brighterColor(Color color) {
@@ -572,7 +571,6 @@
if (panel.getComponentCount() >= 2) {
JLabel textLabel = (JLabel) panel.getComponent(1);
if (textLabel.getText().startsWith("涓插彛:")) {
- String statusText = lunxun.isSerialConnected() ? "姝e父" : "鏂紑";
Color statusColor = lunxun.isSerialConnected() ?
new Color(52, 152, 219) : new Color(231, 76, 60);
@@ -591,8 +589,9 @@
boolean pickupSuccess = CardPickupDialog.showCardPickup(this, slotId, status);
if (pickupSuccess) {
- // 鍙栧崱鎴愬姛锛岀晫闈㈠凡缁忓湪瀵硅瘽妗嗕腑鏇存柊锛岃繖閲屼笉闇�瑕侀澶栨搷浣�
- System.out.println("鍗℃Ы " + slotId + " 鍙栧崱鎴愬姛");
+ // 鍙栧崱鎴愬姛锛岃皟鐢╟hanggehaska鏂规硶鏀瑰彉鍗℃Ы灞炴��
+ SlotManager.changgehaska(slotId,1); // "1"琛ㄧず绠$悊鍛樻搷浣�
+ //System.out.println("鍗℃Ы " + slotId + " 鍙栧崱鎴愬姛锛屽凡鏇存柊鍗℃Ы鐘舵��");
}
}
@@ -610,7 +609,7 @@
// 鍋滄杞鏌ヨ
if (lunxun.isPolling()) {
lunxun.stopPolling();
- System.out.println("搴旂敤绋嬪簭鍏抽棴锛岃疆璇㈡煡璇㈠凡鍋滄");
+ //System.out.println("搴旂敤绋嬪簭鍏抽棴锛岃疆璇㈡煡璇㈠凡鍋滄");
}
// 鍋滄涓插彛鍗忚瑙f瀽鍣紙鏂板锛�
@@ -637,34 +636,6 @@
return "涓插彛瑙f瀽鍣ㄦ湭鍒濆鍖�";
}
- /**
- * 鍒涘缓缇庡寲鎸夐挳
- */
- private JButton createStyledButton(String text, Color color) {
- JButton button = new JButton(text);
- button.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
- button.setBackground(color);
- button.setForeground(Color.WHITE);
- button.setFocusPainted(false);
- button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
- button.setCursor(new Cursor(Cursor.HAND_CURSOR));
-
- // 榧犳爣鎮仠鏁堟灉
- button.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseEntered(java.awt.event.MouseEvent evt) {
- button.setBackground(brighterColor(color));
- }
-
- public void mouseExited(java.awt.event.MouseEvent evt) {
- button.setBackground(color);
- }
- });
-
- return button;
- }
-
-
-
/**
* 楠岃瘉鍙栧崱瀵嗙爜
*/
@@ -818,7 +789,7 @@
if (lunxun.checkSerialConnection()) {
boolean resumed = lunxun.setPollingPaused(false);
if (resumed) {
- System.out.println("鍒囨崲鍒颁富椤甸潰锛岃疆璇㈡煡璇㈠凡鎭㈠");
+ //System.out.println("鍒囨崲鍒颁富椤甸潰锛岃疆璇㈡煡璇㈠凡鎭㈠");
}
} else {
System.err.println("鍒囨崲鍒颁富椤甸潰锛屼覆鍙f湭杩炴帴锛屼繚鎸佽疆璇㈡殏鍋�");
@@ -829,12 +800,15 @@
/**
* 鍚姩UI鍒锋柊瀹氭椂鍣� - 姣�3绉掑埛鏂颁竴娆�
*/
+ // 鍦╯tartUIUpdates()鏂规硶涓皟鐢�
private void startUIUpdates() {
- uiUpdateTimer = new Timer(3000, e -> {
- updateCardSlotsDisplay();
- updateStatistics();
- });
- uiUpdateTimer.start();
+ uiUpdateTimer = new Timer(3000, e -> {
+ ensurePollingRunning(); // 纭繚杞杩愯
+ ensureSerialParserRunning(); // 纭繚涓插彛瑙f瀽鍣ㄨ繍琛�
+ updateCardSlotsDisplay();
+ updateStatistics();
+ });
+ uiUpdateTimer.start();
}
/**
@@ -862,5 +836,42 @@
public SlotManager getSlotManager() {
return slotManager;
}
+
+ /**
+ * 妫�鏌ュ苟纭繚杞鏌ヨ姝e父杩愯
+ */
+ private void ensurePollingRunning() {
+ if (!lunxun.isPolling() && lunxun.checkSerialConnection()) {
+ // 濡傛灉杞鏈繍琛屼絾涓插彛宸茶繛鎺ワ紝鑷姩鍚姩杞
+ boolean started = lunxun.startPolling();
+ if (started) {
+ System.out.println("妫�娴嬪埌杞鏈繍琛岋紝宸茶嚜鍔ㄥ惎鍔�");
+ }
+ }
+ }
+
+ /**
+ * 纭繚涓插彛瑙f瀽鍣ㄦ甯歌繍琛�
+ */
+ private void ensureSerialParserRunning() {
+ // 濡傛灉涓插彛瑙f瀽鍣ㄦ湭杩愯浣嗕覆鍙e凡杩炴帴锛岃嚜鍔ㄥ惎鍔�
+ if (serialProtocolParser != null && !serialProtocolParser.isRunning() && lunxun.checkSerialConnection()) {
+ serialProtocolParser.start();
+ //System.out.println("妫�娴嬪埌涓插彛瑙f瀽鍣ㄦ湭杩愯锛屽凡鑷姩鍚姩");
+ }
+ }
+
+ /**
+ * 鍒濆鍖栦覆鍙hВ鏋愬櫒
+ */
+ private void initializeSerialParser() {
+ try {
+ serialProtocolParser = new SerialProtocolParser();
+ serialProtocolParser.start();
+ //System.out.println("涓插彛鍗忚瑙f瀽鍣ㄥ凡鍚姩");
+ } catch (Exception e) {
+ System.err.println("鍒濆鍖栦覆鍙hВ鏋愬櫒澶辫触: " + e.getMessage());
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3