From 9d7822ada88392e1b1a612e9b4f680fe6b09aedf Mon Sep 17 00:00:00 2001
From: 826220679@qq.com <826220679@qq.com>
Date: 星期六, 27 十二月 2025 22:25:38 +0800
Subject: [PATCH] 优化了路径规划
---
src/Mqttmessage/Client.java | 368 ++++++++++++++++++++++++++++++++++------------------
1 files changed, 242 insertions(+), 126 deletions(-)
diff --git a/src/Mqttmessage/Client.java b/src/Mqttmessage/Client.java
index de7d4a8..35c9b35 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;
/**
* 鏋勯�犲嚱鏁�
@@ -52,7 +57,6 @@
*/
public void connect() throws MqttException {
if (client != null && client.isConnected()) {
- System.out.println("MQTT瀹㈡埛绔凡杩炴帴锛孋lientId: " + clientId);
return;
}
@@ -68,12 +72,14 @@
// 浣跨敤鍐呭瓨鎸佷箙鍖栵紝閬垮厤鏂囦欢閿佸畾闂
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,6 +88,7 @@
* @throws MqttException 璁㈤槄澶辫触鏃舵姏鍑哄紓甯�
*/
public void subscribe(int qos) throws MqttException {
+ this.qos = qos; // 淇濆瓨QoS鍊硷紝鐢ㄤ簬閲嶈繛鍚庨噸鏂拌闃�
if (client == null || !client.isConnected()) {
connect();
}
@@ -93,7 +100,8 @@
* @throws MqttException 璁㈤槄澶辫触鏃舵姏鍑哄紓甯�
*/
public void subscribe() throws MqttException {
- subscribe(2);
+ // 浣跨敤QoS 0纭繚娑堟伅鍙潬浼犻�掞紙鑷冲皯涓�娆★紝涓斾粎涓�娆★級
+ subscribe(0);
}
/**
@@ -107,9 +115,76 @@
}
/**
+ * 鍚姩杩炴帴鐩戞帶绾跨▼锛屽畾鏈熸鏌ヨ繛鎺ョ姸鎬佸苟鍦ㄦ柇寮�鏃跺皾璇曢噸杩�
+ */
+ 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()) {
+ try {
+ // 灏濊瘯閲嶆柊杩炴帴
+ if (!client.isConnected()) {
+ client.reconnect();
+ // 閲嶈繛鎴愬姛鍚庨噸鏂拌闃�
+ if (client.isConnected()) {
+ client.subscribe(topic, qos);
+ }
+ }
+ } 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();
+ }
+
+ /**
+ * 鍋滄杩炴帴鐩戞帶绾跨▼
+ */
+ 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 +220,98 @@
* @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
*/
public static boolean connectMQTT() {
- // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
- disconnectAll();
+ // 闃叉閲嶅閲嶈繛
+ if (isReconnecting) {
+ return false;
+ }
- boolean gpsSuccess = false;
- boolean responseSuccess = false;
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
+ if (areClientsConnected()) {
+ 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;
+ }
+ } 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;
+ }
+ } 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) {
+ return true;
+ } else if (gpsSuccess || responseSuccess) {
+ 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 +323,96 @@
* @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
*/
public static boolean connectMQTT(String host, String deviceId, String userEmail) {
- // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
- disconnectAll();
+ // 闃叉閲嶅閲嶈繛
+ if (isReconnecting) {
+ return false;
+ }
- boolean gpsSuccess = false;
- boolean responseSuccess = false;
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
+ if (areClientsConnected()) {
+ 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;
+ }
+ } 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;
+ }
+ } 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) {
+ return true;
+ } else if (gpsSuccess || responseSuccess) {
+ 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;
}
}
@@ -310,7 +429,6 @@
Client mqttClient = new Client(host, topic, clientId);
mqttClient.connect();
mqttClient.subscribe(qos);
- System.out.println("MQTT瀹㈡埛绔垱寤哄苟璁㈤槄鎴愬姛锛屼富棰�: " + topic + ", ClientId: " + clientId);
return mqttClient;
} catch (MqttException e) {
System.err.println("MQTT瀹㈡埛绔垱寤哄け璐�: " + e.getMessage() + ", 涓婚: " + topic);
@@ -337,12 +455,10 @@
try {
if (gpsClient != null) {
gpsClient.close();
- System.out.println("GPS涓婚MQTT杩炴帴宸叉柇寮�");
gpsClient = null;
}
if (responseClient != null) {
responseClient.close();
- System.out.println("鍝嶅簲涓婚MQTT杩炴帴宸叉柇寮�");
responseClient = null;
}
} catch (Exception e) {
--
Gitblit v1.10.0