From d71e3a495a125811889fdf7c2a2ec0650e28262d Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期四, 25 十二月 2025 17:41:21 +0800
Subject: [PATCH] 最新版本541200007最新版本541200007

---
 server/visits/device_service/src/main/java/com/doumee/mqtt/service/MqttToolService.java |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/server/visits/device_service/src/main/java/com/doumee/mqtt/service/MqttToolService.java b/server/visits/device_service/src/main/java/com/doumee/mqtt/service/MqttToolService.java
new file mode 100644
index 0000000..47b01e6
--- /dev/null
+++ b/server/visits/device_service/src/main/java/com/doumee/mqtt/service/MqttToolService.java
@@ -0,0 +1,109 @@
+package com.doumee.mqtt.service;
+
+import com.doumee.mqtt.config.MqttClientInit;
+import com.doumee.mqtt.config.MqttConfig;
+import lombok.Data;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+
+@Data
+public class MqttToolService {
+    /**
+     * 璁㈤槄娑堟伅锛屽惎鍔ㄥ姞杞戒竴娆�
+     * @param topics
+     */
+    public static MqttClient subscribe(MqttClient clientTemp,MqttConfig config,String[]  topics, MqttCallback callBack) {
+        try {
+            //璁㈤槄娑堟伅
+            int[] Qos = new int[topics.length];//0锛氭渶澶氫竴娆� 銆�1锛氭渶灏戜竴娆� 銆�2锛氬彧鏈変竴娆�
+            for (int i = 0; i < Qos.length; i++) {
+                Qos[i] = 1;
+                /*if(i ==2){
+                    Qos[i] = 2;
+                }else{
+                    Qos[i] = 1;
+                }*/
+            }
+            clientTemp =  MqttClientInit.getInstance(clientTemp,config,callBack);
+            try {
+                clientTemp.subscribe(topics, Qos);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+            return clientTemp;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return clientTemp;
+    }
+    public static MqttClient unsubscribe(MqttClient clientTemp,MqttConfig config,String[]  topics, MqttCallback callBack) {
+        try {
+            //璁㈤槄娑堟伅
+            int[] Qos = new int[topics.length];//0锛氭渶澶氫竴娆� 銆�1锛氭渶灏戜竴娆� 銆�2锛氬彧鏈変竴娆�
+            for (int i = 0; i < Qos.length; i++) {
+                Qos[i] = 1;
+                /*if(i ==2){
+                    Qos[i] = 2;
+                }else{
+                    Qos[i] = 1;
+                }*/
+            }
+            clientTemp =  MqttClientInit.getInstance(clientTemp,config,callBack);
+            try {
+                clientTemp.unsubscribe(topics);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+            return clientTemp;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return clientTemp;
+    }
+    /**
+     * 娑堟伅鍙戦��
+     * @param message
+     * @param topic
+     */
+    public static MqttClient pubMessage(MqttClient clientTemp,MqttConfig config,String message,String topic, MqttCallback callBack){
+        try {
+            MqttMessage mess = new MqttMessage();
+            mess.setQos(1);
+            mess.setRetained(false);
+            mess.setPayload(message.getBytes());
+            clientTemp =  MqttClientInit.getInstance(clientTemp,config,callBack);
+            try {
+                clientTemp.publish(topic, mess);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+            return  clientTemp;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return clientTemp;
+    }
+    public static void main(String[] args) {
+        MqttConfig config = (new MqttConfig());
+        config.setHost("tcp://192.168.0.7:1883");
+        config.setClientid("doumee1");
+        config.setPassword("doumee@168");
+        config.setUsername("doumee");
+        config.setVersion("003");
+        MqttToolService.pubMessage(null,config,"{" +
+                "  \"method\": 3," +
+                "  \"dstID\" :43008827854690816," +
+                "  \"msgCode\": 120," +
+                "  \"timeMS\": 0," +
+                "  \"data\":" +
+                "   [{" +
+                "    \"tag\":305," +
+                "    \"cmd\":\"do_turn_off\", " +
+                "    \"count\":3," +
+                "    \"busAddr\":[3]" +
+                "   }]" +
+                "}","/dev/MTS/98CC4D121E5A/cmd/json",null);
+    }
+
+}

--
Gitblit v1.9.3