Search in sources :

Example 6 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class DefaultDataUpdateService method updateNestedRuleChains.

private void updateNestedRuleChains() {
    try {
        var packSize = 1024;
        var updated = 0;
        boolean hasNext = true;
        while (hasNext) {
            List<EntityRelation> relations = relationService.findRuleNodeToRuleChainRelations(TenantId.SYS_TENANT_ID, RuleChainType.CORE, packSize);
            hasNext = relations.size() == packSize;
            for (EntityRelation relation : relations) {
                try {
                    RuleNodeId sourceNodeId = new RuleNodeId(relation.getFrom().getId());
                    RuleNode sourceNode = ruleChainService.findRuleNodeById(TenantId.SYS_TENANT_ID, sourceNodeId);
                    if (sourceNode == null) {
                        log.info("Skip processing of relation for non existing source rule node: [{}]", sourceNodeId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    RuleChainId sourceRuleChainId = sourceNode.getRuleChainId();
                    RuleChainId targetRuleChainId = new RuleChainId(relation.getTo().getId());
                    RuleChain targetRuleChain = ruleChainService.findRuleChainById(TenantId.SYS_TENANT_ID, targetRuleChainId);
                    if (targetRuleChain == null) {
                        log.info("Skip processing of relation for non existing target rule chain: [{}]", targetRuleChainId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    TenantId tenantId = targetRuleChain.getTenantId();
                    RuleNode targetNode = new RuleNode();
                    targetNode.setName(targetRuleChain.getName());
                    targetNode.setRuleChainId(sourceRuleChainId);
                    targetNode.setType(TbRuleChainInputNode.class.getName());
                    TbRuleChainInputNodeConfiguration configuration = new TbRuleChainInputNodeConfiguration();
                    configuration.setRuleChainId(targetRuleChain.getId().toString());
                    targetNode.setConfiguration(JacksonUtil.valueToTree(configuration));
                    targetNode.setAdditionalInfo(relation.getAdditionalInfo());
                    targetNode.setDebugMode(false);
                    targetNode = ruleChainService.saveRuleNode(tenantId, targetNode);
                    EntityRelation sourceRuleChainToRuleNode = new EntityRelation();
                    sourceRuleChainToRuleNode.setFrom(sourceRuleChainId);
                    sourceRuleChainToRuleNode.setTo(targetNode.getId());
                    sourceRuleChainToRuleNode.setType(EntityRelation.CONTAINS_TYPE);
                    sourceRuleChainToRuleNode.setTypeGroup(RelationTypeGroup.RULE_CHAIN);
                    relationService.saveRelation(tenantId, sourceRuleChainToRuleNode);
                    EntityRelation sourceRuleNodeToTargetRuleNode = new EntityRelation();
                    sourceRuleNodeToTargetRuleNode.setFrom(sourceNode.getId());
                    sourceRuleNodeToTargetRuleNode.setTo(targetNode.getId());
                    sourceRuleNodeToTargetRuleNode.setType(relation.getType());
                    sourceRuleNodeToTargetRuleNode.setTypeGroup(RelationTypeGroup.RULE_NODE);
                    sourceRuleNodeToTargetRuleNode.setAdditionalInfo(relation.getAdditionalInfo());
                    relationService.saveRelation(tenantId, sourceRuleNodeToTargetRuleNode);
                    // Delete old relation
                    relationService.deleteRelation(tenantId, relation);
                    updated++;
                } catch (Exception e) {
                    log.info("Failed to update RuleNodeToRuleChainRelation: {}", relation, e);
                }
            }
            if (updated > 0) {
                log.info("RuleNodeToRuleChainRelations: {} entities updated so far...", updated);
            }
        }
    } catch (Exception e) {
        log.error("Unable to update Tenant", e);
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ExecutionException(java.util.concurrent.ExecutionException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode)

Example 7 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleChainEntity method toData.

@Override
public RuleChain toData() {
    RuleChain ruleChain = new RuleChain(new RuleChainId(this.getUuid()));
    ruleChain.setCreatedTime(createdTime);
    ruleChain.setTenantId(TenantId.fromUUID(tenantId));
    ruleChain.setName(name);
    ruleChain.setType(type);
    if (firstRuleNodeId != null) {
        ruleChain.setFirstRuleNodeId(new RuleNodeId(firstRuleNodeId));
    }
    ruleChain.setRoot(root);
    ruleChain.setDebugMode(debugMode);
    ruleChain.setConfiguration(configuration);
    ruleChain.setAdditionalInfo(additionalInfo);
    return ruleChain;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 8 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleNodeStateEntity method toData.

@Override
public RuleNodeState toData() {
    RuleNodeState ruleNode = new RuleNodeState(new RuleNodeStateId(this.getUuid()));
    ruleNode.setCreatedTime(createdTime);
    ruleNode.setRuleNodeId(new RuleNodeId(ruleNodeId));
    ruleNode.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));
    ruleNode.setStateData(stateData);
    return ruleNode;
}
Also used : RuleNodeStateId(org.thingsboard.server.common.data.id.RuleNodeStateId) RuleNodeState(org.thingsboard.server.common.data.rule.RuleNodeState) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 9 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleNodeEntity method toData.

@Override
public RuleNode toData() {
    RuleNode ruleNode = new RuleNode(new RuleNodeId(this.getUuid()));
    ruleNode.setCreatedTime(createdTime);
    if (ruleChainId != null) {
        ruleNode.setRuleChainId(new RuleChainId(ruleChainId));
    }
    ruleNode.setType(type);
    ruleNode.setName(name);
    ruleNode.setDebugMode(debugMode);
    ruleNode.setConfiguration(configuration);
    ruleNode.setAdditionalInfo(additionalInfo);
    return ruleNode;
}
Also used : RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 10 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleChainController method getLatestRuleNodeDebugInput.

@ApiOperation(value = "Get latest input message (getLatestRuleNodeDebugInput)", notes = "Gets the input message from the debug events for specified Rule Chain Id. " + "Referencing non-existing rule chain Id will cause an error. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleNode/{ruleNodeId}/debugIn", method = RequestMethod.GET)
@ResponseBody
public JsonNode getLatestRuleNodeDebugInput(@ApiParam(value = RULE_NODE_ID_PARAM_DESCRIPTION) @PathVariable(RULE_NODE_ID) String strRuleNodeId) throws ThingsboardException {
    checkParameter(RULE_NODE_ID, strRuleNodeId);
    try {
        RuleNodeId ruleNodeId = new RuleNodeId(toUUID(strRuleNodeId));
        checkRuleNode(ruleNodeId, Operation.READ);
        TenantId tenantId = getCurrentUser().getTenantId();
        List<Event> events = eventService.findLatestEvents(tenantId, ruleNodeId, DataConstants.DEBUG_RULE_NODE, 2);
        JsonNode result = null;
        if (events != null) {
            for (Event event : events) {
                JsonNode body = event.getBody();
                if (body.has("type") && body.get("type").asText().equals("IN")) {
                    result = body;
                    break;
                }
            }
        }
        return result;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Event(org.thingsboard.server.common.data.Event) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)26 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)21 TbMsg (org.thingsboard.server.common.msg.TbMsg)15 EntityId (org.thingsboard.server.common.data.id.EntityId)14 TenantId (org.thingsboard.server.common.data.id.TenantId)12 Test (org.junit.Test)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)9 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Callable (java.util.concurrent.Callable)6 Consumer (java.util.function.Consumer)6 ScriptException (javax.script.ScriptException)6 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 Assert.assertEquals (org.junit.Assert.assertEquals)6