feat:【IoT 物联网】新增全局配置类 YudaoIotProperties,优化设备离线检测逻辑
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* iot 模块的【全局】拓展封装
|
||||
*/
|
||||
package cn.iocoder.yudao.module.iot.framework.iot;
|
||||
@@ -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 <a href="https://help.aliyun.com/zh/iot/support/faq-about-device-status#98f7056b2957y">阿里云 IoT —— 设备离线分析</a>
|
||||
* @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<Long> timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime(
|
||||
LocalDateTime.now().minus(OFFLINE_TIMEOUT));
|
||||
Set<Long> timeoutDeviceIds = devicePropertyService.getDeviceIdListByReportTime(getTimeoutTime());
|
||||
|
||||
// 2. 下线设备
|
||||
List<String[]> 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())));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ public enum IotDeviceMessageMethodEnum implements ArrayValuable<String> {
|
||||
|
||||
STATE_UPDATE("thing.state.update", "设备状态更新", true),
|
||||
|
||||
// TODO 芋艿:要不要加个 ping 消息;
|
||||
|
||||
// ========== 设备属性 ==========
|
||||
// 可参考:https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.gateway.codec.modbus;
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* TODO @芋艿:实现一个 alink 的 xml 版本
|
||||
*/
|
||||
package cn.iocoder.yudao.module.iot.gateway.codec.simple;
|
||||
Reference in New Issue
Block a user