Search in sources :

Example 1 with HasTenantId

use of org.thingsboard.server.common.data.HasTenantId in project thingsboard by thingsboard.

the class EntityActionService method pushEntityActionToRuleEngine.

public void pushEntityActionToRuleEngine(EntityId entityId, HasName entity, TenantId tenantId, CustomerId customerId, ActionType actionType, User user, Object... additionalInfo) {
    String msgType = null;
    switch(actionType) {
        case ADDED:
            msgType = DataConstants.ENTITY_CREATED;
            break;
        case DELETED:
            msgType = DataConstants.ENTITY_DELETED;
            break;
        case UPDATED:
            msgType = DataConstants.ENTITY_UPDATED;
            break;
        case ASSIGNED_TO_CUSTOMER:
            msgType = DataConstants.ENTITY_ASSIGNED;
            break;
        case UNASSIGNED_FROM_CUSTOMER:
            msgType = DataConstants.ENTITY_UNASSIGNED;
            break;
        case ATTRIBUTES_UPDATED:
            msgType = DataConstants.ATTRIBUTES_UPDATED;
            break;
        case ATTRIBUTES_DELETED:
            msgType = DataConstants.ATTRIBUTES_DELETED;
            break;
        case ALARM_ACK:
            msgType = DataConstants.ALARM_ACK;
            break;
        case ALARM_CLEAR:
            msgType = DataConstants.ALARM_CLEAR;
            break;
        case ALARM_DELETE:
            msgType = DataConstants.ALARM_DELETE;
            break;
        case ASSIGNED_FROM_TENANT:
            msgType = DataConstants.ENTITY_ASSIGNED_FROM_TENANT;
            break;
        case ASSIGNED_TO_TENANT:
            msgType = DataConstants.ENTITY_ASSIGNED_TO_TENANT;
            break;
        case PROVISION_SUCCESS:
            msgType = DataConstants.PROVISION_SUCCESS;
            break;
        case PROVISION_FAILURE:
            msgType = DataConstants.PROVISION_FAILURE;
            break;
        case TIMESERIES_UPDATED:
            msgType = DataConstants.TIMESERIES_UPDATED;
            break;
        case TIMESERIES_DELETED:
            msgType = DataConstants.TIMESERIES_DELETED;
            break;
        case ASSIGNED_TO_EDGE:
            msgType = DataConstants.ENTITY_ASSIGNED_TO_EDGE;
            break;
        case UNASSIGNED_FROM_EDGE:
            msgType = DataConstants.ENTITY_UNASSIGNED_FROM_EDGE;
            break;
    }
    if (!StringUtils.isEmpty(msgType)) {
        try {
            TbMsgMetaData metaData = new TbMsgMetaData();
            if (user != null) {
                metaData.putValue("userId", user.getId().toString());
                metaData.putValue("userName", user.getName());
            }
            if (customerId != null && !customerId.isNullUid()) {
                metaData.putValue("customerId", customerId.toString());
            }
            if (actionType == ActionType.ASSIGNED_TO_CUSTOMER) {
                String strCustomerId = extractParameter(String.class, 1, additionalInfo);
                String strCustomerName = extractParameter(String.class, 2, additionalInfo);
                metaData.putValue("assignedCustomerId", strCustomerId);
                metaData.putValue("assignedCustomerName", strCustomerName);
            } else if (actionType == ActionType.UNASSIGNED_FROM_CUSTOMER) {
                String strCustomerId = extractParameter(String.class, 1, additionalInfo);
                String strCustomerName = extractParameter(String.class, 2, additionalInfo);
                metaData.putValue("unassignedCustomerId", strCustomerId);
                metaData.putValue("unassignedCustomerName", strCustomerName);
            } else if (actionType == ActionType.ASSIGNED_FROM_TENANT) {
                String strTenantId = extractParameter(String.class, 0, additionalInfo);
                String strTenantName = extractParameter(String.class, 1, additionalInfo);
                metaData.putValue("assignedFromTenantId", strTenantId);
                metaData.putValue("assignedFromTenantName", strTenantName);
            } else if (actionType == ActionType.ASSIGNED_TO_TENANT) {
                String strTenantId = extractParameter(String.class, 0, additionalInfo);
                String strTenantName = extractParameter(String.class, 1, additionalInfo);
                metaData.putValue("assignedToTenantId", strTenantId);
                metaData.putValue("assignedToTenantName", strTenantName);
            } else if (actionType == ActionType.ASSIGNED_TO_EDGE) {
                String strEdgeId = extractParameter(String.class, 1, additionalInfo);
                String strEdgeName = extractParameter(String.class, 2, additionalInfo);
                metaData.putValue("assignedEdgeId", strEdgeId);
                metaData.putValue("assignedEdgeName", strEdgeName);
            } else if (actionType == ActionType.UNASSIGNED_FROM_EDGE) {
                String strEdgeId = extractParameter(String.class, 1, additionalInfo);
                String strEdgeName = extractParameter(String.class, 2, additionalInfo);
                metaData.putValue("unassignedEdgeId", strEdgeId);
                metaData.putValue("unassignedEdgeName", strEdgeName);
            }
            ObjectNode entityNode;
            if (entity != null) {
                entityNode = json.valueToTree(entity);
                if (entityId.getEntityType() == EntityType.DASHBOARD) {
                    entityNode.put("configuration", "");
                }
            } else {
                entityNode = json.createObjectNode();
                if (actionType == ActionType.ATTRIBUTES_UPDATED) {
                    String scope = extractParameter(String.class, 0, additionalInfo);
                    @SuppressWarnings("unchecked") List<AttributeKvEntry> attributes = extractParameter(List.class, 1, additionalInfo);
                    metaData.putValue(DataConstants.SCOPE, scope);
                    if (attributes != null) {
                        for (AttributeKvEntry attr : attributes) {
                            addKvEntry(entityNode, attr);
                        }
                    }
                } else if (actionType == ActionType.ATTRIBUTES_DELETED) {
                    String scope = extractParameter(String.class, 0, additionalInfo);
                    @SuppressWarnings("unchecked") List<String> keys = extractParameter(List.class, 1, additionalInfo);
                    metaData.putValue(DataConstants.SCOPE, scope);
                    ArrayNode attrsArrayNode = entityNode.putArray("attributes");
                    if (keys != null) {
                        keys.forEach(attrsArrayNode::add);
                    }
                } else if (actionType == ActionType.TIMESERIES_UPDATED) {
                    @SuppressWarnings("unchecked") List<TsKvEntry> timeseries = extractParameter(List.class, 0, additionalInfo);
                    addTimeseries(entityNode, timeseries);
                } else if (actionType == ActionType.TIMESERIES_DELETED) {
                    @SuppressWarnings("unchecked") List<String> keys = extractParameter(List.class, 0, additionalInfo);
                    if (keys != null) {
                        ArrayNode timeseriesArrayNode = entityNode.putArray("timeseries");
                        keys.forEach(timeseriesArrayNode::add);
                    }
                    entityNode.put("startTs", extractParameter(Long.class, 1, additionalInfo));
                    entityNode.put("endTs", extractParameter(Long.class, 2, additionalInfo));
                }
            }
            TbMsg tbMsg = TbMsg.newMsg(msgType, entityId, customerId, metaData, TbMsgDataType.JSON, json.writeValueAsString(entityNode));
            if (tenantId.isNullUid()) {
                if (entity instanceof HasTenantId) {
                    tenantId = ((HasTenantId) entity).getTenantId();
                }
            }
            tbClusterService.pushMsgToRuleEngine(tenantId, entityId, tbMsg, null);
        } catch (Exception e) {
            log.warn("[{}] Failed to push entity action to rule engine: {}", entityId, actionType, e);
        }
    }
}
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) HasTenantId(org.thingsboard.server.common.data.HasTenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 List (java.util.List)1 HasTenantId (org.thingsboard.server.common.data.HasTenantId)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 TbMsg (org.thingsboard.server.common.msg.TbMsg)1 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)1