修改代码

This commit is contained in:
孙小云 2026-01-17 15:49:35 +08:00
parent 39f423dd28
commit c08855a3b5
1 changed files with 21 additions and 8 deletions

View File

@ -17,6 +17,10 @@ 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;
@ -243,21 +247,30 @@ public class ThingsBoardDomainImpl implements IThingsBoardDomain {
try {
DeviceId gatewayId = new DeviceId(UUID.fromString(gatewayDeviceId));
// 查询从网关出发的 "Contains" 关系网关 -> 子设备
List<EntityRelation> relations = client.findByFrom(
gatewayId,
EntityRelation.CONTAINS_TYPE,
RelationTypeGroup.COMMON
// 构建查询参数从网关出发查询子设备
// direction = FROM 表示从 rootId 出发的关系网关 -> 子设备
RelationsSearchParameters parameters = new RelationsSearchParameters(
gatewayId, // rootId: 网关设备ID
EntitySearchDirection.FROM, // direction: 从网关出发
1, // maxLevel: 查询深度为1直接子设备
RelationTypeGroup.COMMON, // relationTypeGroup: COMMON
false // fetchLastLevelOnly: false
);
if (relations == null || relations.isEmpty()) {
EntityRelationsQuery query = new EntityRelationsQuery();
query.setParameters(parameters);
// 调用 findInfoByQuery 查询关系信息
List<EntityRelationInfo> relationInfos = client.findInfoByQuery(query);
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);
}