Search in sources :

Example 11 with EntityId

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

the class EntityRelationController method findInfoByFrom.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations/info", method = RequestMethod.GET, params = { FROM_ID, FROM_TYPE })
@ResponseBody
public List<EntityRelationInfo> findInfoByFrom(@RequestParam(FROM_ID) String strFromId, @RequestParam(FROM_TYPE) String strFromType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup) throws ThingsboardException {
    checkParameter(FROM_ID, strFromId);
    checkParameter(FROM_TYPE, strFromType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strFromType, strFromId);
    checkEntityId(entityId);
    RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
    try {
        return checkNotNull(relationService.findInfoByFrom(entityId, typeGroup).get());
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 12 with EntityId

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

the class EntityRelationController method deleteRelation.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.DELETE, params = { FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE })
@ResponseStatus(value = HttpStatus.OK)
public void deleteRelation(@RequestParam(FROM_ID) String strFromId, @RequestParam(FROM_TYPE) String strFromType, @RequestParam(RELATION_TYPE) String strRelationType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup, @RequestParam(TO_ID) String strToId, @RequestParam(TO_TYPE) String strToType) throws ThingsboardException {
    checkParameter(FROM_ID, strFromId);
    checkParameter(FROM_TYPE, strFromType);
    checkParameter(RELATION_TYPE, strRelationType);
    checkParameter(TO_ID, strToId);
    checkParameter(TO_TYPE, strToType);
    EntityId fromId = EntityIdFactory.getByTypeAndId(strFromType, strFromId);
    EntityId toId = EntityIdFactory.getByTypeAndId(strToType, strToId);
    checkEntityId(fromId);
    checkEntityId(toId);
    RelationTypeGroup relationTypeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
    try {
        Boolean found = relationService.deleteRelation(fromId, toId, strRelationType, relationTypeGroup);
        if (!found) {
            throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 13 with EntityId

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

the class EntityRelationController method deleteRelations.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations", method = RequestMethod.DELETE, params = { "id", "type" })
@ResponseStatus(value = HttpStatus.OK)
public void deleteRelations(@RequestParam("entityId") String strId, @RequestParam("entityType") String strType) throws ThingsboardException {
    checkParameter("entityId", strId);
    checkParameter("entityType", strType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strType, strId);
    checkEntityId(entityId);
    try {
        relationService.deleteEntityRelations(entityId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with EntityId

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

the class TelemetryRestMsgHandler method handleHttpGetRequest.

@Override
public void handleHttpGetRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
    RestRequest request = msg.getRequest();
    String[] pathParams = request.getPathParams();
    if (pathParams.length < 4) {
        msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
        return;
    }
    String entityType = pathParams[0];
    String entityIdStr = pathParams[1];
    String method = pathParams[2];
    TelemetryFeature feature = TelemetryFeature.forName(pathParams[3]);
    String scope = pathParams.length >= 5 ? pathParams[4] : null;
    if (StringUtils.isEmpty(entityType) || EntityType.valueOf(entityType) == null || StringUtils.isEmpty(entityIdStr) || StringUtils.isEmpty(method) || StringUtils.isEmpty(feature)) {
        msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
        return;
    }
    EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
    if (method.equals("keys")) {
        handleHttpGetKeysMethod(ctx, msg, feature, scope, entityId);
    } else if (method.equals("values")) {
        handleHttpGetValuesMethod(ctx, msg, request, feature, scope, entityId);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest)

Example 15 with EntityId

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

the class TelemetryRestMsgHandler method handleHttpPostRequest.

@Override
public void handleHttpPostRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
    RestRequest request = msg.getRequest();
    Exception error = null;
    try {
        String[] pathParams = request.getPathParams();
        EntityId entityId;
        String scope;
        long ttl = 0L;
        TelemetryFeature feature;
        if (pathParams.length == 2) {
            entityId = DeviceId.fromString(pathParams[0]);
            scope = pathParams[1];
            feature = TelemetryFeature.ATTRIBUTES;
        } else if (pathParams.length == 3) {
            entityId = EntityIdFactory.getByTypeAndId(pathParams[0], pathParams[1]);
            scope = pathParams[2];
            feature = TelemetryFeature.ATTRIBUTES;
        } else if (pathParams.length == 4) {
            entityId = EntityIdFactory.getByTypeAndId(pathParams[0], pathParams[1]);
            feature = TelemetryFeature.forName(pathParams[2].toUpperCase());
            scope = pathParams[3];
        } else if (pathParams.length == 5) {
            entityId = EntityIdFactory.getByTypeAndId(pathParams[0], pathParams[1]);
            feature = TelemetryFeature.forName(pathParams[2].toUpperCase());
            scope = pathParams[3];
            ttl = Long.parseLong(pathParams[4]);
        } else {
            msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
            return;
        }
        if (feature == TelemetryFeature.ATTRIBUTES) {
            if (handleHttpPostAttributes(ctx, msg, request, entityId, scope)) {
                return;
            }
        } else if (feature == TelemetryFeature.TIMESERIES) {
            handleHttpPostTimeseries(ctx, msg, request, entityId, ttl);
            return;
        }
    } catch (IOException | RuntimeException e) {
        log.debug("Failed to process POST request due to exception", e);
        error = e;
    }
    handleError(error, msg, HttpStatus.BAD_REQUEST);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) UncheckedApiException(org.thingsboard.server.extensions.api.exception.UncheckedApiException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) InvalidParametersException(org.thingsboard.server.extensions.api.exception.InvalidParametersException)

Aggregations

EntityId (org.thingsboard.server.common.data.id.EntityId)27 Slf4j (lombok.extern.slf4j.Slf4j)12 StringUtils (org.springframework.util.StringUtils)10 java.util (java.util)9 Function (com.google.common.base.Function)8 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)8 Futures (com.google.common.util.concurrent.Futures)8 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)8 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)7 IOException (java.io.IOException)6 Collectors (java.util.stream.Collectors)6 Service (org.springframework.stereotype.Service)6 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)6 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)6 Nullable (javax.annotation.Nullable)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 ExecutionException (java.util.concurrent.ExecutionException)4