From df1cbe260ce35ef5c5f4381e6d475862f2ba16da Mon Sep 17 00:00:00 2001 From: yangxiaozhong <429869597@qq.com> Date: Fri, 12 Dec 2025 13:02:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BA=E5=8E=BB=E4=B8=8A=E4=BC=A0=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=EF=BC=8C=E4=BB=A5=E5=8F=8A=E8=AE=BE=E5=A4=87=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E7=BA=BF=E7=8A=B6=E6=80=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/HazardMqttMessageHandler.java | 25 ++++++++++++++++++- .../hazard/service/IIotDeviceService.java | 3 +++ .../service/impl/IotDeviceServiceImpl.java | 5 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) 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); + } }