From 5ae9bbe3583384afab8eb95a134ccb74aee6487a Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 13:46:38 +0800
Subject: [PATCH] 曾加修改密码功能

---
 src/Mqttmessage/Client.java |  308 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 287 insertions(+), 21 deletions(-)

diff --git a/src/Mqttmessage/Client.java b/src/Mqttmessage/Client.java
index 60be1c3..de7d4a8 100644
--- a/src/Mqttmessage/Client.java
+++ b/src/Mqttmessage/Client.java
@@ -3,6 +3,7 @@
 import org.eclipse.paho.client.mqttv3.MqttClient;
 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 
 import set.Setsys;
 import user.Usrdell;
@@ -19,6 +20,10 @@
     private MqttClient client;
     private MqttConnectOptions options;
     
+    // 闈欐�佸彉閲忕敤浜庡瓨鍌ㄥ鎴风瀹炰緥
+    private static Client gpsClient;
+    private static Client responseClient;
+    
     /**
      * 鏋勯�犲嚱鏁�
      * @param host MQTT鏈嶅姟鍣ㄥ湴鍧�锛屾牸寮忥細tcp://ip:port
@@ -26,12 +31,19 @@
      * @param clientId 瀹㈡埛绔疘D锛屼笉鑳介噸澶�
      */
     public Client(String host, String topic, String clientId) {
-
         this.host = host;
         this.topic = topic;
         this.clientId = clientId;
         this.options = new MqttConnectOptions();
         this.options.setCleanSession(true);
+        // 璁剧疆杩炴帴瓒呮椂鏃堕棿锛堢锛�
+        this.options.setConnectionTimeout(30);
+        // 璁剧疆KeepAlive闂撮殧锛堢锛夛紝鐢ㄤ簬淇濇寔杩炴帴娲昏穬
+        this.options.setKeepAliveInterval(60);
+        // 璁剧疆鑷姩閲嶈繛
+        this.options.setAutomaticReconnect(true);
+        // 璁剧疆MQTT鐗堟湰锛屼娇鐢�3.1.1
+        this.options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
     }
     
     /**
@@ -40,11 +52,28 @@
      */
     public void connect() throws MqttException {
         if (client != null && client.isConnected()) {
+            System.out.println("MQTT瀹㈡埛绔凡杩炴帴锛孋lientId: " + clientId);
             return;
         }
-        client = new MqttClient(host, clientId);
-        client.connect(options);
+        
+        // 濡傛灉瀹㈡埛绔凡瀛樺湪浣嗘湭杩炴帴锛屽厛鍏抽棴
+        if (client != null) {
+            try {
+                client.close();
+            } catch (Exception e) {
+                // 蹇界暐鍏抽棴鏃剁殑寮傚父
+            }
+            client = null;
+        }
+        
+        // 浣跨敤鍐呭瓨鎸佷箙鍖栵紝閬垮厤鏂囦欢閿佸畾闂
+        client = new MqttClient(host, clientId, new MemoryPersistence());
+        // 鍏堣缃洖璋冿紝鍐嶈繛鎺�
         client.setCallback(new PushCallback());
+        
+        // 鎵ц杩炴帴
+        client.connect(options);
+        System.out.println("MQTT杩炴帴鎴愬姛锛丆lientId: " + clientId + ", 鏈嶅姟鍣�: " + host + ", 涓婚: " + topic);
     }
     
     /**
@@ -78,6 +107,23 @@
     }
     
     /**
+     * 鍏抽棴瀹㈡埛绔苟閲婃斁璧勬簮
+     */
+    public void close() {
+        try {
+            if (client != null) {
+                if (client.isConnected()) {
+                    client.disconnect();
+                }
+                client.close();
+                client = null;
+            }
+        } catch (Exception e) {
+            // 蹇界暐鍏抽棴鏃剁殑寮傚父
+        }
+    }
+    
+    /**
      * 妫�鏌ユ槸鍚﹀凡杩炴帴
      * @return true琛ㄧず宸茶繛鎺ワ紝false琛ㄧず鏈繛鎺�
      */
@@ -94,29 +140,249 @@
     }
     
     /**
-     * 绀轰緥鐢ㄦ硶
+     * 杩炴帴MQTT鏈嶅姟鍣ㄧ殑宸ュ叿鏂规硶
+     * 渚涘叾浠栫被鐩存帴璋冪敤锛岃繛鎺PS涓婚鍜屽搷搴斾富棰�
+     * @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
      */
-
-    public static void lianjiemqqt()  {
+    public static boolean connectMQTT() {
+        // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
+        disconnectAll();
+        
+        boolean gpsSuccess = false;
+        boolean responseSuccess = false;
+        
         try {
             String host = "tcp://39.99.43.227:1883";
-            String deiveID=Setsys.getMowerIdValue();
-            String clientId =Usrdell.getUserEmail()+"mower";
-            String clientId2 =Usrdell.getUserEmail()+"response";
-            String topic = "mower/"+deiveID+"/gps";
-            String topic2 = "mower/"+deiveID+"/response";
-            Client mqttClient = new Client(host, topic, clientId);
-            Client mqttClient1 = new Client(host, topic2, clientId2);
-            mqttClient.connect();
-            mqttClient.subscribe();
+            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";
+            
+            // 杩炴帴GPS涓婚
+            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 (e.getCause() != null) {
+                    System.err.println("澶辫触鍘熷洜: " + e.getCause().getMessage());
+                }
+                e.printStackTrace();
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                System.err.println("杩炴帴杩囩▼琚腑鏂�");
+            }
 
-            mqttClient1.connect();
-            mqttClient1.subscribe();
+            // 杩炴帴鍝嶅簲涓婚
+            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("杩炴帴杩囩▼琚腑鏂�");
+            }
 
-            // 淇濇寔绋嬪簭杩愯
-//           Thread.sleep(Long.MAX_VALUE);
-        } catch (MqttException e) {
-            throw new RuntimeException(e);
+            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;
         }
     }
+    
+    /**
+     * 杩炴帴MQTT鏈嶅姟鍣ㄧ殑宸ュ叿鏂规硶锛堝甫鍙傛暟鐗堟湰锛�
+     * @param host MQTT鏈嶅姟鍣ㄥ湴鍧�锛屾牸寮忥細tcp://ip:port
+     * @param deviceId 璁惧ID
+     * @param userEmail 鐢ㄦ埛閭
+     * @return true琛ㄧず杩炴帴鎴愬姛锛宖alse琛ㄧず杩炴帴澶辫触
+     */
+    public static boolean connectMQTT(String host, String deviceId, String userEmail) {
+        // 鍏堟柇寮�涔嬪墠鐨勮繛鎺�
+        disconnectAll();
+        
+        boolean gpsSuccess = false;
+        boolean responseSuccess = false;
+        
+        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";
+            
+            // 杩炴帴GPS涓婚
+            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 (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);
+                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;
+        }
+    }
+    
+    /**
+     * 鍒涘缓骞惰繛鎺QTT瀹㈡埛绔殑宸ュ叿鏂规硶
+     * @param host MQTT鏈嶅姟鍣ㄥ湴鍧�
+     * @param topic 璁㈤槄涓婚
+     * @param clientId 瀹㈡埛绔疘D
+     * @param qos 鏈嶅姟璐ㄩ噺绛夌骇锛岄粯璁�2
+     * @return Client瀹炰緥锛岃繛鎺ュけ璐ヨ繑鍥瀗ull
+     */
+    public static Client createAndConnect(String host, String topic, String clientId, int qos) {
+        try {
+            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);
+            e.printStackTrace();
+            return null;
+        }
+    }
+    
+    /**
+     * 鍒涘缓骞惰繛鎺QTT瀹㈡埛绔殑宸ュ叿鏂规硶锛堥粯璁oS涓�2锛�
+     * @param host MQTT鏈嶅姟鍣ㄥ湴鍧�
+     * @param topic 璁㈤槄涓婚
+     * @param clientId 瀹㈡埛绔疘D
+     * @return Client瀹炰緥锛岃繛鎺ュけ璐ヨ繑鍥瀗ull
+     */
+    public static Client createAndConnect(String host, String topic, String clientId) {
+        return createAndConnect(host, topic, clientId, 2);
+    }
+    
+    /**
+     * 鏂紑鎵�鏈塎QTT杩炴帴
+     */
+    public static void disconnectAll() {
+        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) {
+            System.err.println("鏂紑MQTT杩炴帴澶辫触: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+    
+    /**
+     * 鑾峰彇GPS瀹㈡埛绔疄渚�
+     * @return GPS瀹㈡埛绔疄渚�
+     */
+    public static Client getGpsClient() {
+        return gpsClient;
+    }
+    
+    /**
+     * 鑾峰彇鍝嶅簲瀹㈡埛绔疄渚�
+     * @return 鍝嶅簲瀹㈡埛绔疄渚�
+     */
+    public static Client getResponseClient() {
+        return responseClient;
+    }
+    
+    /**
+     * 妫�鏌QTT杩炴帴鐘舵�侊紙闈欐�佹柟娉曪級
+     * @return true琛ㄧず宸茶繛鎺ワ紝false琛ㄧず鏈繛鎺�
+     */
+    public static boolean areClientsConnected() {
+        boolean gpsConnected = gpsClient != null && gpsClient.isConnected();
+        boolean responseConnected = responseClient != null && responseClient.isConnected();
+        return gpsConnected || responseConnected;
+    }
+    
+    /**
+     * 绀轰緥鐢ㄦ硶锛堜繚鐣欏悜鍚庡吋瀹癸級
+     * @deprecated 璇蜂娇鐢� connectMQTT() 鏂规硶鏇夸唬
+     */
+    @Deprecated
+    public static void lianjiemqqt() {
+        connectMQTT();
+    }
 }
\ No newline at end of file

--
Gitblit v1.10.0