71 lines
3.0 KiB
Java
71 lines
3.0 KiB
Java
|
|
package com.ruoyi.device.service.impl;
|
|||
|
|
|
|||
|
|
import com.ruoyi.device.domain.api.IThingsBoardDomain;
|
|||
|
|
import com.ruoyi.device.domain.model.thingsboard.AttributeMap;
|
|||
|
|
import com.ruoyi.device.domain.model.thingsboard.DeviceInfo;
|
|||
|
|
import com.ruoyi.device.domain.model.thingsboard.constants.DeviceAttributes;
|
|||
|
|
import org.slf4j.Logger;
|
|||
|
|
import org.slf4j.LoggerFactory;
|
|||
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Optional;
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
public class SynService {
|
|||
|
|
|
|||
|
|
private static final Logger log = LoggerFactory.getLogger(SynService.class);
|
|||
|
|
|
|||
|
|
private final IThingsBoardDomain iThingsBoardDomain;
|
|||
|
|
|
|||
|
|
public SynService(IThingsBoardDomain iThingsBoardDomain) {
|
|||
|
|
this.iThingsBoardDomain = iThingsBoardDomain;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 定时任务:同步基础表数据
|
|||
|
|
* 执行时间:启动后1分钟开始,每2分钟执行一次(可通过配置文件修改)
|
|||
|
|
* 配置项:device.schedule.print-devices.initial-delay 初始延迟时间(毫秒)
|
|||
|
|
* device.schedule.print-devices.fixed-delay 执行间隔时间(毫秒)
|
|||
|
|
*/
|
|||
|
|
@Scheduled(initialDelayString = "${device.schedule.update-devices.initial-delay:60000}",
|
|||
|
|
fixedDelayString = "${device.schedule.update-devices.fixed-delay:120000}")
|
|||
|
|
public void updateDevicesScheduled() {
|
|||
|
|
try {
|
|||
|
|
log.info("========== 开始执行定时任务:同步基础表数据 ==========");
|
|||
|
|
|
|||
|
|
Iterable<List<DeviceInfo>> allDevices = iThingsBoardDomain.getAllDevices();
|
|||
|
|
int totalCount = 0;
|
|||
|
|
|
|||
|
|
for (List<DeviceInfo> deviceBatch : allDevices) {
|
|||
|
|
for (DeviceInfo device : deviceBatch) {
|
|||
|
|
// 获取设备属性以获取活跃状态
|
|||
|
|
Boolean activeStatus = false;
|
|||
|
|
try {
|
|||
|
|
AttributeMap attributes = iThingsBoardDomain.getDeviceAttributes(device.getId());
|
|||
|
|
// 尝试从 AttributeMap 中获取 active 属性
|
|||
|
|
Optional<Boolean> active = attributes.get(DeviceAttributes.ACTIVE);
|
|||
|
|
if (active.isPresent()) {
|
|||
|
|
activeStatus = active.get();
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.debug("获取设备 {} 的活跃状态失败: {}", device.getId(), e.getMessage());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log.info("Device Name: {}, Device ID: {}, Device Type: {}, Active: {}",
|
|||
|
|
device.getName(),
|
|||
|
|
device.getId(),
|
|||
|
|
device.getType(),
|
|||
|
|
activeStatus);
|
|||
|
|
totalCount++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log.info("========== 定时任务执行完成,共打印 {} 个设备 ==========", totalCount);
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.error("定时任务执行失败: {}", e.getMessage(), e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|