From 1c8e32b4a45c1865e2a422e9949c1e996df861a6 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 16:01:17 +0800
Subject: [PATCH] 新增了凸形有障碍物地块路径规划未完成

---
 src/Mqttmessage/PushCallback.java |   91 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 87 insertions(+), 4 deletions(-)

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) {

--
Gitblit v1.10.0