修改IOT客户端和代码结构
This commit is contained in:
parent
7ce75685aa
commit
35d61801fe
|
|
@ -218,45 +218,5 @@ public class ThingsBoardDomainImpl implements IThingsBoardDomain {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时任务:每隔1分钟打印所有设备信息
|
||||
* 执行时间:每分钟的第0秒执行
|
||||
*/
|
||||
@Scheduled(cron = "0 * * * * ?")
|
||||
public void printAllDevicesScheduled() {
|
||||
try {
|
||||
log.info("========== 开始执行定时任务:打印所有设备信息 ==========");
|
||||
|
||||
Iterable<List<DeviceInfo>> allDevices = getAllDevices();
|
||||
int totalCount = 0;
|
||||
|
||||
for (List<DeviceInfo> deviceBatch : allDevices) {
|
||||
for (DeviceInfo device : deviceBatch) {
|
||||
// 获取设备属性以获取活跃状态
|
||||
Boolean activeStatus = false;
|
||||
try {
|
||||
AttributeMap attributes = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.ruoyi.device.service.api;
|
||||
|
||||
/**
|
||||
* 设备服务
|
||||
*/
|
||||
public interface IDeviceService {
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
package com.ruoyi.device.service.impl;
|
||||
|
||||
import com.ruoyi.device.domain.api.IThingsBoardDomain;
|
||||
import com.ruoyi.device.domain.impl.ThingsBoardDomainImpl;
|
||||
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 com.ruoyi.device.service.api.IDeviceService;
|
||||
import com.ruoyi.device.service.api.IDeviceTempService;
|
||||
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 DeviceServiceImpl implements IDeviceService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DeviceServiceImpl.class);
|
||||
|
||||
private final IThingsBoardDomain iThingsBoardDomain;
|
||||
|
||||
public DeviceServiceImpl(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue