Search in sources :

Example 1 with EdgeEventType

use of org.thingsboard.server.common.data.edge.EdgeEventType in project thingsboard by thingsboard.

the class DefaultEdgeNotificationService method pushNotificationToEdge.

@Override
public void pushNotificationToEdge(TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg, TbCallback callback) {
    log.debug("Pushing notification to edge {}", edgeNotificationMsg);
    try {
        TenantId tenantId = TenantId.fromUUID(new UUID(edgeNotificationMsg.getTenantIdMSB(), edgeNotificationMsg.getTenantIdLSB()));
        EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
        switch(type) {
            case EDGE:
                edgeProcessor.processEdgeNotification(tenantId, edgeNotificationMsg);
                break;
            case USER:
            case ASSET:
            case DEVICE:
            case DEVICE_PROFILE:
            case ENTITY_VIEW:
            case DASHBOARD:
            case RULE_CHAIN:
                entityProcessor.processEntityNotification(tenantId, edgeNotificationMsg);
                break;
            case CUSTOMER:
                customerProcessor.processCustomerNotification(tenantId, edgeNotificationMsg);
                break;
            case WIDGETS_BUNDLE:
            case WIDGET_TYPE:
                entityProcessor.processEntityNotificationForAllEdges(tenantId, edgeNotificationMsg);
                break;
            case ALARM:
                alarmProcessor.processAlarmNotification(tenantId, edgeNotificationMsg);
                break;
            case RELATION:
                relationProcessor.processRelationNotification(tenantId, edgeNotificationMsg);
                break;
            default:
                log.warn("Edge event type [{}] is not designed to be pushed to edge", type);
        }
    } catch (Exception e) {
        callback.onFailure(e);
        String errMsg = String.format("Can't push to edge updates, edgeNotificationMsg [%s]", edgeNotificationMsg);
        log.error(errMsg, e);
    } finally {
        callback.onSuccess();
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) UUID(java.util.UUID) IOException(java.io.IOException)

Example 2 with EdgeEventType

use of org.thingsboard.server.common.data.edge.EdgeEventType in project thingsboard by thingsboard.

the class AlarmEdgeProcessor method processAlarmNotification.

public void processAlarmNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) throws JsonProcessingException {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    AlarmId alarmId = new AlarmId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
    switch(actionType) {
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            Alarm alarm = mapper.readValue(edgeNotificationMsg.getBody(), Alarm.class);
            saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, actionType, alarmId, mapper.valueToTree(alarm));
            break;
        default:
            ListenableFuture<Alarm> alarmFuture = alarmService.findAlarmByIdAsync(tenantId, alarmId);
            Futures.addCallback(alarmFuture, new FutureCallback<Alarm>() {

                @Override
                public void onSuccess(@Nullable Alarm alarm) {
                    if (alarm != null) {
                        EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(alarm.getOriginator().getEntityType());
                        if (type != null) {
                            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
                            PageData<EdgeId> pageData;
                            do {
                                pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, alarm.getOriginator(), pageLink);
                                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                                    for (EdgeId edgeId : pageData.getData()) {
                                        saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()), alarmId, null);
                                    }
                                    if (pageData.hasNext()) {
                                        pageLink = pageLink.nextPageLink();
                                    }
                                }
                            } while (pageData != null && pageData.hasNext());
                        }
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    log.warn("[{}] can't find alarm by id [{}] {}", tenantId.getId(), alarmId.getId(), t);
                }
            }, dbCallbackExecutorService);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) PageData(org.thingsboard.server.common.data.page.PageData) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) UUID(java.util.UUID)

Example 3 with EdgeEventType

use of org.thingsboard.server.common.data.edge.EdgeEventType in project thingsboard by thingsboard.

the class EntityEdgeProcessor method processEntityNotification.

public void processEntityNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
    EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
    EdgeId edgeId = null;
    if (edgeNotificationMsg.getEdgeIdMSB() != 0 && edgeNotificationMsg.getEdgeIdLSB() != 0) {
        edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
    }
    switch(actionType) {
        // used only for USER entity
        case ADDED:
        case UPDATED:
        case CREDENTIALS_UPDATED:
            pushNotificationToAllRelatedEdges(tenantId, entityId, type, actionType);
            break;
        case ASSIGNED_TO_CUSTOMER:
        case UNASSIGNED_FROM_CUSTOMER:
            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
            PageData<EdgeId> pageData;
            do {
                pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId, pageLink);
                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                    for (EdgeId relatedEdgeId : pageData.getData()) {
                        try {
                            CustomerId customerId = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class);
                            ListenableFuture<Edge> future = edgeService.findEdgeByIdAsync(tenantId, relatedEdgeId);
                            Futures.addCallback(future, new FutureCallback<>() {

                                @Override
                                public void onSuccess(@Nullable Edge edge) {
                                    if (edge != null && edge.getCustomerId() != null && !edge.getCustomerId().isNullUid() && edge.getCustomerId().equals(customerId)) {
                                        saveEdgeEvent(tenantId, relatedEdgeId, type, actionType, entityId, null);
                                    }
                                }

                                @Override
                                public void onFailure(Throwable t) {
                                    log.error("Failed to find edge by id [{}] {}", edgeNotificationMsg, t);
                                }
                            }, dbCallbackExecutorService);
                        } catch (Exception e) {
                            log.error("Can't parse customer id from entity body [{}]", edgeNotificationMsg, e);
                        }
                    }
                    if (pageData.hasNext()) {
                        pageLink = pageLink.nextPageLink();
                    }
                }
            } while (pageData != null && pageData.hasNext());
            break;
        case DELETED:
            if (edgeId != null) {
                saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null);
            } else {
                pushNotificationToAllRelatedEdges(tenantId, entityId, type, actionType);
            }
            break;
        case ASSIGNED_TO_EDGE:
        case UNASSIGNED_FROM_EDGE:
            saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null);
            if (type.equals(EdgeEventType.RULE_CHAIN)) {
                updateDependentRuleChains(tenantId, new RuleChainId(entityId.getId()), edgeId);
            }
            break;
    }
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) UUID(java.util.UUID) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 4 with EdgeEventType

