Search in sources :

Example 6 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId in project thingsboard by thingsboard.

the class EdgeEventEntity method toData.

@Override
public EdgeEvent toData() {
    EdgeEvent edgeEvent = new EdgeEvent(new EdgeEventId(this.getUuid()));
    edgeEvent.setCreatedTime(createdTime);
    edgeEvent.setTenantId(TenantId.fromUUID(tenantId));
    edgeEvent.setEdgeId(new EdgeId(edgeId));
    if (entityId != null) {
        edgeEvent.setEntityId(entityId);
    }
    edgeEvent.setType(edgeEventType);
    edgeEvent.setAction(edgeEventAction);
    edgeEvent.setBody(entityBody);
    edgeEvent.setUid(edgeEventUid);
    return edgeEvent;
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) EdgeEventId(org.thingsboard.server.common.data.id.EdgeEventId) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 7 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId in project thingsboard by thingsboard.

the class TbMsgPushToEdgeNode method processMsg.

private void processMsg(TbContext ctx, TbMsg msg) {
    if (EntityType.EDGE.equals(msg.getOriginator().getEntityType())) {
        EdgeEvent edgeEvent = buildEdgeEvent(msg, ctx);
        if (edgeEvent != null) {
            EdgeId edgeId = new EdgeId(msg.getOriginator().getId());
            notifyEdge(ctx, msg, edgeEvent, edgeId);
        }
    } else {
        PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
        PageData<EdgeId> pageData;
        do {
            pageData = ctx.getEdgeService().findRelatedEdgeIdsByEntityId(ctx.getTenantId(), msg.getOriginator(), pageLink);
            if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                for (EdgeId edgeId : pageData.getData()) {
                    EdgeEvent edgeEvent = buildEdgeEvent(msg, ctx);
                    if (edgeEvent == null) {
                        log.debug("Edge event type is null. Entity Type {}", msg.getOriginator().getEntityType());
                        ctx.tellFailure(msg, new RuntimeException("Edge event type is null. Entity Type '" + msg.getOriginator().getEntityType() + "'"));
                    } else {
                        notifyEdge(ctx, msg, edgeEvent, edgeId);
                    }
                }
                if (pageData.hasNext()) {
                    pageLink = pageLink.nextPageLink();
                }
            }
        } while (pageData != null && pageData.hasNext());
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 8 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId in project thingsboard by thingsboard.

the class EdgeController method getEdgesByIds.

@ApiOperation(value = "Get Edges By Ids (getEdgesByIds)", notes = "Requested edges must be owned by tenant or assigned to customer which user is performing the request." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/edges", params = { "edgeIds" }, method = RequestMethod.GET)
@ResponseBody
public List<Edge> getEdgesByIds(@ApiParam(value = "A list of edges ids, separated by comma ','", required = true) @RequestParam("edgeIds") String[] strEdgeIds) throws ThingsboardException {
    checkArrayParameter("edgeIds", strEdgeIds);
    try {
        SecurityUser user = getCurrentUser();
        TenantId tenantId = user.getTenantId();
        CustomerId customerId = user.getCustomerId();
        List<EdgeId> edgeIds = new ArrayList<>();
        for (String strEdgeId : strEdgeIds) {
            edgeIds.add(new EdgeId(toUUID(strEdgeId)));
        }
        ListenableFuture<List<Edge>> edgesFuture;
        if (customerId == null || customerId.isNullUid()) {
            edgesFuture = edgeService.findEdgesByTenantIdAndIdsAsync(tenantId, edgeIds);
        } else {
            edgesFuture = edgeService.findEdgesByTenantIdCustomerIdAndIdsAsync(tenantId, customerId, edgeIds);
        }
        List<Edge> edges = edgesFuture.get();
        return checkNotNull(edges);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId in project thingsboard by thingsboard.

the class EdgeController method syncEdge.

@ApiOperation(value = "Sync edge (syncEdge)", notes = "Starts synchronization process between edge and cloud. \n" + "All entities that are assigned to particular edge are going to be send to remote edge service." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/sync/{edgeId}", method = RequestMethod.POST)
public void syncEdge(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable("edgeId") String strEdgeId) throws ThingsboardException {
    checkParameter("edgeId", strEdgeId);
    try {
        if (isEdgesEnabled()) {
            EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
            edgeId = checkNotNull(edgeId);
            SecurityUser user = getCurrentUser();
            TenantId tenantId = user.getTenantId();
            edgeGrpcService.startSyncProcess(tenantId, edgeId);
        } else {
            throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL);
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId in project thingsboard by thingsboard.

the class EdgeController method unassignEdgeFromCustomer.

@ApiOperation(value = "Unassign edge from customer (unassignEdgeFromCustomer)", notes = "Clears assignment of the edge to customer. Customer will not be able to query edge afterwards." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/edge/{edgeId}", method = RequestMethod.DELETE)
@ResponseBody
public Edge unassignEdgeFromCustomer(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable(EDGE_ID) String strEdgeId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.UNASSIGN_FROM_CUSTOMER);
        if (edge.getCustomerId() == null || edge.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Edge isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(edge.getCustomerId(), Operation.READ);
        Edge savedEdge = checkNotNull(edgeService.unassignEdgeFromCustomer(getCurrentUser().getTenantId(), edgeId));
        tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, ComponentLifecycleEvent.UPDATED);
        logEntityAction(edgeId, edge, edge.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strEdgeId, customer.getId().toString(), customer.getName());
        sendEntityAssignToCustomerNotificationMsg(savedEdge.getTenantId(), savedEdge.getId(), customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER);
        return savedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strEdgeId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) EdgeId(org.thingsboard.server.common.data.id.EdgeId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

EdgeId (org.thingsboard.server.common.data.id.EdgeId)57 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)33 ApiOperation (io.swagger.annotations.ApiOperation)32 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)32 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)32 Edge (org.thingsboard.server.common.data.edge.Edge)29 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)23 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)14 PageLink (org.thingsboard.server.common.data.page.PageLink)14 IOException (java.io.IOException)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)12 DeviceId (org.thingsboard.server.common.data.id.DeviceId)10 ArrayList (java.util.ArrayList)9 Customer (org.thingsboard.server.common.data.Customer)9 EdgeEventActionType (org.thingsboard.server.common.data.edge.EdgeEventActionType)9 List (java.util.List)8 UUID (java.util.UUID)7 Device (org.thingsboard.server.common.data.Device)7