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: