|
|
@ -15,6 +15,8 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.rule.engine.filter; |
|
|
package org.thingsboard.rule.engine.filter; |
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.thingsboard.rule.engine.api.RuleNode; |
|
|
import org.thingsboard.rule.engine.api.RuleNode; |
|
|
import org.thingsboard.rule.engine.api.TbContext; |
|
|
import org.thingsboard.rule.engine.api.TbContext; |
|
|
@ -25,13 +27,13 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|
|
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|
|
import org.thingsboard.server.common.data.plugin.ComponentType; |
|
|
import org.thingsboard.server.common.data.plugin.ComponentType; |
|
|
|
|
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
|
|
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|
|
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
|
|
|
|
|
import javax.management.relation.RelationType; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
|
|
|
|
|
import static org.thingsboard.rule.engine.api.util.DonAsynchron.withCallback; |
|
|
import static org.thingsboard.rule.engine.api.util.DonAsynchron.withCallback; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -43,8 +45,10 @@ import static org.thingsboard.rule.engine.api.util.DonAsynchron.withCallback; |
|
|
name = "check relation", |
|
|
name = "check relation", |
|
|
configClazz = TbCheckRelationNodeConfiguration.class, |
|
|
configClazz = TbCheckRelationNodeConfiguration.class, |
|
|
relationTypes = {"True", "False"}, |
|
|
relationTypes = {"True", "False"}, |
|
|
nodeDescription = "Checks the relation from the selected entity to originator of the message by type and direction", |
|
|
nodeDescription = "Checks the relation from the selected entity to the originator of the message by type and direction" + |
|
|
nodeDetails = "If relation exists - send Message via <b>True</b> chain, otherwise <b>False</b> chain is used.", |
|
|
" if 'Check for single entity' is set to true, otherwise rule node will check if exist" + |
|
|
|
|
|
" any relation to the originator of the message by type and direction.", |
|
|
|
|
|
nodeDetails = "If at least one relation exists - send Message via <b>True</b> chain, otherwise <b>False</b> chain is used.", |
|
|
uiResources = {"static/rulenode/rulenode-core-config.js"}, |
|
|
uiResources = {"static/rulenode/rulenode-core-config.js"}, |
|
|
configDirective = "tbFilterNodeCheckRelationConfig") |
|
|
configDirective = "tbFilterNodeCheckRelationConfig") |
|
|
public class TbCheckRelationNode implements TbNode { |
|
|
public class TbCheckRelationNode implements TbNode { |
|
|
@ -58,6 +62,16 @@ public class TbCheckRelationNode implements TbNode { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException { |
|
|
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException { |
|
|
|
|
|
ListenableFuture<Boolean> checkRelationFuture; |
|
|
|
|
|
if (config.isCheckForSingleEntity()) { |
|
|
|
|
|
checkRelationFuture = processSingle(ctx, msg); |
|
|
|
|
|
} else { |
|
|
|
|
|
checkRelationFuture = processList(ctx, msg); |
|
|
|
|
|
} |
|
|
|
|
|
withCallback(checkRelationFuture, filterResult -> ctx.tellNext(msg, filterResult ? "True" : "False"), t -> ctx.tellFailure(msg, t), ctx.getDbCallbackExecutor()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ListenableFuture<Boolean> processSingle(TbContext ctx, TbMsg msg) { |
|
|
EntityId from; |
|
|
EntityId from; |
|
|
EntityId to; |
|
|
EntityId to; |
|
|
if (EntitySearchDirection.FROM.name().equals(config.getDirection())) { |
|
|
if (EntitySearchDirection.FROM.name().equals(config.getDirection())) { |
|
|
@ -67,8 +81,25 @@ public class TbCheckRelationNode implements TbNode { |
|
|
to = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId()); |
|
|
to = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId()); |
|
|
from = msg.getOriginator(); |
|
|
from = msg.getOriginator(); |
|
|
} |
|
|
} |
|
|
withCallback(ctx.getRelationService().checkRelation(ctx.getTenantId(), from, to, config.getRelationType(), RelationTypeGroup.COMMON), |
|
|
return ctx.getRelationService().checkRelation(ctx.getTenantId(), from, to, config.getRelationType(), RelationTypeGroup.COMMON); |
|
|
filterResult -> ctx.tellNext(msg, filterResult ? "True" : "False"), t -> ctx.tellFailure(msg, t), ctx.getDbCallbackExecutor()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ListenableFuture<Boolean> processList(TbContext ctx, TbMsg msg) { |
|
|
|
|
|
if (EntitySearchDirection.FROM.name().equals(config.getDirection())) { |
|
|
|
|
|
return Futures.transformAsync(ctx.getRelationService() |
|
|
|
|
|
.findByToAndTypeAsync(ctx.getTenantId(), msg.getOriginator(), config.getRelationType(), RelationTypeGroup.COMMON), this::isEmptyList); |
|
|
|
|
|
} else { |
|
|
|
|
|
return Futures.transformAsync(ctx.getRelationService() |
|
|
|
|
|
.findByFromAndTypeAsync(ctx.getTenantId(), msg.getOriginator(), config.getRelationType(), RelationTypeGroup.COMMON), this::isEmptyList); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ListenableFuture<Boolean> isEmptyList(List<EntityRelation> entityRelations) { |
|
|
|
|
|
if (entityRelations.isEmpty()) { |
|
|
|
|
|
return Futures.immediateFuture(false); |
|
|
|
|
|
} else { |
|
|
|
|
|
return Futures.immediateFuture(true); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|