|
|
|
@ -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<String,Object> 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<String,Object> 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("未开启设备连接状态推送");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|