Search in sources :

Example 46 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class EntityViewServiceImpl method findEntityViewsByQuery.

@Override
public ListenableFuture<List<EntityView>> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery());
    ListenableFuture<List<EntityView>> entityViews = Futures.transformAsync(relations, r -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<EntityView>> futures = new ArrayList<>();
        for (EntityRelation relation : r) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.ENTITY_VIEW) {
                futures.add(findEntityViewByIdAsync(tenantId, new EntityViewId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    }, MoreExecutors.directExecutor());
    entityViews = Futures.transform(entityViews, new Function<List<EntityView>, List<EntityView>>() {

        @Nullable
        @Override
        public List<EntityView> apply(@Nullable List<EntityView> entityViewList) {
            return entityViewList == null ? Collections.emptyList() : entityViewList.stream().filter(entityView -> query.getEntityViewTypes().contains(entityView.getType())).collect(Collectors.toList());
        }
    }, MoreExecutors.directExecutor());
    return entityViews;
}
Also used : EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) EntityView(org.thingsboard.server.common.data.EntityView) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(javax.annotation.Nullable)

Example 47 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class DeviceServiceImpl method assignDeviceToTenant.

@Transactional
@Override
public Device assignDeviceToTenant(TenantId tenantId, Device device) {
    log.trace("Executing assignDeviceToTenant [{}][{}]", tenantId, device);
    try {
        List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), device.getId()).get();
        if (!CollectionUtils.isEmpty(entityViews)) {
            throw new DataValidationException("Can't assign device that has entity views to another tenant!");
        }
    } catch (ExecutionException | InterruptedException e) {
        log.error("Exception while finding entity views for deviceId [{}]", device.getId(), e);
        throw new RuntimeException("Exception while finding entity views for deviceId [" + device.getId() + "]", e);
    }
    eventService.removeEvents(device.getTenantId(), device.getId());
    relationService.removeRelations(device.getTenantId(), device.getId());
    TenantId oldTenantId = device.getTenantId();
    device.setTenantId(tenantId);
    device.setCustomerId(null);
    Device savedDevice = doSaveDevice(device, null, true);
    // explicitly remove device with previous tenant id from cache
    // result device object will have different tenant id and will not remove entity from cache
    cacheManager.removeDeviceFromCacheByName(oldTenantId, device.getName());
    cacheManager.removeDeviceFromCacheById(oldTenantId, device.getId());
    return savedDevice;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) EntityView(org.thingsboard.server.common.data.EntityView) Device(org.thingsboard.server.common.data.Device) ExecutionException(java.util.concurrent.ExecutionException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 48 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class AbstractEntityViewEntity method toEntityView.

protected EntityView toEntityView() {
    EntityView entityView = new EntityView(new EntityViewId(getUuid()));
    entityView.setCreatedTime(createdTime);
    if (entityId != null) {
        entityView.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType.name(), entityId));
    }
    if (tenantId != null) {
        entityView.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        entityView.setCustomerId(new CustomerId(customerId));
    }
    entityView.setType(type);
    entityView.setName(name);
    try {
        entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class));
    } catch (IOException e) {
        log.error("Unable to read entity view keys!", e);
    }
    entityView.setStartTimeMs(startTs);
    entityView.setEndTimeMs(endTs);
    entityView.setAdditionalInfo(additionalInfo);
    return entityView;
}
Also used : EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) IOException(java.io.IOException)

Example 49 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method saveAndCreateEntityView.

private EntityView saveAndCreateEntityView(DeviceId deviceId, List<String> timeseries) {
    EntityView entityView = new EntityView();
    entityView.setName("entity_view_name");
    entityView.setType("default");
    entityView.setTenantId(tenantId);
    TelemetryEntityView keys = new TelemetryEntityView();
    keys.setTimeseries(timeseries);
    entityView.setKeys(keys);
    entityView.setEntityId(deviceId);
    return entityViewService.saveEntityView(entityView);
}
Also used : TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView)

Example 50 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method testFindByQueryDescOrder.

@Test
public void testFindByQueryDescOrder() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    saveEntries(deviceId, TS - 3);
    saveEntries(deviceId, TS - 2);
    saveEntries(deviceId, TS - 1);
    List<ReadTsKvQuery> queries = new ArrayList<>();
    queries.add(new BaseReadTsKvQuery(STRING_KEY, TS - 3, TS, 0, 1000, Aggregation.NONE, "DESC"));
    List<TsKvEntry> entries = tsService.findAll(tenantId, deviceId, queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
    EntityView entityView = saveAndCreateEntityView(deviceId, Arrays.asList(STRING_KEY));
    entries = tsService.findAll(tenantId, entityView.getId(), queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

EntityView (org.thingsboard.server.common.data.EntityView)51 TelemetryEntityView (org.thingsboard.server.common.data.objects.TelemetryEntityView)21 Test (org.junit.Test)18 AttributesEntityView (org.thingsboard.server.common.data.objects.AttributesEntityView)17 ExecutionException (java.util.concurrent.ExecutionException)16 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)15 ArrayList (java.util.ArrayList)13 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Customer (org.thingsboard.server.common.data.Customer)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ApiOperation (io.swagger.annotations.ApiOperation)9 List (java.util.List)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 Edge (org.thingsboard.server.common.data.edge.Edge)8 PageLink (org.thingsboard.server.common.data.page.PageLink)7 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6