diff --git a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/handler/HazardMqttMessageHandler.java b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/handler/HazardMqttMessageHandler.java index 8be9436c7..9f017c860 100644 --- a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/handler/HazardMqttMessageHandler.java +++ b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/handler/HazardMqttMessageHandler.java @@ -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); + } + } } } diff --git a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/IIotDeviceService.java b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/IIotDeviceService.java index 2465a6c05..f6e6e6d40 100644 --- a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/IIotDeviceService.java +++ b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/IIotDeviceService.java @@ -1,5 +1,6 @@ package org.dromara.hazard.service; +import org.dromara.hazard.domain.IotDevice; import org.dromara.hazard.domain.vo.IotDeviceVo; import org.dromara.hazard.domain.bo.IotDeviceBo; import org.dromara.common.mybatis.core.page.TableDataInfo; @@ -67,4 +68,6 @@ public interface IIotDeviceService { Boolean deleteWithValidByIds(Collection ids, Boolean isValid); IotDeviceVo queryByDeviceCode(String deviceCode); + + void updateStatusByBo(IotDevice iotDeviceBo); } diff --git a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/impl/IotDeviceServiceImpl.java b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/impl/IotDeviceServiceImpl.java index 025dba354..d2036a71f 100644 --- a/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/impl/IotDeviceServiceImpl.java +++ b/ruoyi-modules/ruoyi-hazard/src/main/java/org/dromara/hazard/service/impl/IotDeviceServiceImpl.java @@ -221,4 +221,9 @@ public class IotDeviceServiceImpl implements IIotDeviceService { public IotDeviceVo queryByDeviceCode(String deviceCode) { return baseMapper.selectVoOne(new LambdaQueryWrapper().eq(IotDevice::getDeviceCode, deviceCode)); } + + @Override + public void updateStatusByBo(IotDevice iotDevice) { + baseMapper.updateById(iotDevice); + } }