|
|
|
|
@ -6,7 +6,10 @@ import org.dromara.common.json.utils.JsonUtils;
|
|
|
|
|
import org.dromara.common.mqtt.handler.MqttMessageHandler;
|
|
|
|
|
import org.dromara.common.mqtt.server.MqttGateway;
|
|
|
|
|
import org.dromara.common.sse.dto.SseMessageDto;
|
|
|
|
|
import org.dromara.common.sse.enums.SseEnums;
|
|
|
|
|
import org.dromara.common.sse.utils.SseMessageUtils;
|
|
|
|
|
import org.dromara.hazard.domain.IotDevice;
|
|
|
|
|
import org.dromara.hazard.domain.bo.IotDeviceBo;
|
|
|
|
|
import org.dromara.hazard.domain.bo.IotSensorDataBo;
|
|
|
|
|
import org.dromara.hazard.domain.dto.Payload;
|
|
|
|
|
import org.dromara.hazard.domain.dto.SensorData;
|
|
|
|
|
@ -18,6 +21,7 @@ import org.dromara.hazard.service.IIotSensorService;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
@ -143,7 +147,8 @@ public class HazardMqttMessageHandler implements MqttMessageHandler {
|
|
|
|
|
messageData.setValue(dataBo.getValue());
|
|
|
|
|
messageData.setType(iotSensorVo.getType());
|
|
|
|
|
String jsonStr = JSONUtil.toJsonStr(messageData);
|
|
|
|
|
dto.setMessage(topic+jsonStr);
|
|
|
|
|
dto.setMessage(jsonStr);
|
|
|
|
|
dto.setEvent(SseEnums.Event.HAZARD.getEvent());
|
|
|
|
|
// 如果需要指定特定用户接收消息,可以设置userIds
|
|
|
|
|
// dto.setUserIds(List.of(userId));
|
|
|
|
|
SseMessageUtils.publishMessage(dto);
|
|
|
|
|
@ -167,8 +172,26 @@ public class HazardMqttMessageHandler implements MqttMessageHandler {
|
|
|
|
|
String sign = strings.get(0);
|
|
|
|
|
if ("OFFLINE".equals(sign)) {
|
|
|
|
|
log.info("设备 {} 下线。", strings.get(1));
|
|
|
|
|
//修改设备状态为下线状态
|
|
|
|
|
IotDeviceVo deviceVo = iotDeviceService.queryByDeviceCode(strings.get(1));
|
|
|
|
|
if (deviceVo != null){
|
|
|
|
|
deviceVo.setStatus("0");
|
|
|
|
|
IotDevice iotDeviceBo = new IotDevice();
|
|
|
|
|
BeanUtils.copyProperties(deviceVo, iotDeviceBo);
|
|
|
|
|
iotDeviceService.updateStatusByBo(iotDeviceBo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else if ("ONLINE".equals(sign)) {
|
|
|
|
|
log.info("设备 {} 上线。", strings.get(1));
|
|
|
|
|
//修改设备状态为上线状态
|
|
|
|
|
IotDeviceVo deviceVo = iotDeviceService.queryByDeviceCode(strings.get(1));
|
|
|
|
|
if (deviceVo != null){
|
|
|
|
|
deviceVo.setStatus("1");
|
|
|
|
|
IotDevice iotDeviceBo = new IotDevice();
|
|
|
|
|
BeanUtils.copyProperties(deviceVo, iotDeviceBo);
|
|
|
|
|
iotDeviceService.updateStatusByBo(iotDeviceBo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|