Search in sources :

Example 1 with EntityId

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

the class BaseAlarmService method createAlarmRelations.

private void createAlarmRelations(Alarm alarm) throws InterruptedException, ExecutionException {
    if (alarm.isPropagate()) {
        EntityRelationsQuery query = new EntityRelationsQuery();
        query.setParameters(new RelationsSearchParameters(alarm.getOriginator(), EntitySearchDirection.TO, Integer.MAX_VALUE));
        List<EntityId> parentEntities = relationService.findByQuery(query).get().stream().map(r -> r.getFrom()).collect(Collectors.toList());
        for (EntityId parentId : parentEntities) {
            createAlarmRelation(parentId, alarm.getId(), alarm.getStatus(), true);
        }
    }
    createAlarmRelation(alarm.getOriginator(), alarm.getId(), alarm.getStatus(), true);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) PreDestroy(javax.annotation.PreDestroy) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) Service(org.springframework.stereotype.Service) org.thingsboard.server.common.data.alarm(org.thingsboard.server.common.data.alarm) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityService(org.thingsboard.server.dao.entity.EntityService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) Function(com.google.common.base.Function) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) Set(java.util.Set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) TimePageData(org.thingsboard.server.common.data.page.TimePageData) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) StringUtils(org.springframework.util.StringUtils) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters)

Example 2 with EntityId

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

the class CassandraAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], searchStatus [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getSearchStatus(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) CassandraAbstractModelDao(org.thingsboard.server.dao.nosql.CassandraAbstractModelDao) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Select(com.datastax.driver.core.querybuilder.Select) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) AlarmEntity(org.thingsboard.server.dao.model.nosql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with EntityId

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

the class DeviceServiceImpl method findDevicesByQuery.

@Override
public ListenableFuture<List<Device>> findDevicesByQuery(DeviceSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Device>> devices = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Device>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Device>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.DEVICE) {
                futures.add(findDeviceByIdAsync(new DeviceId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() {

        @Nullable
        @Override
        public List<Device> apply(@Nullable List<Device> deviceList) {
            return deviceList == null ? Collections.emptyList() : deviceList.stream().filter(device -> query.getDeviceTypes().contains(device.getType())).collect(Collectors.toList());
        }
    });
    return devices;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) Cacheable(org.springframework.cache.annotation.Cacheable) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) org.thingsboard.server.common.data(org.thingsboard.server.common.data) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Nullable(javax.annotation.Nullable) DEVICE_CACHE(org.thingsboard.server.common.data.CacheConstants.DEVICE_CACHE) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) DeviceSearchQuery(org.thingsboard.server.common.data.device.DeviceSearchQuery) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Nullable(javax.annotation.Nullable)

Example 4 with EntityId

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

the class JpaAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) JpaAbstractDao(org.thingsboard.server.dao.sql.JpaAbstractDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) BaseAlarmService(org.thingsboard.server.dao.alarm.BaseAlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) SqlDao(org.thingsboard.server.dao.util.SqlDao) PageRequest(org.springframework.data.domain.PageRequest) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmDao(org.thingsboard.server.dao.alarm.AlarmDao) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) AlarmEntity(org.thingsboard.server.dao.model.sql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with EntityId

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

the class TelemetryRestMsgHandler method handleHttpDeleteRequest.

@Override
public void handleHttpDeleteRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
    RestRequest request = msg.getRequest();
    Exception error = null;
    try {
        String[] pathParams = request.getPathParams();
        EntityId entityId;
        String scope;
        if (pathParams.length == 2) {
            entityId = DeviceId.fromString(pathParams[0]);
            scope = pathParams[1];
        } else if (pathParams.length == 3) {
            entityId = EntityIdFactory.getByTypeAndId(pathParams[0], pathParams[1]);
            scope = pathParams[2];
        } else {
            msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
            return;
        }
        if (DataConstants.SERVER_SCOPE.equals(scope) || DataConstants.SHARED_SCOPE.equals(scope) || DataConstants.CLIENT_SCOPE.equals(scope)) {
            String keysParam = request.getParameter("keys");
            if (!StringUtils.isEmpty(keysParam)) {
                String[] keys = keysParam.split(",");
                List<String> keyList = Arrays.asList(keys);
                ctx.removeAttributes(ctx.getSecurityCtx().orElseThrow(IllegalArgumentException::new).getTenantId(), entityId, scope, keyList, new PluginCallback<Void>() {

                    @Override
                    public void onSuccess(PluginContext ctx, Void value) {
                        ctx.logAttributesDeleted(msg.getSecurityCtx(), entityId, scope, keyList, null);
                        msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.OK));
                    }

                    @Override
                    public void onFailure(PluginContext ctx, Exception e) {
                        log.error("Failed to remove attributes", e);
                        ctx.logAttributesDeleted(msg.getSecurityCtx(), entityId, scope, keyList, e);
                        handleError(e, msg, HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                });
                return;
            }
        }
    } catch (RuntimeException e) {
        log.debug("Failed to process DELETE request due to Runtime exception", e);
        error = e;
    }
    handleError(error, msg, HttpStatus.BAD_REQUEST);
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) 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) 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)

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