Search in sources :

Example 1 with NodeDefinition

use of org.thingsboard.rule.engine.api.NodeDefinition in project thingsboard by thingsboard.

the class AnnotationComponentDiscoveryService method scanAndPersistComponent.

private ComponentDescriptor scanAndPersistComponent(BeanDefinition def, ComponentType type) {
    ComponentDescriptor scannedComponent = new ComponentDescriptor();
    String clazzName = def.getBeanClassName();
    try {
        scannedComponent.setType(type);
        Class<?> clazz = Class.forName(clazzName);
        RuleNode ruleNodeAnnotation = clazz.getAnnotation(RuleNode.class);
        scannedComponent.setName(ruleNodeAnnotation.name());
        scannedComponent.setScope(ruleNodeAnnotation.scope());
        NodeDefinition nodeDefinition = prepareNodeDefinition(ruleNodeAnnotation);
        ObjectNode configurationDescriptor = mapper.createObjectNode();
        JsonNode node = mapper.valueToTree(nodeDefinition);
        configurationDescriptor.set("nodeDefinition", node);
        scannedComponent.setConfigurationDescriptor(configurationDescriptor);
        scannedComponent.setClazz(clazzName);
        log.info("Processing scanned component: {}", scannedComponent);
    } catch (Exception e) {
        log.error("Can't initialize component {}, due to {}", def.getBeanClassName(), e.getMessage(), e);
        throw new RuntimeException(e);
    }
    ComponentDescriptor persistedComponent = componentDescriptorService.findByClazz(TenantId.SYS_TENANT_ID, clazzName);
    if (persistedComponent == null) {
        log.info("Persisting new component: {}", scannedComponent);
        scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent);
    } else if (scannedComponent.equals(persistedComponent)) {
        log.info("Component is already persisted: {}", persistedComponent);
        scannedComponent = persistedComponent;
    } else {
        log.info("Component {} will be updated to {}", persistedComponent, scannedComponent);
        componentDescriptorService.deleteByClazz(TenantId.SYS_TENANT_ID, persistedComponent.getClazz());
        scannedComponent.setId(persistedComponent.getId());
        scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent);
    }
    return scannedComponent;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) RuleNode(org.thingsboard.rule.engine.api.RuleNode) NodeDefinition(org.thingsboard.rule.engine.api.NodeDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with NodeDefinition

use of org.thingsboard.rule.engine.api.NodeDefinition in project thingsboard by thingsboard.

the class AnnotationComponentDiscoveryService method prepareNodeDefinition.

private NodeDefinition prepareNodeDefinition(RuleNode nodeAnnotation) throws Exception {
    NodeDefinition nodeDefinition = new NodeDefinition();
    nodeDefinition.setDetails(nodeAnnotation.nodeDetails());
    nodeDefinition.setDescription(nodeAnnotation.nodeDescription());
    nodeDefinition.setInEnabled(nodeAnnotation.inEnabled());
    nodeDefinition.setOutEnabled(nodeAnnotation.outEnabled());
    nodeDefinition.setRelationTypes(getRelationTypesWithFailureRelation(nodeAnnotation));
    nodeDefinition.setCustomRelations(nodeAnnotation.customRelations());
    nodeDefinition.setRuleChainNode(nodeAnnotation.ruleChainNode());
    Class<? extends NodeConfiguration> configClazz = nodeAnnotation.configClazz();
    NodeConfiguration config = configClazz.getDeclaredConstructor().newInstance();
    NodeConfiguration defaultConfiguration = config.defaultConfiguration();
    nodeDefinition.setDefaultConfiguration(mapper.valueToTree(defaultConfiguration));
    nodeDefinition.setUiResources(nodeAnnotation.uiResources());
    nodeDefinition.setConfigDirective(nodeAnnotation.configDirective());
    nodeDefinition.setIcon(nodeAnnotation.icon());
    nodeDefinition.setIconUrl(nodeAnnotation.iconUrl());
    nodeDefinition.setDocUrl(nodeAnnotation.docUrl());
    return nodeDefinition;
}
Also used : NodeDefinition(org.thingsboard.rule.engine.api.NodeDefinition) NodeConfiguration(org.thingsboard.rule.engine.api.NodeConfiguration)

Aggregations

NodeDefinition (org.thingsboard.rule.engine.api.NodeDefinition)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 NodeConfiguration (org.thingsboard.rule.engine.api.NodeConfiguration)1 RuleNode (org.thingsboard.rule.engine.api.RuleNode)1 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)1