Search in sources :

Example 46 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method processNameOrTypeUpdate.

void processNameOrTypeUpdate(DeviceNameOrTypeUpdateMsg msg) {
    this.deviceName = msg.getDeviceName();
    this.deviceType = msg.getDeviceType();
    this.defaultMetaData = new TbMsgMetaData();
    this.defaultMetaData.putValue("deviceName", deviceName);
    this.defaultMetaData.putValue("deviceType", deviceType);
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Example 47 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData 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)

Example 48 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class DefaultTbContext method getActionMetaData.

private TbMsgMetaData getActionMetaData(RuleNodeId ruleNodeId) {
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("ruleNodeId", ruleNodeId.toString());
    return metaData;
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Example 49 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class DefaultDeviceStateService method pushRuleEngineMessage.

private void pushRuleEngineMessage(DeviceStateData stateData, String msgType) {
    DeviceState state = stateData.getState();
    try {
        String data;
        if (msgType.equals(CONNECT_EVENT)) {
            ObjectNode stateNode = JacksonUtil.convertValue(state, ObjectNode.class);
            stateNode.remove(ACTIVITY_STATE);
            data = JacksonUtil.toString(stateNode);
        } else {
            data = JacksonUtil.toString(state);
        }
        TbMsgMetaData md = stateData.getMetaData().copy();
        if (!persistToTelemetry) {
            md.putValue(DataConstants.SCOPE, SERVER_SCOPE);
        }
        TbMsg tbMsg = TbMsg.newMsg(msgType, stateData.getDeviceId(), stateData.getCustomerId(), md, TbMsgDataType.JSON, data);
        clusterService.pushMsgToRuleEngine(stateData.getTenantId(), stateData.getDeviceId(), tbMsg, null);
    } catch (Exception e) {
        log.warn("[{}] Failed to push inactivity alarm: {}", stateData.getDeviceId(), state, e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) ExecutionException(java.util.concurrent.ExecutionException)

Example 50 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processTelemetryFromEdge.

public List<ListenableFuture<Void>> processTelemetryFromEdge(TenantId tenantId, CustomerId customerId, EntityDataProto entityData) {
    log.trace("[{}] onTelemetryUpdate [{}]", tenantId, entityData);
    List<ListenableFuture<Void>> result = new ArrayList<>();
    EntityId entityId = constructEntityId(entityData);
    if ((entityData.hasPostAttributesMsg() || entityData.hasPostTelemetryMsg() || entityData.hasAttributesUpdatedMsg()) && entityId != null) {
        // @voba - in terms of performance we should not fetch device from DB by id
        // TbMsgMetaData metaData = constructBaseMsgMetadata(tenantId, entityId);
        TbMsgMetaData metaData = new TbMsgMetaData();
        metaData.putValue(DataConstants.MSG_SOURCE_KEY, DataConstants.EDGE_MSG_SOURCE);
        if (entityData.hasPostAttributesMsg()) {
            result.add(processPostAttributes(tenantId, customerId, entityId, entityData.getPostAttributesMsg(), metaData));
        }
        if (entityData.hasAttributesUpdatedMsg()) {
            metaData.putValue("scope", entityData.getPostAttributeScope());
            result.add(processAttributesUpdate(tenantId, customerId, entityId, entityData.getAttributesUpdatedMsg(), metaData));
        }
        if (entityData.hasPostTelemetryMsg()) {
            result.add(processPostTelemetry(tenantId, customerId, entityId, entityData.getPostTelemetryMsg(), metaData));
        }
    }
    if (entityData.hasAttributeDeleteMsg()) {
        result.add(processAttributeDeleteMsg(tenantId, entityId, entityData.getAttributeDeleteMsg(), entityData.getEntityType()));
    }
    return result;
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Aggregations

TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)89 TbMsg (org.thingsboard.server.common.msg.TbMsg)56 Test (org.junit.Test)49 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)26 Device (org.thingsboard.server.common.data.Device)17 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)17 List (java.util.List)16 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 DeviceId (org.thingsboard.server.common.data.id.DeviceId)15 EntityId (org.thingsboard.server.common.data.id.EntityId)15 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)14 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)14 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)14 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)14 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)14 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13