Search in sources :

Example 21 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadataWithCirclingRelation.

private RuleChainMetaData createRuleChainMetadataWithCirclingRelation() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    ruleChainMetaData.addConnectionInfo(2, 2, "success");
    return ruleChainMetaData;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadataWithCirclingRelation2.

private RuleChainMetaData createRuleChainMetadataWithCirclingRelation2() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    ruleChainMetaData.addConnectionInfo(2, 0, "success");
    return ruleChainMetaData;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class AuditLogServiceImpl method constructActionData.

private <E extends HasName, I extends EntityId> JsonNode constructActionData(I entityId, E entity, ActionType actionType, Object... additionalInfo) {
    ObjectNode actionData = JacksonUtil.newObjectNode();
    switch(actionType) {
        case ADDED:
        case UPDATED:
        case ALARM_ACK:
        case ALARM_CLEAR:
        case RELATIONS_DELETED:
        case ASSIGNED_TO_TENANT:
            if (entity != null) {
                ObjectNode entityNode = (ObjectNode) JacksonUtil.valueToTree(entity);
                if (entityId.getEntityType() == EntityType.DASHBOARD) {
                    entityNode.put("configuration", "");
                }
                actionData.set("entity", entityNode);
            }
            if (entityId.getEntityType() == EntityType.RULE_CHAIN) {
                RuleChainMetaData ruleChainMetaData = extractParameter(RuleChainMetaData.class, additionalInfo);
                if (ruleChainMetaData != null) {
                    ObjectNode ruleChainMetaDataNode = (ObjectNode) JacksonUtil.valueToTree(ruleChainMetaData);
                    actionData.set("metadata", ruleChainMetaDataNode);
                }
            }
            break;
        case DELETED:
        case ACTIVATED:
        case SUSPENDED:
        case CREDENTIALS_READ:
            String strEntityId = extractParameter(String.class, additionalInfo);
            actionData.put("entityId", strEntityId);
            break;
        case ATTRIBUTES_UPDATED:
            actionData.put("entityId", entityId.toString());
            String scope = extractParameter(String.class, 0, additionalInfo);
            @SuppressWarnings("unchecked") List<AttributeKvEntry> attributes = extractParameter(List.class, 1, additionalInfo);
            actionData.put("scope", scope);
            ObjectNode attrsNode = JacksonUtil.newObjectNode();
            if (attributes != null) {
                for (AttributeKvEntry attr : attributes) {
                    attrsNode.put(attr.getKey(), attr.getValueAsString());
                }
            }
            actionData.set("attributes", attrsNode);
            break;
        case ATTRIBUTES_DELETED:
        case ATTRIBUTES_READ:
            actionData.put("entityId", entityId.toString());
            scope = extractParameter(String.class, 0, additionalInfo);
            actionData.put("scope", scope);
            @SuppressWarnings("unchecked") List<String> keys = extractParameter(List.class, 1, additionalInfo);
            ArrayNode attrsArrayNode = actionData.putArray("attributes");
            if (keys != null) {
                keys.forEach(attrsArrayNode::add);
            }
            break;
        case RPC_CALL:
            actionData.put("entityId", entityId.toString());
            Boolean oneWay = extractParameter(Boolean.class, 1, additionalInfo);
            String method = extractParameter(String.class, 2, additionalInfo);
            String params = extractParameter(String.class, 3, additionalInfo);
            actionData.put("oneWay", oneWay);
            actionData.put("method", method);
            actionData.put("params", params);
            break;
        case CREDENTIALS_UPDATED:
            actionData.put("entityId", entityId.toString());
            DeviceCredentials deviceCredentials = extractParameter(DeviceCredentials.class, additionalInfo);
            actionData.set("credentials", JacksonUtil.valueToTree(deviceCredentials));
            break;
        case ASSIGNED_TO_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            String strCustomerId = extractParameter(String.class, 1, additionalInfo);
            String strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("assignedCustomerId", strCustomerId);
            actionData.put("assignedCustomerName", strCustomerName);
            break;
        case UNASSIGNED_FROM_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            strCustomerId = extractParameter(String.class, 1, additionalInfo);
            strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("unassignedCustomerId", strCustomerId);
            actionData.put("unassignedCustomerName", strCustomerName);
            break;
        case RELATION_ADD_OR_UPDATE:
        case RELATION_DELETED:
            EntityRelation relation = extractParameter(EntityRelation.class, 0, additionalInfo);
            actionData.set("relation", JacksonUtil.valueToTree(relation));
            break;
        case LOGIN:
        case LOGOUT:
        case LOCKOUT:
            String clientAddress = extractParameter(String.class, 0, additionalInfo);
            String browser = extractParameter(String.class, 1, additionalInfo);
            String os = extractParameter(String.class, 2, additionalInfo);
            String device = extractParameter(String.class, 3, additionalInfo);
            actionData.put("clientAddress", clientAddress);
            actionData.put("browser", browser);
            actionData.put("os", os);
            actionData.put("device", device);
            break;
        case PROVISION_SUCCESS:
        case PROVISION_FAILURE:
            ProvisionRequest request = extractParameter(ProvisionRequest.class, additionalInfo);
            if (request != null) {
                actionData.set("provisionRequest", JacksonUtil.valueToTree(request));
            }
            break;
        case TIMESERIES_UPDATED:
            actionData.put("entityId", entityId.toString());
            @SuppressWarnings("unchecked") List<TsKvEntry> updatedTimeseries = extractParameter(List.class, 0, additionalInfo);
            if (updatedTimeseries != null) {
                ArrayNode result = actionData.putArray("timeseries");
                updatedTimeseries.stream().collect(Collectors.groupingBy(TsKvEntry::getTs)).forEach((k, v) -> {
                    ObjectNode element = JacksonUtil.newObjectNode();
                    element.put("ts", k);
                    ObjectNode values = element.putObject("values");
                    v.forEach(kvEntry -> values.put(kvEntry.getKey(), kvEntry.getValueAsString()));
                    result.add(element);
                });
            }
            break;
        case TIMESERIES_DELETED:
            actionData.put("entityId", entityId.toString());
            @SuppressWarnings("unchecked") List<String> timeseriesKeys = extractParameter(List.class, 0, additionalInfo);
            if (timeseriesKeys != null) {
                ArrayNode timeseriesArrayNode = actionData.putArray("timeseries");
                timeseriesKeys.forEach(timeseriesArrayNode::add);
            }
            actionData.put("startTs", extractParameter(Long.class, 1, additionalInfo));
            actionData.put("endTs", extractParameter(Long.class, 2, additionalInfo));
            break;
        case ASSIGNED_TO_EDGE:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            String strEdgeId = extractParameter(String.class, 1, additionalInfo);
            String strEdgeName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("assignedEdgeId", strEdgeId);
            actionData.put("assignedEdgeName", strEdgeName);
            break;
        case UNASSIGNED_FROM_EDGE:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            strEdgeId = extractParameter(String.class, 1, additionalInfo);
            strEdgeName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("unassignedEdgeId", strEdgeId);
            actionData.put("unassignedEdgeName", strEdgeName);
            break;
    }
    return actionData;
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ProvisionRequest(org.thingsboard.server.dao.device.provision.ProvisionRequest)

Aggregations

RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)23 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)15 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)12 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)5 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 TbGetAttributesNodeConfiguration (org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration)3 Device (org.thingsboard.server.common.data.Device)3 Event (org.thingsboard.server.common.data.Event)3 RuleChainMetadataUpdateMsg (org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2