Commit 3535fda9e153f503e2d1951e710c0c21db7bb80e
1 parent
a3f58be5
feat:【IoT 物联网】新增全局配置类 YudaoIotProperties,优化设备离线检测逻辑
Showing
6 changed files
with
48 additions
and
9 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/config/YudaoIotProperties.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.framework.iot.config; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | +import org.springframework.stereotype.Component; | ||
| 5 | + | ||
| 6 | +import java.time.Duration; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 芋道 IoT 全局配置类 | ||
| 10 | + * | ||
| 11 | + * @author 芋道源码 | ||
| 12 | + */ | ||
| 13 | +@Component | ||
| 14 | +@Data | ||
| 15 | +public class YudaoIotProperties { | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * 设备连接超时时间 | ||
| 19 | + */ | ||
| 20 | + private Duration keepAliveTime = Duration.ofMinutes(10); | ||
| 21 | + /** | ||
| 22 | + * 设备连接超时时间的因子 | ||
| 23 | + * | ||
| 24 | + * 因为设备可能会有网络抖动,所以需要乘以一个因子,避免误判 | ||
| 25 | + */ | ||
| 26 | + private double keepAliveFactor = 1.5D; | ||
| 27 | + | ||
| 28 | +} |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/iot/package-info.java
0 → 100644
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; | @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; | ||
| 7 | import cn.iocoder.yudao.module.iot.core.enums.IotDeviceStateEnum; | 7 | import cn.iocoder.yudao.module.iot.core.enums.IotDeviceStateEnum; |
| 8 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | 8 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; |
| 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 10 | +import cn.iocoder.yudao.module.iot.framework.iot.config.YudaoIotProperties; | ||
| 10 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; | 11 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; |
| 11 | import cn.iocoder.yudao.module.iot.service.device.message.IotDeviceMessageService; | 12 | import cn.iocoder.yudao.module.iot.service.device.message.IotDeviceMessageService; |
| 12 | import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; | 13 | import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; |
| @@ -24,17 +25,14 @@ import java.util.Set; | @@ -24,17 +25,14 @@ import java.util.Set; | ||
| 24 | * | 25 | * |
| 25 | * 检测逻辑:设备最后一条 {@link IotDeviceMessage} 消息超过一定时间,则认为设备离线 | 26 | * 检测逻辑:设备最后一条 {@link IotDeviceMessage} 消息超过一定时间,则认为设备离线 |
| 26 | * | 27 | * |
| 28 | + * @see <a href="https://help.aliyun.com/zh/iot/support/faq-about-device-status#98f7056b2957y">阿里云 IoT —— 设备离线分析</a> | ||
| 27 | * @author 芋道源码 | 29 | * @author 芋道源码 |
| 28 | */ | 30 | */ |
| 29 | @Component | 31 | @Component |
| 30 | public class IotDeviceOfflineCheckJob implements JobHandler { | 32 | public class IotDeviceOfflineCheckJob implements JobHandler { |
| 31 | 33 | ||
| 32 | - /** | ||
| 33 | - * 设备离线超时时间 | ||
| 34 | - * | ||
| 35 | - * TODO 芋艿:暂定 10 分钟,后续看看要不要基于设备或者全局有配置文件 | ||
| 36 | - */ | ||
| 37 | - public static final Duration OFFLINE_TIMEOUT = Duration.ofMinutes(10); | 34 | + @Resource |
| 35 | + private YudaoIotProperties iotProperties; | ||
| 38 | 36 | ||
| 39 | @Resource | 37 | @Resource |
| 40 | private IotDeviceService deviceService; | 38 | private IotDeviceService deviceService; |
| @@ -52,8 +50,7 @@ public class IotDeviceOfflineCheckJob implements JobHandler { | @@ -52,8 +50,7 @@ public class IotDeviceOfflineCheckJob implements JobHandler { | ||
| 52 | return JsonUtils.toJsonString(Collections.emptyList()); | 50 | return JsonUtils.toJsonString(Collections.emptyList()); |
| 53 | } | 51 | } |
| 54 | // 1.2 获取超时的设备集合 | 52 | // 1.2 获取超时的设备集合 |
| 55 | - Set<Long> timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime( | ||
| 56 | - LocalDateTime.now().minus(OFFLINE_TIMEOUT)); | 53 | + Set<Long> timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime(getTimeoutTime()); |
| 57 | 54 | ||
| 58 | // 2. 下线设备 | 55 | // 2. 下线设备 |
| 59 | List<String[]> offlineDevices = CollUtil.newArrayList(); | 56 | List<String[]> offlineDevices = CollUtil.newArrayList(); |
| @@ -68,4 +65,9 @@ public class IotDeviceOfflineCheckJob implements JobHandler { | @@ -68,4 +65,9 @@ public class IotDeviceOfflineCheckJob implements JobHandler { | ||
| 68 | return JsonUtils.toJsonString(offlineDevices); | 65 | return JsonUtils.toJsonString(offlineDevices); |
| 69 | } | 66 | } |
| 70 | 67 | ||
| 68 | + private LocalDateTime getTimeoutTime() { | ||
| 69 | + return LocalDateTime.now().minus(Duration.ofNanos( | ||
| 70 | + (long) (iotProperties.getKeepAliveTime().toNanos() * iotProperties.getKeepAliveFactor()))); | ||
| 71 | + } | ||
| 72 | + | ||
| 71 | } | 73 | } |
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<String> { | @@ -21,6 +21,8 @@ public enum IotDeviceMessageMethodEnum implements ArrayValuable<String> { | ||
| 21 | 21 | ||
| 22 | STATE_UPDATE("thing.state.update", "设备状态更新", true), | 22 | STATE_UPDATE("thing.state.update", "设备状态更新", true), |
| 23 | 23 | ||
| 24 | + // TODO 芋艿:要不要加个 ping 消息; | ||
| 25 | + | ||
| 24 | // ========== 设备属性 ========== | 26 | // ========== 设备属性 ========== |
| 25 | // 可参考:https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services | 27 | // 可参考:https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services |
| 26 | 28 |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/modbus/package-info.java deleted
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/simple/package-info.java
0 → 100644