From 3535fda9e153f503e2d1951e710c0c21db7bb80e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 28 Jun 2025 22:09:45 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E3=80=90IoT=20=E7=89=A9=E8=81=94?= =?UTF-8?q?=E7=BD=91=E3=80=91=E6=96=B0=E5=A2=9E=E5=85=A8=E5=B1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=B1=BB=20YudaoIotProperties=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=AE=BE=E5=A4=87=E7=A6=BB=E7=BA=BF=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/config/YudaoIotProperties.java | 28 +++++++++++++++++++ .../iot/framework/iot/package-info.java | 4 +++ .../job/device/IotDeviceOfflineCheckJob.java | 18 ++++++------ .../enums/IotDeviceMessageMethodEnum.java | 2 ++ .../gateway/codec/modbus/package-info.java | 1 - .../gateway/codec/simple/package-info.java | 4 +++ 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/config/YudaoIotProperties.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/package-info.java delete mode 100644 yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/modbus/package-info.java create mode 100644 yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/simple/package-info.java diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/config/YudaoIotProperties.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/config/YudaoIotProperties.java new file mode 100644 index 0000000000..07473c0293 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/config/YudaoIotProperties.java @@ -0,0 +1,28 @@ +package cn.iocoder.yudao.module.iot.framework.iot.config; + +import lombok.Data; +import org.springframework.stereotype.Component; + +import java.time.Duration; + +/** + * 芋道 IoT 全局配置类 + * + * @author 芋道源码 + */ +@Component +@Data +public class YudaoIotProperties { + + /** + * 设备连接超时时间 + */ + private Duration keepAliveTime = Duration.ofMinutes(10); + /** + * 设备连接超时时间的因子 + * + * 因为设备可能会有网络抖动,所以需要乘以一个因子,避免误判 + */ + private double keepAliveFactor = 1.5D; + +} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/package-info.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/package-info.java new file mode 100644 index 0000000000..0930a1409c --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/package-info.java @@ -0,0 +1,4 @@ +/** + * iot 模块的【全局】拓展封装 + */ +package cn.iocoder.yudao.module.iot.framework.iot; \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/device/IotDeviceOfflineCheckJob.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/device/IotDeviceOfflineCheckJob.java index b14beafe23..6bd27a679a 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/device/IotDeviceOfflineCheckJob.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/device/IotDeviceOfflineCheckJob.java @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; import cn.iocoder.yudao.module.iot.core.enums.IotDeviceStateEnum; import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; +import cn.iocoder.yudao.module.iot.framework.iot.config.YudaoIotProperties; import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; import cn.iocoder.yudao.module.iot.service.device.message.IotDeviceMessageService; import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; @@ -24,17 +25,14 @@ import java.util.Set; * * 检测逻辑:设备最后一条 {@link IotDeviceMessage} 消息超过一定时间,则认为设备离线 * + * @see 阿里云 IoT —— 设备离线分析 * @author 芋道源码 */ @Component public class IotDeviceOfflineCheckJob implements JobHandler { - /** - * 设备离线超时时间 - * - * TODO 芋艿:暂定 10 分钟,后续看看要不要基于设备或者全局有配置文件 - */ - public static final Duration OFFLINE_TIMEOUT = Duration.ofMinutes(10); + @Resource + private YudaoIotProperties iotProperties; @Resource private IotDeviceService deviceService; @@ -52,8 +50,7 @@ public class IotDeviceOfflineCheckJob implements JobHandler { return JsonUtils.toJsonString(Collections.emptyList()); } // 1.2 获取超时的设备集合 - Set timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime( - LocalDateTime.now().minus(OFFLINE_TIMEOUT)); + Set timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime(getTimeoutTime()); // 2. 下线设备 List offlineDevices = CollUtil.newArrayList(); @@ -68,4 +65,9 @@ public class IotDeviceOfflineCheckJob implements JobHandler { return JsonUtils.toJsonString(offlineDevices); } + private LocalDateTime getTimeoutTime() { + return LocalDateTime.now().minus(Duration.ofNanos( + (long) (iotProperties.getKeepAliveTime().toNanos() * iotProperties.getKeepAliveFactor()))); + } + } diff --git a/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/enums/IotDeviceMessageMethodEnum.java b/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/enums/IotDeviceMessageMethodEnum.java index cb343e33ec..fddf155a08 100644 --- a/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/enums/IotDeviceMessageMethodEnum.java +++ b/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/enums/IotDeviceMessageMethodEnum.java @@ -21,6 +21,8 @@ public enum IotDeviceMessageMethodEnum implements ArrayValuable { STATE_UPDATE("thing.state.update", "设备状态更新", true), + // TODO 芋艿:要不要加个 ping 消息; + // ========== 设备属性 ========== // 可参考:https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/modbus/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/modbus/package-info.java deleted file mode 100644 index 5e4835da78..0000000000 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/modbus/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.yudao.module.iot.gateway.codec.modbus; \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/simple/package-info.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/simple/package-info.java new file mode 100644 index 0000000000..5bd676ad1a --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/simple/package-info.java @@ -0,0 +1,4 @@ +/** + * TODO @芋艿:实现一个 alink 的 xml 版本 + */ +package cn.iocoder.yudao.module.iot.gateway.codec.simple; \ No newline at end of file