use of org.thingsboard.server.common.data.edge.EdgeEventType in project thingsboard by thingsboard.

the class CustomerEdgeProcessor method processCustomerNotification.

public void processCustomerNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
    UUID uuid = new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB());
    CustomerId customerId = new CustomerId(EntityIdFactory.getByEdgeEventTypeAndUuid(type, uuid).getId());
    switch(actionType) {
        case UPDATED:
            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
            PageData<Edge> pageData;
            do {
                pageData = edgeService.findEdgesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                    for (Edge edge : pageData.getData()) {
                        saveEdgeEvent(tenantId, edge.getId(), type, actionType, customerId, null);
                    }
                    if (pageData.hasNext()) {
                        pageLink = pageLink.nextPageLink();
                    }
                }
            } while (pageData != null && pageData.hasNext());
            break;
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            saveEdgeEvent(tenantId, edgeId, type, actionType, customerId, null);
            break;
    }
}
Also used : EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 5 with EdgeEventType

use of org.thingsboard.server.common.data.edge.EdgeEventType in project thingsboard by thingsboard.

the class DefaultEdgeRequestsService method processAttributesRequestMsg.

@Override
public ListenableFuture<Void> processAttributesRequestMsg(TenantId tenantId, Edge edge, AttributesRequestMsg attributesRequestMsg) {
    log.trace("[{}] processAttributesRequestMsg [{}][{}]", tenantId, edge.getName(), attributesRequestMsg);
    EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(attributesRequestMsg.getEntityType()), new UUID(attributesRequestMsg.getEntityIdMSB(), attributesRequestMsg.getEntityIdLSB()));
    final EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(entityId.getEntityType());
    if (type != null) {
        SettableFuture<Void> futureToSet = SettableFuture.create();
        String scope = attributesRequestMsg.getScope();
        ListenableFuture<List<AttributeKvEntry>> findAttrFuture = attributesService.findAll(tenantId, entityId, scope);
        Futures.addCallback(findAttrFuture, new FutureCallback<List<AttributeKvEntry>>() {

            @Override
            public void onSuccess(@Nullable List<AttributeKvEntry> ssAttributes) {
                if (ssAttributes != null && !ssAttributes.isEmpty()) {
                    try {
                        Map<String, Object> entityData = new HashMap<>();
                        ObjectNode attributes = mapper.createObjectNode();
                        for (AttributeKvEntry attr : ssAttributes) {
                            if (attr.getDataType() == DataType.BOOLEAN && attr.getBooleanValue().isPresent()) {
                                attributes.put(attr.getKey(), attr.getBooleanValue().get());
                            } else if (attr.getDataType() == DataType.DOUBLE && attr.getDoubleValue().isPresent()) {
                                attributes.put(attr.getKey(), attr.getDoubleValue().get());
                            } else if (attr.getDataType() == DataType.LONG && attr.getLongValue().isPresent()) {
                                attributes.put(attr.getKey(), attr.getLongValue().get());
                            } else {
                                attributes.put(attr.getKey(), attr.getValueAsString());
                            }
                        }
                        entityData.put("kv", attributes);
                        entityData.put("scope", scope);
                        JsonNode body = mapper.valueToTree(entityData);
                        log.debug("Sending attributes data msg, entityId [{}], attributes [{}]", entityId, body);
                        saveEdgeEvent(tenantId, edge.getId(), type, EdgeEventActionType.ATTRIBUTES_UPDATED, entityId, body);
                    } catch (Exception e) {
                        log.error("[{}] Failed to save attribute updates to the edge", edge.getName(), e);
                        futureToSet.setException(new RuntimeException("[" + edge.getName() + "] Failed to send attribute updates to the edge", e));
                        return;
                    }
                } else {
                    log.trace("[{}][{}] No attributes found for entity {} [{}]", tenantId, edge.getName(), entityId.getEntityType(), entityId.getId());
                }
                futureToSet.set(null);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("Can't find attributes [{}]", attributesRequestMsg, t);
                futureToSet.setException(t);
            }
        }, dbCallbackExecutorService);
        return futureToSet;
    } else {
        log.warn("[{}] Type doesn't supported {}", tenantId, entityId.getEntityType());
        return Futures.immediateFuture(null);
    }
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) EntityId(org.thingsboard.server.common.data.id.EntityId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

EdgeEventType (org.thingsboard.server.common.data.edge.EdgeEventType)7 UUID (java.util.UUID)6 EdgeEventActionType (org.thingsboard.server.common.data.edge.EdgeEventActionType)5 EdgeId (org.thingsboard.server.common.data.id.EdgeId)3 EntityId (org.thingsboard.server.common.data.id.EntityId)3 PageLink (org.thingsboard.server.common.data.page.PageLink)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 HashMap (java.util.HashMap)2 Edge (org.thingsboard.server.common.data.edge.Edge)2 CustomerId (org.thingsboard.server.common.data.id.CustomerId)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Alarm (org.thingsboard.server.common.data.alarm.Alarm)1 AlarmId (org.thingsboard.server.common.data.id.AlarmId)1 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1