Search in sources :

Example 41 with EntityView

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

the class BaseEntityViewControllerTest method testGetTelemetryWhenEntityViewTimeRangeInsideTimestampRange.

@Test
public void testGetTelemetryWhenEntityViewTimeRangeInsideTimestampRange() throws Exception {
    uploadTelemetry("{\"tsKey1\":\"value1\", \"tsKey2\":true, \"tsKey3\":40.0}");
    Thread.sleep(1000);
    long startTimeMs = System.currentTimeMillis();
    uploadTelemetry("{\"tsKey1\":\"value2\", \"tsKey2\":false, \"tsKey3\":80.0}");
    Thread.sleep(1000);
    uploadTelemetry("{\"tsKey1\":\"value3\", \"tsKey2\":false, \"tsKey3\":120.0}");
    long endTimeMs = System.currentTimeMillis();
    uploadTelemetry("{\"tsKey1\":\"value4\", \"tsKey2\":true, \"tsKey3\":160.0}");
    String deviceId = testDevice.getId().getId().toString();
    Set<String> keys = getTelemetryKeys("DEVICE", deviceId);
    Thread.sleep(1000);
    EntityView view = createEntityView("Test entity view", startTimeMs, endTimeMs);
    EntityView savedView = doPost("/api/entityView", view, EntityView.class);
    String entityViewId = savedView.getId().getId().toString();
    Map<String, List<Map<String, String>>> expectedValues = getTelemetryValues("DEVICE", deviceId, keys, 0L, (startTimeMs + endTimeMs) / 2);
    Assert.assertEquals(2, expectedValues.get("tsKey1").size());
    Assert.assertEquals(2, expectedValues.get("tsKey2").size());
    Assert.assertEquals(2, expectedValues.get("tsKey3").size());
    Map<String, List<Map<String, String>>> actualValues = getTelemetryValues("ENTITY_VIEW", entityViewId, keys, 0L, (startTimeMs + endTimeMs) / 2);
    Assert.assertEquals(1, actualValues.get("tsKey1").size());
    Assert.assertEquals(1, actualValues.get("tsKey2").size());
    Assert.assertEquals(1, actualValues.get("tsKey3").size());
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 42 with EntityView

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

the class BaseEntityViewControllerTest method createEntityView.

private EntityView createEntityView(String name, long startTimeMs, long endTimeMs) {
    EntityView view = new EntityView();
    view.setEntityId(testDevice.getId());
    view.setTenantId(savedTenant.getId());
    view.setName(name);
    view.setType("default");
    view.setKeys(telemetry);
    view.setStartTimeMs(startTimeMs);
    view.setEndTimeMs(endTimeMs);
    return view;
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView)

Example 43 with EntityView

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

the class DeviceServiceImpl method deleteDevice.

@Transactional
@Override
public void deleteDevice(final TenantId tenantId, final DeviceId deviceId) {
    log.trace("Executing deleteDevice [{}]", deviceId);
    validateId(deviceId, INCORRECT_DEVICE_ID + deviceId);
    Device device = deviceDao.findById(tenantId, deviceId.getId());
    final String deviceName = device.getName();
    try {
        List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), deviceId).get();
        if (entityViews != null && !entityViews.isEmpty()) {
            throw new DataValidationException("Can't delete device that has entity views!");
        }
    } catch (ExecutionException | InterruptedException e) {
        log.error("Exception while finding entity views for deviceId [{}]", deviceId, e);
        throw new RuntimeException("Exception while finding entity views for deviceId [" + deviceId + "]", e);
    }
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, deviceId);
    if (deviceCredentials != null) {
        deviceCredentialsService.deleteDeviceCredentials(tenantId, deviceCredentials);
    }
    deleteEntityRelations(tenantId, deviceId);
    deviceDao.removeById(tenantId, deviceId.getId());
    cacheManager.removeDeviceFromCacheByName(tenantId, deviceName);
    cacheManager.removeDeviceFromCacheById(tenantId, deviceId);
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) EntityView(org.thingsboard.server.common.data.EntityView) Device(org.thingsboard.server.common.data.Device) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) ExecutionException(java.util.concurrent.ExecutionException) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Transactional(org.springframework.transaction.annotation.Transactional)

Example 44 with EntityView

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

the class EntityViewServiceImpl method findEntityViewsByTenantIdAndEntityIdAsync.

@Override
public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId) {
    log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId);
    validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
    validateId(entityId.getId(), "Incorrect entityId" + entityId);
    List<Object> tenantIdAndEntityId = new ArrayList<>();
    tenantIdAndEntityId.add(tenantId);
    tenantIdAndEntityId.add(entityId);
    Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE);
    @SuppressWarnings("unchecked") List<EntityView> fromCache = cache.get(tenantIdAndEntityId, List.class);
    if (fromCache != null) {
        return Futures.immediateFuture(fromCache);
    } else {
        ListenableFuture<List<EntityView>> entityViewsFuture = entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId());
        Futures.addCallback(entityViewsFuture, new FutureCallback<List<EntityView>>() {

            @Override
            public void onSuccess(@Nullable List<EntityView> result) {
                cache.putIfAbsent(tenantIdAndEntityId, result);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("Error while finding entity views by tenantId and entityId", t);
            }
        }, MoreExecutors.directExecutor());
        return entityViewsFuture;
    }
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.springframework.cache.Cache)

Example 45 with EntityView

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

the class EntityViewServiceImpl method assignEntityViewToCustomer.

@CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}")
@Override
public EntityView assignEntityViewToCustomer(TenantId tenantId, EntityViewId entityViewId, CustomerId customerId) {
    EntityView entityView = findEntityViewById(tenantId, entityViewId);
    entityView.setCustomerId(customerId);
    return saveEntityView(entityView);
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) CacheEvict(org.springframework.cache.annotation.CacheEvict)

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