change - broker 参数配置修改,增加设备连接状态推送配置

dev
Wen JY 1 year ago
parent 8c12296969
commit 26e2fe0b4e

@ -35,7 +35,7 @@ public class MqttAuthHandler implements IMqttServerAuthHandler {
*/ */
private List<DeviceInfoDto> deviceInfoDtos; private List<DeviceInfoDto> deviceInfoDtos;
@Value("${mqtt.whitelist}") @Value("${broker.whitelist}")
private String clientIdWhitelist; private String clientIdWhitelist;
private final StringRedisTemplate redisTemplate; private final StringRedisTemplate redisTemplate;

@ -27,6 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -53,6 +54,12 @@ public class MqttConnectStatusListener implements IMqttConnectStatusListener, Sm
private MqttServerCreator serverCreator; private MqttServerCreator serverCreator;
private MqttServerTemplate mqttServerTemplate; private MqttServerTemplate mqttServerTemplate;
@Value("${broker.connectStatus.enable}")
private boolean publishConnectStatusEnable;
@Value("${broker.connectStatus.topic}")
private String publishConnectStatusTopic;
public MqttConnectStatusListener(ApplicationContext context, StringRedisTemplate redisTemplate) { public MqttConnectStatusListener(ApplicationContext context, StringRedisTemplate redisTemplate) {
this.context = context; this.context = context;
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
@ -99,6 +106,7 @@ public class MqttConnectStatusListener implements IMqttConnectStatusListener, Sm
* @param connectStatus * @param connectStatus
*/ */
public void pushConnectStatus(String clientId, ConnectStatus connectStatus){ public void pushConnectStatus(String clientId, ConnectStatus connectStatus){
if(publishConnectStatusEnable){
Map<String,Object> entityMap = new HashMap<>(); Map<String,Object> entityMap = new HashMap<>();
entityMap.put("msg","设备设备连接状态信息"); entityMap.put("msg","设备设备连接状态信息");
entityMap.put("deviceType", DeviceType.edge.getKey()); entityMap.put("deviceType", DeviceType.edge.getKey());
@ -106,11 +114,15 @@ public class MqttConnectStatusListener implements IMqttConnectStatusListener, Sm
entityMap.put("connectStatus",connectStatus.getKey()); entityMap.put("connectStatus",connectStatus.getKey());
entityMap.put("statusTime",System.currentTimeMillis()); entityMap.put("statusTime",System.currentTimeMillis());
String jsonString = JSONArray.toJSONString(entityMap); String jsonString = JSONArray.toJSONString(entityMap);
boolean result = mqttServerTemplate.publishAll("/device/status/v1", jsonString.getBytes(StandardCharsets.UTF_8)); boolean result = mqttServerTemplate.publishAll(publishConnectStatusTopic, jsonString.getBytes(StandardCharsets.UTF_8));
if(result){ if(result){
logger.info("客户端:"+clientId+""+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送成功"); logger.info("客户端:"+clientId+""+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送成功");
}else { }else {
logger.info("客户端:"+clientId+""+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送失败"); logger.info("客户端:"+clientId+""+ (connectStatus == ConnectStatus.connct ? "连接" :"断开") +"状态推送失败");
} }
}else {
logger.info("未开启设备连接状态推送");
}
} }
} }

@ -7,8 +7,15 @@ mqtt:
server: server:
port: 1883 # MQTT端口默认1883 port: 1883 # MQTT端口默认1883
web-port: 8083 # http、websocket 端口默认8083 web-port: 8083 # http、websocket 端口默认8083
#客户端ClientId白名单不进行接入校验
#broker相关配置
broker:
#客户端ClientId白名单不进行接入校验,多个ClientId用逗号隔开
whitelist: Test123,Test234,Test256 whitelist: Test123,Test234,Test256
#设备接入状态推送
connectStatus:
enable: true #是否启用设备连接状态推送
topic: /device/status/v1 #设备连接状态推送主题
# Spring # Spring
spring: spring:

Loading…
Cancel
Save