Compare commits
5 Commits
ac95f0a163
...
5fc82d4bc3
| Author | SHA1 | Date |
|---|---|---|
|
|
5fc82d4bc3 | |
|
|
6cad98b742 | |
|
|
054460beaf | |
|
|
c08855a3b5 | |
|
|
39f423dd28 |
|
|
@ -12,11 +12,16 @@ import org.springframework.scheduling.annotation.Scheduled;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.thingsboard.rest.client.RestClient;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.id.EntityId;
|
||||
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
|
||||
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelation;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelationInfo;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelationsQuery;
|
||||
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
|
||||
import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
|
||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -241,23 +246,35 @@ public class ThingsBoardDomainImpl implements IThingsBoardDomain {
|
|||
List<String> childDeviceIds = new ArrayList<>();
|
||||
|
||||
try {
|
||||
DeviceId gatewayId = new DeviceId(UUID.fromString(gatewayDeviceId));
|
||||
|
||||
// 查询从网关出发的 "Contains" 关系(网关 -> 子设备)
|
||||
List<EntityRelation> relations = client.findByFrom(
|
||||
// 调用 findInfoByFrom 查询从网关出发的关系信息
|
||||
// 对应前端 API: GET /api/relations/info?fromId=xxx&fromType=DEVICE&relationTypeGroup=COMMON
|
||||
log.info("--------------------------" + gatewayDeviceId);
|
||||
EntityId gatewayId = new EntityId() {
|
||||
@Override
|
||||
public UUID getId() {
|
||||
return UUID.fromString(gatewayDeviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityType getEntityType() {
|
||||
return EntityType.DEVICE;
|
||||
}
|
||||
};
|
||||
|
||||
List<EntityRelationInfo> relationInfos = client.findInfoByFrom(
|
||||
gatewayId,
|
||||
EntityRelation.CONTAINS_TYPE,
|
||||
RelationTypeGroup.COMMON
|
||||
);
|
||||
|
||||
if (relations == null || relations.isEmpty()) {
|
||||
if (relationInfos == null || relationInfos.isEmpty()) {
|
||||
log.debug("网关 {} 没有子设备", gatewayDeviceId);
|
||||
return childDeviceIds;
|
||||
}
|
||||
|
||||
// 提取所有子设备的ID
|
||||
for (EntityRelation relation : relations) {
|
||||
EntityId childEntityId = relation.getTo();
|
||||
for (EntityRelationInfo relationInfo : relationInfos) {
|
||||
EntityId childEntityId = relationInfo.getTo();
|
||||
String childDeviceId = childEntityId.getId().toString();
|
||||
childDeviceIds.add(childDeviceId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.ruoyi.device.domain.model.thingsboard;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.thingsboard.rest.client.RestClient;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
|
|
@ -20,12 +22,15 @@ import java.util.stream.Collectors;
|
|||
*/
|
||||
public class GatewayDeviceIterator implements Iterable<List<DeviceInfo>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GatewayDeviceIterator.class);
|
||||
|
||||
private final RestClient client;
|
||||
private final int pageSize;
|
||||
|
||||
public GatewayDeviceIterator(RestClient client, int pageSize) {
|
||||
this.client = client;
|
||||
this.pageSize = pageSize;
|
||||
log.debug("初始化网关设备迭代器,页大小: {}", pageSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -68,12 +73,23 @@ public class GatewayDeviceIterator implements Iterable<List<DeviceInfo>> {
|
|||
*/
|
||||
private void loadNextBatch() {
|
||||
nextBatch = null;
|
||||
int skippedPages = 0;
|
||||
int totalDevicesScanned = 0;
|
||||
|
||||
log.debug("开始加载下一批网关设备");
|
||||
|
||||
// 循环直到找到至少一个网关设备,或者没有更多页
|
||||
while (hasMorePages && (nextBatch == null || nextBatch.isEmpty())) {
|
||||
int currentPageNumber = pageLink.getPage();
|
||||
|
||||
// 获取当前页数据
|
||||
log.debug("正在获取第 {} 页设备数据,页大小: {}", currentPageNumber, pageSize);
|
||||
PageData<Device> currentPage = client.getTenantDevices("", pageLink);
|
||||
|
||||
int pageDeviceCount = currentPage.getData().size();
|
||||
totalDevicesScanned += pageDeviceCount;
|
||||
log.debug("第 {} 页返回 {} 个设备", currentPageNumber, pageDeviceCount);
|
||||
|
||||
// 转换为DeviceInfo列表,并过滤出网关设备
|
||||
nextBatch = currentPage.getData().stream()
|
||||
.filter(device -> {
|
||||
|
|
@ -93,13 +109,32 @@ public class GatewayDeviceIterator implements Iterable<List<DeviceInfo>> {
|
|||
))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int gatewayCount = nextBatch != null ? nextBatch.size() : 0;
|
||||
log.debug("第 {} 页过滤后得到 {} 个网关设备", currentPageNumber, gatewayCount);
|
||||
|
||||
// 如果当前页没有网关设备,记录跳过
|
||||
if (gatewayCount == 0) {
|
||||
skippedPages++;
|
||||
log.debug("第 {} 页没有网关设备,继续查找下一页", currentPageNumber);
|
||||
}
|
||||
|
||||
// 准备下一页
|
||||
if (currentPage.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
} else {
|
||||
hasMorePages = false;
|
||||
log.debug("已到达最后一页,总共扫描 {} 个设备,跳过 {} 个空页", totalDevicesScanned, skippedPages);
|
||||
}
|
||||
}
|
||||
|
||||
// 输出最终结果日志
|
||||
if (nextBatch != null && !nextBatch.isEmpty()) {
|
||||
log.info("成功加载 {} 个网关设备,共扫描 {} 个设备,跳过 {} 个空页",
|
||||
nextBatch.size(), totalDevicesScanned, skippedPages);
|
||||
} else {
|
||||
log.info("没有找到更多网关设备,共扫描 {} 个设备,跳过 {} 个空页",
|
||||
totalDevicesScanned, skippedPages);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue