From 1175f5fbe8fd832943880bfc37c0e2a451a0688a Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 19:34:38 +0800
Subject: [PATCH] 删除了几个类优化了路径生成的逻辑
---
src/Mqttmessage/PushCallback.java | 92 ++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 88 insertions(+), 4 deletions(-)
diff --git a/src/Mqttmessage/PushCallback.java b/src/Mqttmessage/PushCallback.java
index 0d646dc..08d523b 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) {
@@ -29,6 +112,7 @@
String gpsRaw = gpsData.getGps_raw();
UDPServer.processSerialData(gpsRaw);
gecaojistatus.parseStatus(gpsData.getStatus());
+ gpsData.getImu_data();
}
}
--
Gitblit v1.10.0