From 26e2fe0b4ec1a006926433e1522f45796a42986a Mon Sep 17 00:00:00 2001 From: Wen JY Date: Thu, 14 Sep 2023 09:24:34 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20broker=20=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hw/mqtt/auth/MqttAuthHandler.java | 2 +- .../listener/MqttConnectStatusListener.java | 34 +++++++++++++------ .../src/main/resources/bootstrap.yml | 9 ++++- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java index 9b8a918..6a3dec9 100644 --- a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java +++ b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java @@ -35,7 +35,7 @@ public class MqttAuthHandler implements IMqttServerAuthHandler { */ private List deviceInfoDtos; - @Value("${mqtt.whitelist}") + @Value("${broker.whitelist}") private String clientIdWhitelist; private final StringRedisTemplate redisTemplate; diff --git a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/listener/MqttConnectStatusListener.java b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/listener/MqttConnectStatusListener.java index 56a31b0..2634bdc 100644 --- a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/listener/MqttConnectStatusListener.java +++ b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/listener/MqttConnectStatusListener.java @@ -27,6 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.SmartInitializingSingleton; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; @@ -53,6 +54,12 @@ public class MqttConnectStatusListener implements IMqttConnectStatusListener, Sm private MqttServerCreator serverCreator; private MqttServerTemplate mqttServerTemplate; + @Value("${broker.connectStatus.enable}") + private boolean publishConnectStatusEnable; + + @Value("${broker.connectStatus.topic}") + private String publishConnectStatusTopic; + public MqttConnectStatusListener(ApplicationContext context, StringRedisTemplate redisTemplate) { this.context = context; this.redisTemplate = redisTemplate; @@ -99,18 +106,23 @@ public class MqttConnectStatusListener implements IMqttConnectStatusListener, Sm * @param connectStatus */ public void pushConnectStatus(String clientId, ConnectStatus connectStatus){ - Map entityMap = new HashMap<>(); - entityMap.put("msg","设备设备连接状态信息"); - entityMap.put("deviceType", DeviceType.edge.getKey()); - entityMap.put("deviceCode",clientId); - entityMap.put("connectStatus",connectStatus.getKey()); - entityMap.put("statusTime",System.currentTimeMillis()); - String jsonString = JSONArray.toJSONString(entityMap); - boolean result = mqttServerTemplate.publishAll("/device/status/v1", jsonString.getBytes(StandardCharsets.UTF_8)); - if(result){ - logger.info("客户端:"+clientId+";"+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送成功"); + if(publishConnectStatusEnable){ + Map entityMap = new HashMap<>(); + entityMap.put("msg","设备设备连接状态信息"); + entityMap.put("deviceType", DeviceType.edge.getKey()); + entityMap.put("deviceCode",clientId); + entityMap.put("connectStatus",connectStatus.getKey()); + entityMap.put("statusTime",System.currentTimeMillis()); + String jsonString = JSONArray.toJSONString(entityMap); + boolean result = mqttServerTemplate.publishAll(publishConnectStatusTopic, jsonString.getBytes(StandardCharsets.UTF_8)); + if(result){ + logger.info("客户端:"+clientId+";"+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送成功"); + }else { + logger.info("客户端:"+clientId+";"+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送失败"); + } }else { - logger.info("客户端:"+clientId+";"+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送失败"); + logger.info("未开启设备连接状态推送"); } + } } diff --git a/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml b/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml index 94ae11f..3495892 100644 --- a/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml +++ b/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml @@ -7,8 +7,15 @@ mqtt: server: port: 1883 # MQTT端口,默认:1883 web-port: 8083 # http、websocket 端口,默认:8083 - #客户端ClientId白名单,不进行接入校验 + +#broker相关配置 +broker: + #客户端ClientId白名单,不进行接入校验,多个ClientId用逗号隔开 whitelist: Test123,Test234,Test256 + #设备接入状态推送 + connectStatus: + enable: true #是否启用设备连接状态推送 + topic: /device/status/v1 #设备连接状态推送主题 # Spring spring: