Search in sources :

Example 1 with DeviceSearchQuery

use of org.thingsboard.server.common.data.device.DeviceSearchQuery 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)

Aggregations

Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 Slf4j (lombok.extern.slf4j.Slf4j)1 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Cache (org.springframework.cache.Cache)1 CacheManager (org.springframework.cache.CacheManager)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Cacheable (org.springframework.cache.annotation.Cacheable)1 Service (org.springframework.stereotype.Service)1 StringUtils (org.springframework.util.StringUtils)1 org.thingsboard.server.common.data (org.thingsboard.server.common.data)1 DEVICE_CACHE (org.thingsboard.server.common.data.CacheConstants.DEVICE_CACHE)1 DeviceSearchQuery (org.thingsboard.server.common.data.device.DeviceSearchQuery)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1