From 3ca5f28be0a266e57a3ddece80d28072a1811de7 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 16:04:04 +0800
Subject: [PATCH] 解决了点击保存路径的bug
---
src/Mqttmessage/Client.java | 379 ++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 258 insertions(+), 121 deletions(-)
diff --git a/src/Mqttmessage/Client.java b/src/Mqttmessage/Client.java
index de7d4a8..ca1e876 100644
--- a/src/Mqttmessage/Client.java
+++ b/src/Mqttmessage/Client.java
@@ -19,10 +19,15 @@
private String clientId;
private MqttClient client;
private MqttConnectOptions options;
+ private int qos = 0; // 榛樿QoS绛夌骇
+ private Thread connectionMonitorThread; // 杩炴帴鐩戞帶绾跨▼
+ private volatile boolean monitoring = false; // 鐩戞帶鏍囧織
// 闈欐�佸彉閲忕敤浜庡瓨鍌ㄥ鎴风瀹炰緥
private static Client gpsClient;
private static Client responseClient;
+ // 閲嶈繛鏍囧織锛岄槻姝㈤噸澶嶉噸杩�
+ private static volatile boolean isReconnecting = false;
/**
* 鏋勯�犲嚱鏁�
@@ -68,12 +73,15 @@
// 浣跨敤鍐呭瓨鎸佷箙鍖栵紝閬垮厤鏂囦欢閿佸畾闂
client = new MqttClient(host, clientId, new MemoryPersistence());
- // 鍏堣缃洖璋冿紝鍐嶈繛鎺�
- client.setCallback(new PushCallback());
+ // 鍏堣缃洖璋冿紝鍐嶈繛鎺ワ紙浼犲叆Client瀹炰緥鍜岃闃呬俊鎭紝鐢ㄤ簬鑷姩閲嶈繛鍚庨噸鏂拌闃咃級
+ client.setCallback(new PushCallback(this, topic, qos));
// 鎵ц杩炴帴
client.connect(options);
System.out.println("MQTT杩炴帴鎴愬姛锛丆lientId: " + clientId + ", 鏈嶅姟鍣�: " + host + ", 涓婚: " + topic);
+
+ // 鍚姩杩炴帴鐩戞帶绾跨▼
+ startConnectionMonitor();
}
/**
@@ -82,10 +90,12 @@
* @throws MqttException 璁㈤槄澶辫触鏃舵姏鍑哄紓甯�
*/
public void subscribe(int qos) throws MqttException {
+ this.qos = qos; // 淇濆瓨QoS鍊硷紝鐢ㄤ簬閲嶈繛鍚庨噸鏂拌闃�
if (client == null || !client.isConnected()) {
connect();
}
client.subscribe(topic, qos);
+ System.out.println("宸茶闃呬富棰�: " + topic + ", QoS: " + qos);
}
/**
@@ -93,7 +103,8 @@
* @throws MqttException 璁㈤槄澶辫触鏃舵姏鍑哄紓甯�
*/
public void subscribe() throws MqttException {
- subscribe(2);
+ // 浣跨敤QoS 0纭繚娑堟伅鍙潬浼犻�掞紙鑷冲皯涓�娆★紝涓斾粎涓�娆★級
+ subscribe(0);
}
/**
@@ -107,9 +118,79 @@
}
/**
+ * 鍚姩杩炴帴鐩戞帶绾跨▼锛屽畾鏈熸鏌ヨ繛鎺ョ姸鎬佸苟鍦ㄦ柇寮�鏃跺皾璇曢噸杩�
+ */
+ private void startConnectionMonitor() {
+ if (monitoring) {
+ return; // 宸茬粡鍦ㄧ洃鎺т腑
+ }
+
+ monitoring = true;
+ connectionMonitorThread = new Thread(() -> {
+ while (monitoring && client != null) {
+ try {
+ Thread.sleep(5000); // 姣�5绉掓鏌ヤ竴娆�
+
+ if (client != null && !client.isConnected()) {
+ System.out.println("妫�娴嬪埌MQTT杩炴帴鏂紑锛屽皾璇曢噸杩�... ClientId: " + clientId);
+ try {
+ // 灏濊瘯閲嶆柊杩炴帴
+ if (!client.isConnected()) {
+ client.reconnect();
+ // 閲嶈繛鎴愬姛鍚庨噸鏂拌闃�
+ if (client.isConnected()) {
+ client.subscribe(topic, qos);
+ System.out.println("杩炴帴鐩戞帶锛氶噸杩炴垚鍔熷苟閲嶆柊璁㈤槄涓婚: " + topic);
+ }
+ }
+ } catch (Exception e) {
+ System.err.println("杩炴帴鐩戞帶锛氶噸杩炲け璐�: " + e.getMessage());
+ // 濡傛灉鑷姩閲嶈繛澶辫触锛屽皾璇曞畬鍏ㄩ噸鏂拌繛鎺�
+ try {
+ connect();
+ if (isConnected()) {
+ subscribe(qos);
+ }
+ } catch (Exception ex) {
+ System.err.println("杩炴帴鐩戞帶锛氬畬鍏ㄩ噸杩炲け璐�: " + ex.getMessage());
+ }
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ break;
+ } catch (Exception e) {
+ System.err.println("杩炴帴鐩戞帶绾跨▼寮傚父: " + e.getMessage());
+ }
+ }
+ });
+ connectionMonitorThread.setDaemon(true);
+ connectionMonitorThread.setName("MQTT-ConnectionMonitor-" + clientId);
+ connectionMonitorThread.start();
+ System.out.println("宸插惎鍔∕QTT杩炴帴鐩戞帶绾跨▼: " + clientId);
+ }
+
+ /**
+ * 鍋滄杩炴帴鐩戞帶绾跨▼
+ */
+ private void stopConnectionMonitor() {
+ monitoring = false;
+ if (connectionMonitorThread != null) {
+ connectionMonitorThread.interrupt();
+ try {
+ connectionMonitorThread.join(1000);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ connectionMonitorThread = null;
+ }
+ }
+
+ /**
* 鍏抽棴瀹㈡埛绔苟閲婃斁璧勬簮
*/
public void close() {
+ stopConnectionMonitor(); // 鍋滄鐩戞帶绾跨▼
try {
if (client != null) {
if (client.isConnected()) {
@@ -145,76 +226,104 @@
* @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
*/
public static boolean connectMQTT() {
- // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
- disconnectAll();
+ // 闃叉閲嶅閲嶈繛
+ if (isReconnecting) {
+ System.out.println("MQTT姝e湪閲嶈繛涓紝璺宠繃鏈閲嶈繛璇锋眰");
+ return false;
+ }
- boolean gpsSuccess = false;
- boolean responseSuccess = false;
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
+ if (areClientsConnected()) {
+ System.out.println("MQTT瀹㈡埛绔凡杩炴帴锛屾棤闇�閲嶅杩炴帴");
+ return true;
+ }
+ isReconnecting = true;
try {
- String host = "tcp://39.99.43.227:1883";
- String deiveID = Setsys.getMowerIdValue();
- // 娣诲姞鏃堕棿鎴崇‘淇濆鎴风ID鍞竴
- long timestamp = System.currentTimeMillis();
- String clientId = Usrdell.getUserEmail() + "mower" + "_" + timestamp;
- String clientId2 = Usrdell.getUserEmail() + "response" + "_" + timestamp;
- String topic = "mower/" + deiveID + "/gps";
- String topic2 = "mower/" + deiveID + "/response";
+ // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
+ disconnectAll();
- // 杩炴帴GPS涓婚
+ boolean gpsSuccess = false;
+ boolean responseSuccess = false;
+
try {
- gpsClient = new Client(host, topic, clientId);
- gpsClient.connect();
- // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
- Thread.sleep(100);
- gpsClient.subscribe();
- gpsSuccess = true;
- System.out.println("GPS涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
- } catch (MqttException e) {
- System.err.println("GPS涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ String host = "tcp://39.99.43.227:1883";
+ String deiveID = Setsys.getMowerIdValue();
+ if (deiveID == null || deiveID.isEmpty()) {
+ System.err.println("璁惧ID涓虹┖锛屾棤娉曡繛鎺QTT");
+ return false;
+ }
+ // 娣诲姞鏃堕棿鎴崇‘淇濆鎴风ID鍞竴
+ long timestamp = System.currentTimeMillis();
+ String clientId = Usrdell.getUserEmail() + "mower" + "_" + timestamp;
+ String clientId2 = Usrdell.getUserEmail() + "response" + "_" + timestamp;
+ String topic = "mower/" + deiveID + "/gps";
+ String topic2 = "mower/" + deiveID + "/response";
+
+ // 杩炴帴GPS涓婚
+ try {
+ gpsClient = new Client(host, topic, clientId);
+ gpsClient.connect();
+ // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
+ Thread.sleep(100);
+ if (gpsClient != null && gpsClient.isConnected()) {
+ gpsClient.subscribe();
+ gpsSuccess = true;
+ System.out.println("GPS涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
+ }
+ } catch (MqttException e) {
+ System.err.println("GPS涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ if (e.getCause() != null) {
+ System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ }
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ System.err.println("杩炴帴杩囩▼琚腑鏂�");
+ }
+
+ // 杩炴帴鍝嶅簲涓婚
+ try {
+ responseClient = new Client(host, topic2, clientId2);
+ responseClient.connect();
+ // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
+ Thread.sleep(100);
+ if (responseClient != null && responseClient.isConnected()) {
+ responseClient.subscribe();
+ responseSuccess = true;
+ System.out.println("鍝嶅簲涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
+ }
+ } catch (MqttException e) {
+ System.err.println("鍝嶅簲涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ if (e.getCause() != null) {
+ System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ }
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ System.err.println("杩炴帴杩囩▼琚腑鏂�");
+ }
+
+ if (gpsSuccess && responseSuccess) {
+ System.out.println("鎵�鏈塎QTT涓婚杩炴帴骞惰闃呮垚鍔燂紒");
+ return true;
+ } else if (gpsSuccess || responseSuccess) {
+ System.out.println("閮ㄥ垎MQTT涓婚杩炴帴鎴愬姛");
+ return true;
+ } else {
+ System.err.println("鎵�鏈塎QTT涓婚杩炴帴澶辫触");
+ return false;
+ }
+ } catch (Exception e) {
+ System.err.println("MQTT杩炴帴杩囩▼鍙戠敓寮傚父: " + e.getMessage());
if (e.getCause() != null) {
- System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ System.err.println("寮傚父鍘熷洜: " + e.getCause().getMessage());
}
e.printStackTrace();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- System.err.println("杩炴帴杩囩▼琚腑鏂�");
- }
-
- // 杩炴帴鍝嶅簲涓婚
- try {
- responseClient = new Client(host, topic2, clientId2);
- responseClient.connect();
- // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
- Thread.sleep(100);
- responseClient.subscribe();
- responseSuccess = true;
- System.out.println("鍝嶅簲涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
- } catch (MqttException e) {
- System.err.println("鍝嶅簲涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
- if (e.getCause() != null) {
- System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
- }
- e.printStackTrace();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- System.err.println("杩炴帴杩囩▼琚腑鏂�");
- }
-
- if (gpsSuccess && responseSuccess) {
- System.out.println("鎵�鏈塎QTT涓婚杩炴帴骞惰闃呮垚鍔燂紒");
- return true;
- } else if (gpsSuccess || responseSuccess) {
- System.out.println("閮ㄥ垎MQTT涓婚杩炴帴鎴愬姛");
- return true;
- } else {
- System.err.println("鎵�鏈塎QTT涓婚杩炴帴澶辫触");
return false;
}
- } catch (Exception e) {
- System.err.println("MQTT杩炴帴杩囩▼鍙戠敓寮傚父: " + e.getMessage());
- e.printStackTrace();
- return false;
+ } finally {
+ isReconnecting = false;
}
}
@@ -226,74 +335,102 @@
* @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
*/
public static boolean connectMQTT(String host, String deviceId, String userEmail) {
- // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
- disconnectAll();
+ // 闃叉閲嶅閲嶈繛
+ if (isReconnecting) {
+ System.out.println("MQTT姝e湪閲嶈繛涓紝璺宠繃鏈閲嶈繛璇锋眰");
+ return false;
+ }
- boolean gpsSuccess = false;
- boolean responseSuccess = false;
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
+ if (areClientsConnected()) {
+ System.out.println("MQTT瀹㈡埛绔凡杩炴帴锛屾棤闇�閲嶅杩炴帴");
+ return true;
+ }
+ isReconnecting = true;
try {
- // 娣诲姞鏃堕棿鎴崇‘淇濆鎴风ID鍞竴
- long timestamp = System.currentTimeMillis();
- String clientId = userEmail + "mower" + "_" + timestamp;
- String clientId2 = userEmail + "response" + "_" + timestamp;
- String topic = "mower/" + deviceId + "/gps";
- String topic2 = "mower/" + deviceId + "/response";
+ // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
+ disconnectAll();
- // 杩炴帴GPS涓婚
+ boolean gpsSuccess = false;
+ boolean responseSuccess = false;
+
try {
- gpsClient = new Client(host, topic, clientId);
- gpsClient.connect();
- // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
- Thread.sleep(100);
- gpsClient.subscribe();
- gpsSuccess = true;
- System.out.println("GPS涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
- } catch (MqttException e) {
- System.err.println("GPS涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ if (deviceId == null || deviceId.isEmpty()) {
+ System.err.println("璁惧ID涓虹┖锛屾棤娉曡繛鎺QTT");
+ return false;
+ }
+ // 娣诲姞鏃堕棿鎴崇‘淇濆鎴风ID鍞竴
+ long timestamp = System.currentTimeMillis();
+ String clientId = userEmail + "mower" + "_" + timestamp;
+ String clientId2 = userEmail + "response" + "_" + timestamp;
+ String topic = "mower/" + deviceId + "/gps";
+ String topic2 = "mower/" + deviceId + "/response";
+
+ // 杩炴帴GPS涓婚
+ try {
+ gpsClient = new Client(host, topic, clientId);
+ gpsClient.connect();
+ // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
+ Thread.sleep(100);
+ if (gpsClient != null && gpsClient.isConnected()) {
+ gpsClient.subscribe();
+ gpsSuccess = true;
+ System.out.println("GPS涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
+ }
+ } catch (MqttException e) {
+ System.err.println("GPS涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ if (e.getCause() != null) {
+ System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ }
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ System.err.println("杩炴帴杩囩▼琚腑鏂�");
+ }
+
+ // 杩炴帴鍝嶅簲涓婚
+ try {
+ responseClient = new Client(host, topic2, clientId2);
+ responseClient.connect();
+ // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
+ Thread.sleep(100);
+ if (responseClient != null && responseClient.isConnected()) {
+ responseClient.subscribe();
+ responseSuccess = true;
+ System.out.println("鍝嶅簲涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
+ }
+ } catch (MqttException e) {
+ System.err.println("鍝嶅簲涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
+ if (e.getCause() != null) {
+ System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ }
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ System.err.println("杩炴帴杩囩▼琚腑鏂�");
+ }
+
+ if (gpsSuccess && responseSuccess) {
+ System.out.println("鎵�鏈塎QTT涓婚杩炴帴骞惰闃呮垚鍔燂紒");
+ return true;
+ } else if (gpsSuccess || responseSuccess) {
+ System.out.println("閮ㄥ垎MQTT涓婚杩炴帴鎴愬姛");
+ return true;
+ } else {
+ System.err.println("鎵�鏈塎QTT涓婚杩炴帴澶辫触");
+ return false;
+ }
+ } catch (Exception e) {
+ System.err.println("MQTT杩炴帴杩囩▼鍙戠敓寮傚父: " + e.getMessage());
if (e.getCause() != null) {
- System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+ System.err.println("寮傚父鍘熷洜: " + e.getCause().getMessage());
}
e.printStackTrace();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- System.err.println("杩炴帴杩囩▼琚腑鏂�");
- }
-
- // 杩炴帴鍝嶅簲涓婚
- try {
- responseClient = new Client(host, topic2, clientId2);
- responseClient.connect();
- // 绋嶄綔寤惰繜锛岀‘淇濊繛鎺ョǔ瀹�
- Thread.sleep(100);
- responseClient.subscribe();
- responseSuccess = true;
- System.out.println("鍝嶅簲涓婚MQTT杩炴帴骞惰闃呮垚鍔�");
- } catch (MqttException e) {
- System.err.println("鍝嶅簲涓婚MQTT杩炴帴澶辫触: " + e.getMessage());
- if (e.getCause() != null) {
- System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
- }
- e.printStackTrace();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- System.err.println("杩炴帴杩囩▼琚腑鏂�");
- }
-
- if (gpsSuccess && responseSuccess) {
- System.out.println("鎵�鏈塎QTT涓婚杩炴帴骞惰闃呮垚鍔燂紒");
- return true;
- } else if (gpsSuccess || responseSuccess) {
- System.out.println("閮ㄥ垎MQTT涓婚杩炴帴鎴愬姛");
- return true;
- } else {
- System.err.println("鎵�鏈塎QTT涓婚杩炴帴澶辫触");
return false;
}
- } catch (Exception e) {
- System.err.println("MQTT杩炴帴杩囩▼鍙戠敓寮傚父: " + e.getMessage());
- e.printStackTrace();
- return false;
+ } finally {
+ isReconnecting = false;
}
}
--
Gitblit v1.10.0