From 7be65a0428a4527889b6955c56aafdb81dda28a8 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期六, 22 十一月 2025 11:20:39 +0800
Subject: [PATCH] 开启状态线程,优化结束程序逻辑
---
src/home/CardMachineUI.java | 215 +++++++++++++++++++++++++++++++++--------------------
1 files changed, 133 insertions(+), 82 deletions(-)
diff --git a/src/home/CardMachineUI.java b/src/home/CardMachineUI.java
index 12e7f49..4479e23 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,17 +76,27 @@
private JPanel commFaultPanel;
public CardMachineUI() {
- try {
+ try {
// 鍏堝垵濮嬪寲绯荤粺閰嶇疆鍜岀粍浠�
initializeSystem();
+
+ // 鍒濆鍖栦覆鍙hВ鏋愬櫒
+ initializeSerialParser();
// 鐒跺悗鍒濆鍖朥I
initializeUI();
initializeSlots();
startUIUpdates(); // UI鍒锋柊瀹氭椂鍣�
- startSerialStatusMonitoring(); // 涓插彛鐘舵�佺洃鎺�
+ startSerialStatusMonitoring(); // 涓插彛鐘舵�佺洃鎺�
+ setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+ addWindowListener(new java.awt.event.WindowAdapter() {
+ @Override
+ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
+ dispose(); // 璋冪敤鎴戜滑淇敼杩囩殑dispose鏂规硶
+ }
+ });
- System.out.println("涓荤晫闈㈠垵濮嬪寲瀹屾垚");
+ //System.out.println("涓荤晫闈㈠垵濮嬪寲瀹屾垚");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
@@ -110,7 +119,8 @@
if (serialConnected && lunxun.isPolling() && lunxun.isPaused()) {
// 鍙湁鍦ㄤ笉鍦ㄨ缃〉闈㈡椂鎵嶈嚜鍔ㄦ仮澶�
if (!isInConfigPage()) {
- lunxun.resumePolling();
+// lunxun.resumePolling();
+// System.out.println("灏濊瘯鑷姩鍚姩杞鍔熻兘");
}
}
});
@@ -133,7 +143,7 @@
try {
// 鍒濆鍖栧崱妲界鐞嗗櫒 - 蹇呴』棣栧厛鎵ц
slotManager = new SlotManager();
- System.out.println("鍗℃Ы绠$悊鍣ㄥ垵濮嬪寲瀹屾垚");
+ //System.out.println("鍗℃Ы绠$悊鍣ㄥ垵濮嬪寲瀹屾垚");
// 鐩存帴浣跨敤宸茬粡鍒濆鍖栫殑閰嶇疆绯荤粺
Chushihua configSystem = Chushihua.getInstance();
@@ -156,9 +166,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 +181,7 @@
}
private void initializeUI() {
- setTitle("UWB浜哄憳瀹氫綅鍗″彂鍗℃満绠$悊绯荤粺");
+ setTitle("鏅鸿兘浜鸿劯鍙戝崱鏈虹鐞嗙郴缁�");
setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
@@ -288,8 +298,9 @@
refreshBtn.addActionListener(e -> {
updateCardSlotsDisplay();
updateStatistics();
+ lunxun.resumePolling();
JOptionPane.showMessageDialog(CardMachineUI.this,
- "鎵嬪姩鍒锋柊瀹屾垚锛屽叡鍒锋柊 " + TOTAL_SLOTS + " 涓崱妲�",
+ "鎵嬪姩鍒锋柊瀹屾垚锛屽惎鍔ㄨ疆璇紝鍏卞埛鏂� " + TOTAL_SLOTS + " 涓崱妲�",
"鍒锋柊瀹屾垚", JOptionPane.INFORMATION_MESSAGE);
});
@@ -466,9 +477,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 +484,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 +515,7 @@
cardSlotsPanel.revalidate();
cardSlotsPanel.repaint();
- System.out.println("鍗℃Ы鏄剧ず鏇存柊瀹屾垚: " + updatedSlots + " 涓垚鍔�, " + errorSlots + " 涓け璐�");
+// //System.out.println("鍗℃Ы鏄剧ず鏇存柊瀹屾垚: " + updatedSlots + " 涓垚鍔�, " + errorSlots + " 涓け璐�");
}
private Color brighterColor(Color color) {
@@ -572,7 +578,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,40 +596,74 @@
boolean pickupSuccess = CardPickupDialog.showCardPickup(this, slotId, status);
if (pickupSuccess) {
- // 鍙栧崱鎴愬姛锛岀晫闈㈠凡缁忓湪瀵硅瘽妗嗕腑鏇存柊锛岃繖閲屼笉闇�瑕侀澶栨搷浣�
- System.out.println("鍗℃Ы " + slotId + " 鍙栧崱鎴愬姛");
+ // 鍙栧崱鎴愬姛锛岃皟鐢╟hanggehaska鏂规硶鏀瑰彉鍗℃Ы灞炴��
+ SlotManager.changgehaska(slotId,1); // "1"琛ㄧず绠$悊鍛樻搷浣�
+ //System.out.println("鍗℃Ы " + slotId + " 鍙栧崱鎴愬姛锛屽凡鏇存柊鍗℃Ы鐘舵��");
}
}
public void dispose() {
- // 鍋滄UI鍒锋柊瀹氭椂鍣�
- if (uiUpdateTimer != null) {
- uiUpdateTimer.stop();
- }
-
- // 鍋滄涓插彛鐘舵�佺洃鎺�
- if (serialStatusTimer != null) {
- serialStatusTimer.stop();
- }
-
- // 鍋滄杞鏌ヨ
- if (lunxun.isPolling()) {
- lunxun.stopPolling();
- System.out.println("搴旂敤绋嬪簭鍏抽棴锛岃疆璇㈡煡璇㈠凡鍋滄");
- }
-
- // 鍋滄涓插彛鍗忚瑙f瀽鍣紙鏂板锛�
- if (serialProtocolParser != null) {
- serialProtocolParser.stop();
- }
-
- // 鍋滄涓插彛鏈嶅姟
- if (serialPortService != null) {
- serialPortService.stopCapture();
- serialPortService.close();
- }
-
- super.dispose();
+ int result = JOptionPane.showConfirmDialog(this,
+ "纭畾瑕侀��鍑虹▼搴忓悧锛�",
+ "纭閫�鍑�",
+ JOptionPane.YES_NO_OPTION);
+ if (result != JOptionPane.YES_OPTION) {
+ return; // 鐢ㄦ埛鍙栨秷閫�鍑�
+ }
+
+ // 鍋滄UI鍒锋柊瀹氭椂鍣�
+ if (uiUpdateTimer != null) {
+ uiUpdateTimer.stop();
+ }
+
+ // 鍋滄涓插彛鐘舵�佺洃鎺�
+ if (serialStatusTimer != null) {
+ serialStatusTimer.stop();
+ }
+
+ // 鍋滄杞鏌ヨ
+ if (lunxun.isPolling()) {
+ lunxun.stopPolling();
+ //System.out.println("搴旂敤绋嬪簭鍏抽棴锛岃疆璇㈡煡璇㈠凡鍋滄");
+ }
+
+ // 鍋滄涓插彛鍗忚瑙f瀽鍣紙鏂板锛�
+ if (serialProtocolParser != null) {
+ serialProtocolParser.stop();
+ }
+
+ // 鍋滄涓插彛鏈嶅姟
+ if (serialPortService != null) {
+ serialPortService.stopCapture();
+ serialPortService.close();
+ }
+
+ super.dispose();
+
+ // 鏂板锛氱‘淇濈▼搴忓畬鍏ㄩ��鍑哄墠閲婃斁鍗曞疄渚嬮攣
+ try {
+ // 閫氳繃鍙嶅皠璋冪敤 Homein 鐨勬竻鐞嗘柟娉�
+ Class<?> homeinClass = Class.forName("home.Homein");
+ java.lang.reflect.Method cleanupMethod = homeinClass.getDeclaredMethod("cleanupSingleInstanceLock");
+ cleanupMethod.setAccessible(true);
+ cleanupMethod.invoke(null);
+ //System.out.println("鍗曞疄渚嬮攣宸查噴鏀�");
+ } catch (Exception e) {
+ System.err.println("閲婃斁鍗曞疄渚嬮攣鏃跺彂鐢熷紓甯�: " + e.getMessage());
+ // 濡傛灉鍙嶅皠澶辫触锛屽皾璇曠洿鎺ュ叧闂鍙�
+ try {
+ java.net.ServerSocket socket = new java.net.ServerSocket();
+ socket.setReuseAddress(true);
+ socket.bind(new java.net.InetSocketAddress(9999));
+ socket.close();
+ //System.out.println("閫氳繃鐩存帴缁戝畾鏂瑰紡閲婃斁绔彛");
+ } catch (Exception ex) {
+ System.err.println("鐩存帴閲婃斁绔彛涔熷け璐�: " + ex.getMessage());
+ }
+ }
+
+ // 鏂板锛氱‘淇濈▼搴忓畬鍏ㄩ��鍑�
+ System.exit(0);
}
/**
@@ -637,34 +676,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 +829,7 @@
if (lunxun.checkSerialConnection()) {
boolean resumed = lunxun.setPollingPaused(false);
if (resumed) {
- System.out.println("鍒囨崲鍒颁富椤甸潰锛岃疆璇㈡煡璇㈠凡鎭㈠");
+ //System.out.println("鍒囨崲鍒颁富椤甸潰锛岃疆璇㈡煡璇㈠凡鎭㈠");
}
} else {
System.err.println("鍒囨崲鍒颁富椤甸潰锛屼覆鍙f湭杩炴帴锛屼繚鎸佽疆璇㈡殏鍋�");
@@ -829,12 +840,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 +876,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