From 6b828ba1310db528aa8172bd14a0253ebca5a844 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 10 六月 2025 18:34:29 +0800
Subject: [PATCH] 基于mqtt的水肥机、气象站、墒情站协议、功能模块继续开发

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/mqtt/MqttManager.java |   44 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/mqtt/MqttManager.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/mqtt/MqttManager.java
index 3c775b5..ac3e211 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/mqtt/MqttManager.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/mqtt/MqttManager.java
@@ -1,6 +1,11 @@
 package com.dy.rtuMw.server.mqtt;
 
 import com.dy.common.mw.channel.mqtt.MqttClientPool;
+import com.dy.common.mw.protocol4Mqtt.MqttNotify;
+import com.dy.common.mw.protocol4Mqtt.MqttNotifyInfo;
+import com.dy.common.mw.protocol4Mqtt.status.DevOnLineSt;
+import com.dy.common.mw.protocol4Mqtt.status.DevRunSt;
+import com.dy.rtuMw.server.ServerProperties;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.eclipse.paho.client.mqttv3.MqttClient;
@@ -33,13 +38,17 @@
         this.configVo = configVo;
     }
 
+    /**
+     * 鍒涘缓杩炴帴姹� + 璁㈤槄涓婚
+     * @throws Exception
+     */
     public void start()throws Exception{
         String URL = "tcp://" + this.configVo.svIp + ":" + this.configVo.svPort;
         this.pool = new MqttClientPool(URL, this.configVo.svUserName, this.configVo.svUserPassword, this.configVo.poolMaxSize);
         if(this.pool.isClose()){
             throw new Exception("Mqtt杩炴帴姹犲垵濮嬪寲澶辫触");
         }
-        MqttClient clientSub = null ;
+        MqttClient clientSub ;
         try {
             clientSub = pool.popClient();//鏂板垱寤轰竴涓狢lient鏃讹紝姝lient瀹為檯鍘昏繛鎺QTT鏈嶅姟鍣紝濡傛灉杩炴帴涓嶄笂锛屽氨浼氭姏鍑哄紓甯�
         }catch (Exception e){
@@ -48,8 +57,35 @@
         if(clientSub == null || !clientSub.isConnected()){
             throw new Exception("Mqtt杩炴帴姹犺幏寰楄闃呰繛鎺ヤ笉鍙敤");
         }
+        // 璁㈤槄涓婚
         for(int i = 0; i < this.configVo.subTopics.length; i++){
-            clientSub.subscribe(this.configVo.subTopics[i], this.configVo.topicsQos[i], new MqttMessageListener());
+            for(int j = 0 ; j < this.configVo.protocolAndDeviceIds.length; j++){
+                clientSub.subscribe(ServerProperties.orgTag + "/"
+                        + this.configVo.protocolAndDeviceIds[j] + "/"
+                        + this.configVo.subTopics[i],
+                        this.configVo.subTopicsQos[i],
+                        //姣忎竴涓闃呬富棰橀兘鏈変竴涓狹qttMessageListener瀹炰緥
+                        new MqttMessageListener(new MqttNotify(){
+                            @Override
+                            public void notify(String devId, MqttNotifyInfo... infos) {
+                                if(devId != null && infos != null && infos.length > 0){
+                                    for(MqttNotifyInfo info : infos){
+                                        if(info instanceof DevOnLineSt){
+                                            DevOnLineSt onLineSt = (DevOnLineSt)info;
+                                            if(onLineSt.onLine != null && onLineSt.onLine.booleanValue()){
+                                                DevStatusDealer.onLine(devId, ((DevOnLineSt)info).protocol);
+                                            }else{
+                                                DevStatusDealer.offLine(devId);
+                                            }
+                                        } else if(info instanceof DevRunSt){
+                                            DevStatusDealer.setStatus(devId, (DevRunSt)info);
+                                        }
+                                    }
+                                }
+                            }
+                        })
+                );
+            }
         }
     }
 
@@ -69,12 +105,12 @@
     }
 
     public void publishMsg(MqttClient client, String topic, byte[] msg) throws Exception{
-        client.publish(topic, msg, this.configVo.publishQos, false);
+        client.publish(topic, msg, this.configVo.pubTopicQos, false);
     }
 
     public void publishMsg(MqttClient client, String topic, String msg) throws Exception{
         byte[] bs = msg.getBytes("UTF-8") ;
-        client.publish(topic, bs, this.configVo.publishQos, false);
+        client.publish(topic, bs, this.configVo.pubTopicQos, false);
     }
 
     public boolean poolIsClose(){

--
Gitblit v1.8.0