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/PushCallback.java | 100 +++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 91 insertions(+), 9 deletions(-)
diff --git a/src/Mqttmessage/PushCallback.java b/src/Mqttmessage/PushCallback.java
index ab97989..e124a95 100644
--- a/src/Mqttmessage/PushCallback.java
+++ b/src/Mqttmessage/PushCallback.java
@@ -1,22 +1,103 @@
package Mqttmessage;
-
-
-
import Mqttmessage.Util.DeviceMessageParser;
+import gecaoji.gecaojistatus;
import Mqttmessage.Entity.GPSData;
+import Mqttmessage.Entity.GPSData.StatusInfo;
+
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
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) {
- // 杩炴帴涓㈠け鍚庯紝涓�鑸湪杩欓噷闈㈣繘琛岄噸杩�
- System.out.println("杩炴帴鏂紑锛屽彲浠ュ仛閲嶈繛");
-
+ // 杩炴帴涓㈠け鍚庯紝璁板綍鏃ュ織骞惰Е鍙戦噸杩炴満鍒�
+ 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) {
@@ -30,6 +111,7 @@
//ResponseData responseData = DeviceMessageParser.parseResponseData(new String(message.getPayload()));//瑙f瀽鍝嶅簲鏁版嵁
String gpsRaw = gpsData.getGps_raw();
UDPServer.processSerialData(gpsRaw);
+ gecaojistatus.parseStatus(gpsData.getStatus());
}
}
--
Gitblit v1.10.0