Search in sources :

Example 1 with EdgeEventActionType

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

the class EdgeProcessor method processEdgeNotification.

public void processEdgeNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    try {
        EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
        EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
        ListenableFuture<Edge> edgeFuture;
        switch(actionType) {
            case ASSIGNED_TO_CUSTOMER:
                CustomerId customerId = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class);
                edgeFuture = edgeService.findEdgeByIdAsync(tenantId, edgeId);
                Futures.addCallback(edgeFuture, new FutureCallback<Edge>() {

                    @Override
                    public void onSuccess(@Nullable Edge edge) {
                        if (edge != null && !customerId.isNullUid()) {
                            saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, EdgeEventActionType.ADDED, customerId, null);
                            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
                            PageData<User> pageData;
                            do {
                                pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
                                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                                    log.trace("[{}] [{}] user(s) are going to be added to edge.", edge.getId(), pageData.getData().size());
                                    for (User user : pageData.getData()) {
                                        saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.USER, EdgeEventActionType.ADDED, user.getId(), null);
                                    }
                                    if (pageData.hasNext()) {
                                        pageLink = pageLink.nextPageLink();
                                    }
                                }
                            } while (pageData != null && pageData.hasNext());
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        log.error("Can't find edge by id [{}]", edgeNotificationMsg, t);
                    }
                }, dbCallbackExecutorService);
                break;
            case UNASSIGNED_FROM_CUSTOMER:
                CustomerId customerIdToDelete = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class);
                edgeFuture = edgeService.findEdgeByIdAsync(tenantId, edgeId);
                Futures.addCallback(edgeFuture, new FutureCallback<Edge>() {

                    @Override
                    public void onSuccess(@Nullable Edge edge) {
                        if (edge != null && !customerIdToDelete.isNullUid()) {
                            saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, EdgeEventActionType.DELETED, customerIdToDelete, null);
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        log.error("Can't find edge by id [{}]", edgeNotificationMsg, t);
                    }
                }, dbCallbackExecutorService);
                break;
        }
    } catch (Exception e) {
        log.error("Exception during processing edge event", e);
    }
}
Also used : User(org.thingsboard.server.common.data.User) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PageData(org.thingsboard.server.common.data.page.PageData) 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 2 with EdgeEventActionType

use of org.thingsboard.server.common.data.edge.EdgeEventActionType 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 EdgeEventActionType

use of org.thingsboard.server.common.data.edge.EdgeEventActionType 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 EdgeEventActionType

use of org.thingsboard.server.common.data.edge.EdgeEventActionType 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 EdgeEventActionType

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

the class EntityEdgeProcessor method processEntityNotificationForAllEdges.

public void processEntityNotificationForAllEdges(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()));
    switch(actionType) {
        case ADDED:
        case UPDATED:
        case DELETED:
            processActionForAllEdges(tenantId, type, actionType, entityId);
            break;
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) UUID(java.util.UUID)

Aggregations

EdgeEventActionType (org.thingsboard.server.common.data.edge.EdgeEventActionType)6 UUID (java.util.UUID)5 EdgeEventType (org.thingsboard.server.common.data.edge.EdgeEventType)5 EdgeId (org.thingsboard.server.common.data.id.EdgeId)4 PageLink (org.thingsboard.server.common.data.page.PageLink)4 Edge (org.thingsboard.server.common.data.edge.Edge)3 CustomerId (org.thingsboard.server.common.data.id.CustomerId)3 EntityId (org.thingsboard.server.common.data.id.EntityId)2 PageData (org.thingsboard.server.common.data.page.PageData)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashMap (java.util.HashMap)1 User (org.thingsboard.server.common.data.User)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