From 2eea735fd4ddf0ae047687780271ef3962d256cc Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 15:54:26 +0800
Subject: [PATCH] 替换了mqtt最新版
---
src/Mqttmessage/Entity/ResponseData.java | 84 +++++
src/Mqttmessage/Entity/GPSData.java | 215 ++++++++++++-
src/Mqttmessage/Util/DeviceMessageParser.java | 61 ---
src/Mqttmessage/Entity/BasestationData.java | 44 ++
src/Mqttmessage/Client.java | 379 ++++++++++++++++-------
src/Mqttmessage/Entity/HxzkPathMessage.java | 59 +++
src/Mqttmessage/PushCallback.java | 91 +++++
src/Mqttmessage/Entity/CoordinatePoint.java | 19 +
src/Mqttmessage/Entity/PathData.java | 2
9 files changed, 742 insertions(+), 212 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;
}
}
diff --git a/src/Mqttmessage/Entity/BasestationData.java b/src/Mqttmessage/Entity/BasestationData.java
index 058ec2c..2ac6596 100644
--- a/src/Mqttmessage/Entity/BasestationData.java
+++ b/src/Mqttmessage/Entity/BasestationData.java
@@ -1,9 +1,9 @@
package Mqttmessage.Entity;
-import lombok.Data;
-@Data
+
+
public class BasestationData {
private String latitude_dm;
@@ -22,4 +22,44 @@
this.longitude_hemisphere = longitude_hemisphere;
this.altitude = altitude;
}
+
+ public String getLatitude_dm() {
+ return latitude_dm;
+ }
+
+ public void setLatitude_dm(String latitude_dm) {
+ this.latitude_dm = latitude_dm;
+ }
+
+ public String getLatitude_hemisphere() {
+ return latitude_hemisphere;
+ }
+
+ public void setLatitude_hemisphere(String latitude_hemisphere) {
+ this.latitude_hemisphere = latitude_hemisphere;
+ }
+
+ public String getLongitude_dm() {
+ return longitude_dm;
+ }
+
+ public void setLongitude_dm(String longitude_dm) {
+ this.longitude_dm = longitude_dm;
+ }
+
+ public String getLongitude_hemisphere() {
+ return longitude_hemisphere;
+ }
+
+ public void setLongitude_hemisphere(String longitude_hemisphere) {
+ this.longitude_hemisphere = longitude_hemisphere;
+ }
+
+ public double getAltitude() {
+ return altitude;
+ }
+
+ public void setAltitude(double altitude) {
+ this.altitude = altitude;
+ }
}
diff --git a/src/Mqttmessage/Entity/CoordinatePoint.java b/src/Mqttmessage/Entity/CoordinatePoint.java
index 58d3d68..7be43f0 100644
--- a/src/Mqttmessage/Entity/CoordinatePoint.java
+++ b/src/Mqttmessage/Entity/CoordinatePoint.java
@@ -1,9 +1,8 @@
package Mqttmessage.Entity;
-import lombok.Data;
-@Data
+
public class CoordinatePoint {
private String x; // X鍧愭爣
private String y; // Y鍧愭爣
@@ -15,4 +14,20 @@
this.x = x;
this.y = y;
}
+
+ public String getX() {
+ return x;
+ }
+
+ public void setX(String x) {
+ this.x = x;
+ }
+
+ public String getY() {
+ return y;
+ }
+
+ public void setY(String y) {
+ this.y = y;
+ }
}
diff --git a/src/Mqttmessage/Entity/GPSData.java b/src/Mqttmessage/Entity/GPSData.java
index 8f96c20..7d30110 100644
--- a/src/Mqttmessage/Entity/GPSData.java
+++ b/src/Mqttmessage/Entity/GPSData.java
@@ -1,13 +1,5 @@
package Mqttmessage.Entity;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-@Data
-@NoArgsConstructor
-@AllArgsConstructor
public class GPSData{
// JSON涓殑鍘熷瀛楁
private String msg_id; // 娑堟伅鍞竴鏍囪瘑
@@ -26,6 +18,27 @@
private GGAData ggaData;
/**
+ * 榛樿鏋勯�犲嚱鏁�
+ */
+ public GPSData() {
+ }
+
+ /**
+ * 鍏ㄥ弬鏋勯�犲嚱鏁�
+ */
+ public GPSData(String msg_id, Long timestamp, String device_id, String data_type,
+ String gps_raw, IMUData imu_data, StatusInfo status, GGAData ggaData) {
+ this.msg_id = msg_id;
+ this.timestamp = timestamp;
+ this.device_id = device_id;
+ this.data_type = data_type;
+ this.gps_raw = gps_raw;
+ this.imu_data = imu_data;
+ this.status = status;
+ this.ggaData = ggaData;
+ }
+
+ /**
* 鑾峰彇鍘熷GPS鏁版嵁
* @return 鍘熷GPS鏁版嵁瀛楃涓�
*/
@@ -50,15 +63,19 @@
/**
* IMU鏁版嵁鍐呴儴绫伙紙绠�鍖栫増锛屽彧鍖呭惈瑙掑害淇℃伅锛�
*/
- @Data
- @NoArgsConstructor
public static class IMUData {
private Double roll; // 妯粴瑙� 瑙掑害
private Double pitch; // 淇话瑙� 瑙掑害
private Double yaw; // 鍋忚埅瑙� 瑙掑害
/**
- * 甯﹀弬鏁扮殑鏋勯�犲嚱鏁�
+ * 榛樿鏋勯�犲嚱鏁�
+ */
+ public IMUData() {
+ }
+
+ /**
+ * 鍏ㄥ弬鏋勯�犲嚱鏁�
* @param roll 妯粴瑙� 瑙掑害
* @param pitch 淇话瑙� 瑙掑害
* @param yaw 鍋忚埅瑙� 瑙掑害
@@ -68,13 +85,35 @@
this.pitch = pitch;
this.yaw = yaw;
}
+
+ public Double getRoll() {
+ return roll;
+ }
+
+ public void setRoll(Double roll) {
+ this.roll = roll;
+ }
+
+ public Double getPitch() {
+ return pitch;
+ }
+
+ public void setPitch(Double pitch) {
+ this.pitch = pitch;
+ }
+
+ public Double getYaw() {
+ return yaw;
+ }
+
+ public void setYaw(Double yaw) {
+ this.yaw = yaw;
+ }
}
/**
* 鐘舵�佷俊鎭唴閮ㄧ被锛堜笌StatusData涓殑StatusInfo淇濇寔涓�鑷达級
*/
- @Data
- @NoArgsConstructor
public static class StatusInfo {
private Integer battery_level; // 鐢垫睜鐢甸噺鐧惧垎姣�
private Double battery_voltage; // 鐢垫睜鐢靛帇
@@ -88,6 +127,12 @@
private String path_id_saved; // 瀛樺偍鐨勮矾寰処D
/**
+ * 榛樿鏋勯�犲嚱鏁�
+ */
+ public StatusInfo() {
+ }
+
+ /**
* 甯﹀弬鏁扮殑鏋勯�犲嚱鏁�
* @param battery_level 鐢垫睜鐢甸噺鐧惧垎姣�
* @param battery_voltage 鐢垫睜鐢靛帇
@@ -199,9 +244,6 @@
/**
* GGA鏁版嵁瑙f瀽绫伙紙鍙�夛紝鐢ㄤ簬瀛樺偍瑙f瀽鍚庣殑GGA鏁版嵁锛�
*/
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
public static class GGAData {
private String utcTime; // UTC鏃堕棿
private String latitude; // 绾害锛堝師濮嬪害鍒嗘牸寮忥級
@@ -217,6 +259,147 @@
private String geoidSepUnit;// 澶у湴姘村噯闈㈠垎绂诲崟浣�
private String age; // 宸垎GPS鏁版嵁鏈熼檺
private String stationId; // 宸垎鍙傝�冨熀绔欐爣鍙�
+
+ /**
+ * 榛樿鏋勯�犲嚱鏁�
+ */
+ public GGAData() {
+ }
+
+ /**
+ * 鍏ㄥ弬鏋勯�犲嚱鏁�
+ */
+ public GGAData(String utcTime, String latitude, String latitudeDir, String longitude,
+ String longitudeDir, Integer gpsQuality, Integer satellites, Double hdop,
+ Double altitude, String altitudeUnit, Double geoidSep, String geoidSepUnit,
+ String age, String stationId) {
+ this.utcTime = utcTime;
+ this.latitude = latitude;
+ this.latitudeDir = latitudeDir;
+ this.longitude = longitude;
+ this.longitudeDir = longitudeDir;
+ this.gpsQuality = gpsQuality;
+ this.satellites = satellites;
+ this.hdop = hdop;
+ this.altitude = altitude;
+ this.altitudeUnit = altitudeUnit;
+ this.geoidSep = geoidSep;
+ this.geoidSepUnit = geoidSepUnit;
+ this.age = age;
+ this.stationId = stationId;
+ }
+
+ public String getUtcTime() {
+ return utcTime;
+ }
+
+ public void setUtcTime(String utcTime) {
+ this.utcTime = utcTime;
+ }
+
+ public String getLatitude() {
+ return latitude;
+ }
+
+ public void setLatitude(String latitude) {
+ this.latitude = latitude;
+ }
+
+ public String getLatitudeDir() {
+ return latitudeDir;
+ }
+
+ public void setLatitudeDir(String latitudeDir) {
+ this.latitudeDir = latitudeDir;
+ }
+
+ public String getLongitude() {
+ return longitude;
+ }
+
+ public void setLongitude(String longitude) {
+ this.longitude = longitude;
+ }
+
+ public String getLongitudeDir() {
+ return longitudeDir;
+ }
+
+ public void setLongitudeDir(String longitudeDir) {
+ this.longitudeDir = longitudeDir;
+ }
+
+ public Integer getGpsQuality() {
+ return gpsQuality;
+ }
+
+ public void setGpsQuality(Integer gpsQuality) {
+ this.gpsQuality = gpsQuality;
+ }
+
+ public Integer getSatellites() {
+ return satellites;
+ }
+
+ public void setSatellites(Integer satellites) {
+ this.satellites = satellites;
+ }
+
+ public Double getHdop() {
+ return hdop;
+ }
+
+ public void setHdop(Double hdop) {
+ this.hdop = hdop;
+ }
+
+ public Double getAltitude() {
+ return altitude;
+ }
+
+ public void setAltitude(Double altitude) {
+ this.altitude = altitude;
+ }
+
+ public String getAltitudeUnit() {
+ return altitudeUnit;
+ }
+
+ public void setAltitudeUnit(String altitudeUnit) {
+ this.altitudeUnit = altitudeUnit;
+ }
+
+ public Double getGeoidSep() {
+ return geoidSep;
+ }
+
+ public void setGeoidSep(Double geoidSep) {
+ this.geoidSep = geoidSep;
+ }
+
+ public String getGeoidSepUnit() {
+ return geoidSepUnit;
+ }
+
+ public void setGeoidSepUnit(String geoidSepUnit) {
+ this.geoidSepUnit = geoidSepUnit;
+ }
+
+ public String getAge() {
+ return age;
+ }
+
+ public void setAge(String age) {
+ this.age = age;
+ }
+
+ public String getStationId() {
+ return stationId;
+ }
+
+ public void setStationId(String stationId) {
+ this.stationId = stationId;
+ }
}
public String getMsg_id() {
diff --git a/src/Mqttmessage/Entity/HxzkPathMessage.java b/src/Mqttmessage/Entity/HxzkPathMessage.java
index 616b3e7..9dbb547 100644
--- a/src/Mqttmessage/Entity/HxzkPathMessage.java
+++ b/src/Mqttmessage/Entity/HxzkPathMessage.java
@@ -1,9 +1,9 @@
package Mqttmessage.Entity;
-import lombok.Data;
-@Data
+
+
public class HxzkPathMessage {
private String msg_id;
private long timestamp;
@@ -25,4 +25,59 @@
this.path_data = path_data;
}
+ public String getMsg_id() {
+ return msg_id;
+ }
+
+ public void setMsg_id(String msg_id) {
+ this.msg_id = msg_id;
+ }
+
+ public long getTimestamp() {
+ return timestamp;
+ }
+
+ public void setTimestamp(long timestamp) {
+ this.timestamp = timestamp;
+ }
+
+ public String getUser_id() {
+ return user_id;
+ }
+
+ public void setUser_id(String user_id) {
+ this.user_id = user_id;
+ }
+
+ public String getDevice_id() {
+ return device_id;
+ }
+
+ public void setDevice_id(String device_id) {
+ this.device_id = device_id;
+ }
+
+ public String getCommand() {
+ return command;
+ }
+
+ public void setCommand(String command) {
+ this.command = command;
+ }
+
+ public BasestationData getBasestation_data() {
+ return basestation_data;
+ }
+
+ public void setBasestation_data(BasestationData basestation_data) {
+ this.basestation_data = basestation_data;
+ }
+
+ public PathData getPath_data() {
+ return path_data;
+ }
+
+ public void setPath_data(PathData path_data) {
+ this.path_data = path_data;
+ }
}
diff --git a/src/Mqttmessage/Entity/PathData.java b/src/Mqttmessage/Entity/PathData.java
index 559247f..6b4a8bf 100644
--- a/src/Mqttmessage/Entity/PathData.java
+++ b/src/Mqttmessage/Entity/PathData.java
@@ -5,7 +5,7 @@
import java.util.List;
-@Data
+
public class PathData {
private String path_id;
diff --git a/src/Mqttmessage/Entity/ResponseData.java b/src/Mqttmessage/Entity/ResponseData.java
index 358bb82..f6195d5 100644
--- a/src/Mqttmessage/Entity/ResponseData.java
+++ b/src/Mqttmessage/Entity/ResponseData.java
@@ -2,14 +2,14 @@
import com.fasterxml.jackson.annotation.JsonProperty;
-import lombok.Data;
+
import java.util.Map;
/**
* 璁惧鍝嶅簲娑堟伅瀹炰綋绫�
*/
-@Data
+
public class ResponseData {
@JsonProperty("msg_id")
@@ -37,10 +37,50 @@
this.response = response;
}
+ public String getMsgId() {
+ return msgId;
+ }
+
+ public void setMsgId(String msgId) {
+ this.msgId = msgId;
+ }
+
+ public long getTimestamp() {
+ return timestamp;
+ }
+
+ public void setTimestamp(long timestamp) {
+ this.timestamp = timestamp;
+ }
+
+ public String getDeviceId() {
+ return deviceId;
+ }
+
+ public void setDeviceId(String deviceId) {
+ this.deviceId = deviceId;
+ }
+
+ public String getOriginalMsgId() {
+ return originalMsgId;
+ }
+
+ public void setOriginalMsgId(String originalMsgId) {
+ this.originalMsgId = originalMsgId;
+ }
+
+ public ResponseInfo getResponse() {
+ return response;
+ }
+
+ public void setResponse(ResponseInfo response) {
+ this.response = response;
+ }
+
/**
* 鍝嶅簲淇℃伅鍐呴儴绫�
*/
- @Data
+
public static class ResponseInfo {
private String status;
private String command;
@@ -66,6 +106,44 @@
this.additionalInfo = additionalInfo;
}
+ public String getStatus() {
+ return status;
+ }
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public String getCommand() {
+ return command;
+ }
+
+ public void setCommand(String command) {
+ this.command = command;
+ }
+
+ public int getErrorCode() {
+ return errorCode;
+ }
+
+ public void setErrorCode(int errorCode) {
+ this.errorCode = errorCode;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public Map<String, Object> getAdditionalInfo() {
+ return additionalInfo;
+ }
+
+ public void setAdditionalInfo(Map<String, Object> additionalInfo) {
+ this.additionalInfo = additionalInfo;
+ }
}
}
\ No newline at end of file
diff --git a/src/Mqttmessage/PushCallback.java b/src/Mqttmessage/PushCallback.java
index 0d646dc..e124a95 100644
--- a/src/Mqttmessage/PushCallback.java
+++ b/src/Mqttmessage/PushCallback.java
@@ -9,12 +9,95 @@
import org.eclipse.paho.client.mqttv3.MqttMessage;
import udpdell.UDPServer;
public class PushCallback implements MqttCallback {
-
+
+ private Client clientInstance; // 淇濆瓨Client瀹炰緥寮曠敤锛岀敤浜庨噸鏂拌闃�
+ private String topic; // 淇濆瓨璁㈤槄鐨勪富棰�
+ private int qos; // 淇濆瓨QoS绛夌骇
+
+ /**
+ * 榛樿鏋勯�犲嚱鏁�
+ */
+ public PushCallback() {
+ }
+
+ /**
+ * 甯﹀弬鏁扮殑鏋勯�犲嚱鏁帮紝鐢ㄤ簬淇濆瓨Client瀹炰緥鍜岃闃呬俊鎭�
+ * @param clientInstance Client瀹炰緥
+ * @param topic 璁㈤槄鐨勪富棰�
+ * @param qos QoS绛夌骇
+ */
+ public PushCallback(Client clientInstance, String topic, int qos) {
+ this.clientInstance = clientInstance;
+ this.topic = topic;
+ this.qos = qos;
+ }
public void connectionLost(Throwable cause) {
- // 杩炴帴涓㈠け鍚庯紝涓�鑸湪杩欓噷闈㈣繘琛岄噸杩�
- Client.lianjiemqqt();
-
+ // 杩炴帴涓㈠け鍚庯紝璁板綍鏃ュ織骞惰Е鍙戦噸杩炴満鍒�
+ if (cause != null) {
+ System.err.println("MQTT杩炴帴鏂紑锛屽師鍥�: " + cause.getMessage());
+ } else {
+ System.err.println("MQTT杩炴帴鏂紑锛屽皢鑷姩閲嶈繛...");
+ }
+
+ // 鍚姩閲嶈繛绾跨▼锛屽湪鑷姩閲嶈繛鎴愬姛鍚庨噸鏂拌闃�
+ if (clientInstance != null && topic != null) {
+ new Thread(() -> {
+ reconnectAndResubscribe();
+ }).start();
+ }
+ }
+
+ /**
+ * 閲嶈繛骞堕噸鏂拌闃�
+ */
+ private void reconnectAndResubscribe() {
+ int maxRetries = 30; // 鏈�澶氶噸璇�30娆�
+ int retryCount = 0;
+ long retryInterval = 2000; // 姣忔閲嶈瘯闂撮殧2绉�
+
+ while (retryCount < maxRetries && clientInstance != null) {
+ try {
+ Thread.sleep(retryInterval);
+
+ // 妫�鏌ユ槸鍚﹀凡閲嶈繛
+ if (clientInstance.isConnected()) {
+ try {
+ // 閲嶆柊璁㈤槄涓婚
+ clientInstance.getClient().subscribe(topic, qos);
+ System.out.println("MQTT閲嶈繛鎴愬姛锛屽凡閲嶆柊璁㈤槄涓婚: " + topic);
+ return; // 閲嶈繛鎴愬姛锛岄��鍑哄惊鐜�
+ } catch (Exception e) {
+ System.err.println("閲嶆柊璁㈤槄涓婚澶辫触: " + e.getMessage());
+ }
+ }
+
+ retryCount++;
+ if (retryCount % 5 == 0) {
+ System.out.println("绛夊緟MQTT鑷姩閲嶈繛... (" + retryCount + "/" + maxRetries + ")");
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ System.err.println("閲嶈繛绾跨▼琚腑鏂�");
+ return;
+ }
+ }
+
+ if (retryCount >= maxRetries) {
+ System.err.println("MQTT鑷姩閲嶈繛瓒呮椂锛屽皾璇曟墜鍔ㄩ噸杩�...");
+ // 濡傛灉鑷姩閲嶈繛澶辫触锛屽皾璇曟墜鍔ㄩ噸杩�
+ try {
+ if (clientInstance != null) {
+ clientInstance.connect();
+ if (clientInstance.isConnected()) {
+ clientInstance.subscribe(qos);
+ System.out.println("鎵嬪姩閲嶈繛鎴愬姛锛屽凡閲嶆柊璁㈤槄涓婚: " + topic);
+ }
+ }
+ } catch (Exception e) {
+ System.err.println("鎵嬪姩閲嶈繛澶辫触: " + e.getMessage());
+ }
+ }
}
public void deliveryComplete(IMqttDeliveryToken token) {
diff --git a/src/Mqttmessage/Util/DeviceMessageParser.java b/src/Mqttmessage/Util/DeviceMessageParser.java
index d09a56d..dc6a28a 100644
--- a/src/Mqttmessage/Util/DeviceMessageParser.java
+++ b/src/Mqttmessage/Util/DeviceMessageParser.java
@@ -50,68 +50,7 @@
return objectMapper.readValue(jsonStr, ResponseData.class);
}
- /**
- * 瑙f瀽GGA鏍煎紡鐨凣PS鏁版嵁锛堝彲閫夊姛鑳斤級
- * @param ggaString GGA鏍煎紡瀛楃涓�
- * @return GGAData瀵硅薄
- */
- /*public static GPSData.GGAData parseGGA(String ggaString) {
- // 绉婚櫎鍙兘鐨勬牎楠屽拰閮ㄥ垎
- String cleanString = ggaString.split("\\*")[0];
- String[] fields = cleanString.split(",");
- if (fields.length < 15) {
- return null;
- }
-
- GPSData.GGAData ggaData = new GPSData.GGAData();
-
- try {
- ggaData.setUtcTime(fields[1]);
- ggaData.setLatitude(fields[2]);
- ggaData.setLatitudeDir(fields[3]);
- ggaData.setLongitude(fields[4]);
- ggaData.setLongitudeDir(fields[5]);
-
- if (!fields[6].isEmpty()) {
- ggaData.setGpsQuality(Integer.parseInt(fields[6]));
- }
-
- if (!fields[7].isEmpty()) {
- ggaData.setSatellites(Integer.parseInt(fields[7]));
- }
-
- if (!fields[8].isEmpty()) {
- ggaData.setHdop(Double.parseDouble(fields[8]));
- }
-
- if (!fields[9].isEmpty()) {
- ggaData.setAltitude(Double.parseDouble(fields[9]));
- }
-
- ggaData.setAltitudeUnit(fields[10]);
-
- if (!fields[11].isEmpty()) {
- ggaData.setGeoidSep(Double.parseDouble(fields[11]));
- }
-
- ggaData.setGeoidSepUnit(fields[12]);
-
- if (fields.length > 13) {
- ggaData.setAge(fields[13]);
- }
-
- if (fields.length > 14) {
- ggaData.setStationId(fields[14]);
- }
-
- } catch (Exception e) {
- // 瑙f瀽寮傚父锛岃繑鍥為儴鍒嗚В鏋愮殑鏁版嵁鎴杗ull
- System.err.println("瑙f瀽GGA鏁版嵁鏃跺嚭閿�: " + e.getMessage());
- }
-
- return ggaData;
- }*/
/**
* 灏嗘秷鎭璞¤浆鎹负JSON瀛楃涓�
--
Gitblit v1.10.0