Search in sources :

Example 1 with EntityDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleLatestCmd.

private void handleLatestCmd(TbEntityDataSubCtx ctx, LatestValueCmd latestCmd) {
    log.trace("[{}][{}] Going to process latest command: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
    // Fetch the latest values for telemetry keys (in case they are not copied from NoSQL to SQL DB in hybrid mode.
    if (!tsInSqlDB) {
        log.trace("[{}][{}] Going to fetch missing latest values: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
        List<String> allTsKeys = latestCmd.getKeys().stream().filter(key -> key.getType().equals(EntityKeyType.TIME_SERIES)).map(EntityKey::getKey).collect(Collectors.toList());
        Map<EntityData, ListenableFuture<Map<String, TsValue>>> missingTelemetryFutures = new HashMap<>();
        for (EntityData entityData : ctx.getData().getData()) {
            Map<EntityKeyType, Map<String, TsValue>> latestEntityData = entityData.getLatest();
            Map<String, TsValue> tsEntityData = latestEntityData.get(EntityKeyType.TIME_SERIES);
            Set<String> missingTsKeys = new LinkedHashSet<>(allTsKeys);
            if (tsEntityData != null) {
                missingTsKeys.removeAll(tsEntityData.keySet());
            } else {
                tsEntityData = new HashMap<>();
                latestEntityData.put(EntityKeyType.TIME_SERIES, tsEntityData);
            }
            ListenableFuture<List<TsKvEntry>> missingTsData = tsService.findLatest(ctx.getTenantId(), entityData.getEntityId(), missingTsKeys);
            missingTelemetryFutures.put(entityData, Futures.transform(missingTsData, this::toTsValue, MoreExecutors.directExecutor()));
        }
        Futures.addCallback(Futures.allAsList(missingTelemetryFutures.values()), new FutureCallback<List<Map<String, TsValue>>>() {

            @Override
            public void onSuccess(@Nullable List<Map<String, TsValue>> result) {
                missingTelemetryFutures.forEach((key, value) -> {
                    try {
                        key.getLatest().get(EntityKeyType.TIME_SERIES).putAll(value.get());
                    } catch (InterruptedException | ExecutionException e) {
                        log.warn("[{}][{}] Failed to lookup latest telemetry: {}:{}", ctx.getSessionId(), ctx.getCmdId(), key.getEntityId(), allTsKeys, e);
                    }
                });
                EntityDataUpdate update;
                if (!ctx.isInitialDataSent()) {
                    update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
                    ctx.setInitialDataSent(true);
                } else {
                    update = new EntityDataUpdate(ctx.getCmdId(), null, ctx.getData().getData(), ctx.getMaxEntitiesPerDataSubscription());
                }
                wsService.sendWsMsg(ctx.getSessionId(), update);
                ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
            }

            @Override
            public void onFailure(Throwable t) {
                log.warn("[{}][{}] Failed to process websocket command: {}:{}", ctx.getSessionId(), ctx.getCmdId(), ctx.getQuery(), latestCmd, t);
                wsService.sendWsMsg(ctx.getSessionId(), new EntityDataUpdate(ctx.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR.getCode(), "Failed to process websocket command!"));
            }
        }, wsCallBackExecutor);
    } else {
        if (!ctx.isInitialDataSent()) {
            EntityDataUpdate update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
            wsService.sendWsMsg(ctx.getSessionId(), update);
            ctx.setInitialDataSent(true);
        }
        ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) TsValue(org.thingsboard.server.common.data.query.TsValue) Autowired(org.springframework.beans.factory.annotation.Autowired) CloseStatus(org.springframework.web.socket.CloseStatus) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) EntityService(org.thingsboard.server.dao.entity.EntityService) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) ThreadFactory(java.util.concurrent.ThreadFactory) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) Collectors(java.util.stream.Collectors) GetTsCmd(org.thingsboard.server.service.telemetry.cmd.v2.GetTsCmd) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) EntityHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityHistoryCmd) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) UnsubscribeCmd(org.thingsboard.server.service.telemetry.cmd.v2.UnsubscribeCmd) PostConstruct(javax.annotation.PostConstruct) Lazy(org.springframework.context.annotation.Lazy) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Getter(lombok.Getter) AlarmDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) LinkedHashMap(java.util.LinkedHashMap) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) EntityData(org.thingsboard.server.common.data.query.EntityData) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) FutureCallback(com.google.common.util.concurrent.FutureCallback) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) SubscriptionErrorCode(org.thingsboard.server.service.telemetry.sub.SubscriptionErrorCode) PageData(org.thingsboard.server.common.data.page.PageData) EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with EntityDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleCmd.

@Override
public void handleCmd(TelemetryWebSocketSessionRef session, EntityDataCmd cmd) {
    TbEntityDataSubCtx ctx = getSubCtx(session.getSessionId(), cmd.getCmdId());
    if (ctx != null) {
        log.debug("[{}][{}] Updating existing subscriptions using: {}", session.getSessionId(), cmd.getCmdId(), cmd);
        if (cmd.getLatestCmd() != null || cmd.getTsCmd() != null || cmd.getHistoryCmd() != null) {
            ctx.clearEntitySubscriptions();
        }
    } else {
        log.debug("[{}][{}] Creating new subscription using: {}", session.getSessionId(), cmd.getCmdId(), cmd);
        ctx = createSubCtx(session, cmd);
    }
    ctx.setCurrentCmd(cmd);
    if (cmd.getQuery() != null) {
        if (ctx.getQuery() == null) {
            log.debug("[{}][{}] Initializing data using query: {}", session.getSessionId(), cmd.getCmdId(), cmd.getQuery());
        } else {
            log.debug("[{}][{}] Updating data using query: {}", session.getSessionId(), cmd.getCmdId(), cmd.getQuery());
        }
        ctx.setAndResolveQuery(cmd.getQuery());
        EntityDataQuery query = ctx.getQuery();
        // Step 1. Update existing query with the contents of LatestValueCmd
        if (cmd.getLatestCmd() != null) {
            cmd.getLatestCmd().getKeys().forEach(key -> {
                if (!query.getLatestValues().contains(key)) {
                    query.getLatestValues().add(key);
                }
            });
        }
        long start = System.currentTimeMillis();
        ctx.fetchData();
        long end = System.currentTimeMillis();
        stats.getRegularQueryInvocationCnt().incrementAndGet();
        stats.getRegularQueryTimeSpent().addAndGet(end - start);
        ctx.cancelTasks();
        if (ctx.getQuery().getPageLink().isDynamic()) {
            // TODO: validate number of dynamic page links against rate limits. Ignore dynamic flag if limit is reached.
            TbEntityDataSubCtx finalCtx = ctx;
            ScheduledFuture<?> task = scheduler.scheduleWithFixedDelay(() -> refreshDynamicQuery(finalCtx), dynamicPageLinkRefreshInterval, dynamicPageLinkRefreshInterval, TimeUnit.SECONDS);
            finalCtx.setRefreshTask(task);
        }
    }
    ListenableFuture<TbEntityDataSubCtx> historyFuture;
    if (cmd.getHistoryCmd() != null) {
        log.trace("[{}][{}] Going to process history command: {}", session.getSessionId(), cmd.getCmdId(), cmd.getHistoryCmd());
        try {
            historyFuture = handleHistoryCmd(ctx, cmd.getHistoryCmd());
        } catch (RuntimeException e) {
            handleWsCmdRuntimeException(ctx.getSessionId(), e, cmd);
            return;
        }
    } else {
        historyFuture = Futures.immediateFuture(ctx);
    }
    Futures.addCallback(historyFuture, new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable TbEntityDataSubCtx theCtx) {
            try {
                if (cmd.getLatestCmd() != null) {
                    handleLatestCmd(theCtx, cmd.getLatestCmd());
                } else if (cmd.getTsCmd() != null) {
                    handleTimeSeriesCmd(theCtx, cmd.getTsCmd());
                } else if (!theCtx.isInitialDataSent()) {
                    EntityDataUpdate update = new EntityDataUpdate(theCtx.getCmdId(), theCtx.getData(), null, theCtx.getMaxEntitiesPerDataSubscription());
                    wsService.sendWsMsg(theCtx.getSessionId(), update);
                    theCtx.setInitialDataSent(true);
                }
            } catch (RuntimeException e) {
                handleWsCmdRuntimeException(theCtx.getSessionId(), e, cmd);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            log.warn("[{}][{}] Failed to process command", session.getSessionId(), cmd.getCmdId());
        }
    }, wsCallBackExecutor);
}
Also used : EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery)

Example 3 with EntityDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate in project thingsboard by thingsboard.

the class AbstractLwM2MIntegrationTest method basicTestConnectionObserveTelemetry.

public void basicTestConnectionObserveTelemetry(Security security, LwM2MDeviceCredentials deviceCredentials, Configuration coapConfig, String endpoint) throws Exception {
    Lwm2mDeviceProfileTransportConfiguration transportConfiguration = getTransportConfiguration(OBSERVE_ATTRIBUTES_WITH_PARAMS, getBootstrapServerCredentialsNoSec(NONE));
    createDeviceProfile(transportConfiguration);
    Device device = createDevice(deviceCredentials, endpoint);
    SingleEntityFilter sef = new SingleEntityFilter();
    sef.setSingleEntity(device.getId());
    LatestValueCmd latestCmd = new LatestValueCmd();
    latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
    EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
    TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
    wrapper.setEntityDataCmds(Collections.singletonList(cmd));
    wsClient.send(mapper.writeValueAsString(wrapper));
    wsClient.waitForReply();
    wsClient.registerWaitForUpdate();
    createNewClient(security, coapConfig, false, endpoint, false, null);
    String msg = wsClient.waitForUpdate();
    EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> eData = update.getUpdate();
    Assert.assertNotNull(eData);
    Assert.assertEquals(1, eData.size());
    Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
    Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
    var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
    Assert.assertThat(Long.parseLong(tsValue.getValue()), instanceOf(Long.class));
    int expectedMax = 50;
    int expectedMin = 5;
    Assert.assertTrue(expectedMax >= Long.parseLong(tsValue.getValue()));
    Assert.assertTrue(expectedMin <= Long.parseLong(tsValue.getValue()));
}
Also used : Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) SingleEntityFilter(org.thingsboard.server.common.data.query.SingleEntityFilter) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)

Example 4 with EntityDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate in project thingsboard by thingsboard.

the class BaseWebsocketApiTest method testEntityDataLatestTsWsCmd.

@Test
public void testEntityDataLatestTsWsCmd() throws Exception {
    Device device = new Device();
    device.setName("Device");
    device.setType("default");
    device.setLabel("testLabel" + (int) (Math.random() * 1000));
    device = doPost("/api/device", device, Device.class);
    long now = System.currentTimeMillis();
    DeviceTypeFilter dtf = new DeviceTypeFilter();
    dtf.setDeviceNameFilter("D");
    dtf.setDeviceType("default");
    EntityDataQuery edq = new EntityDataQuery(dtf, new EntityDataPageLink(1, 0, null, null), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    LatestValueCmd latestCmd = new LatestValueCmd();
    latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")));
    EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
    TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
    wrapper.setEntityDataCmds(Collections.singletonList(cmd));
    wsClient.send(mapper.writeValueAsString(wrapper));
    String msg = wsClient.waitForReply();
    EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    PageData<EntityData> pageData = update.getData();
    Assert.assertNotNull(pageData);
    Assert.assertEquals(1, pageData.getData().size());
    Assert.assertEquals(device.getId(), pageData.getData().get(0).getEntityId());
    Assert.assertNotNull(pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature"));
    Assert.assertEquals(0, pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getTs());
    Assert.assertEquals("", pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue());
    TsKvEntry dataPoint1 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(1), new LongDataEntry("temperature", 42L));
    List<TsKvEntry> tsData = Arrays.asList(dataPoint1);
    sendTelemetry(device, tsData);
    Thread.sleep(100);
    cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
    wrapper = new TelemetryPluginCmdsWrapper();
    wrapper.setEntityDataCmds(Collections.singletonList(cmd));
    wsClient.send(mapper.writeValueAsString(wrapper));
    msg = wsClient.waitForReply();
    update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> listData = update.getUpdate();
    Assert.assertNotNull(listData);
    Assert.assertEquals(1, listData.size());
    Assert.assertEquals(device.getId(), listData.get(0).getEntityId());
    Assert.assertNotNull(listData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
    TsValue tsValue = listData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature");
    Assert.assertEquals(new TsValue(dataPoint1.getTs(), dataPoint1.getValueAsString()), tsValue);
    now = System.currentTimeMillis();
    TsKvEntry dataPoint2 = new BasicTsKvEntry(now, new LongDataEntry("temperature", 52L));
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate();
    update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> eData = update.getUpdate();
    Assert.assertNotNull(eData);
    Assert.assertEquals(1, eData.size());
    Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
    Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
    tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature");
    Assert.assertEquals(new TsValue(dataPoint2.getTs(), dataPoint2.getValueAsString()), tsValue);
    // Sending update from the past, while latest value has new timestamp;
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint1));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
    // Sending duplicate update again
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Example 5 with EntityDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate in project thingsboard by thingsboard.

the class BaseWebsocketApiTest method testEntityDataLatestWidgetFlow.

@Test
public void testEntityDataLatestWidgetFlow() throws Exception {
    Device device = new Device();
    device.setName("Device");
    device.setType("default");
    device.setLabel("testLabel" + (int) (Math.random() * 1000));
    device = doPost("/api/device", device, Device.class);
    long now = System.currentTimeMillis();
    DeviceTypeFilter dtf = new DeviceTypeFilter();
    dtf.setDeviceNameFilter("D");
    dtf.setDeviceType("default");
    EntityDataQuery edq = new EntityDataQuery(dtf, new EntityDataPageLink(1, 0, null, null), Collections.emptyList(), Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")), Collections.emptyList());
    EntityDataCmd cmd = new EntityDataCmd(1, edq, null, null, null);
    TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
    wrapper.setEntityDataCmds(Collections.singletonList(cmd));
    wsClient.send(mapper.writeValueAsString(wrapper));
    String msg = wsClient.waitForReply();
    EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    PageData<EntityData> pageData = update.getData();
    Assert.assertNotNull(pageData);
    Assert.assertEquals(1, pageData.getData().size());
    Assert.assertEquals(device.getId(), pageData.getData().get(0).getEntityId());
    Assert.assertNotNull(pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature"));
    Assert.assertEquals(0, pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getTs());
    Assert.assertEquals("", pageData.getData().get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue());
    TsKvEntry dataPoint1 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(1), new LongDataEntry("temperature", 42L));
    List<TsKvEntry> tsData = Arrays.asList(dataPoint1);
    sendTelemetry(device, tsData);
    Thread.sleep(100);
    LatestValueCmd latestCmd = new LatestValueCmd();
    latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")));
    cmd = new EntityDataCmd(1, null, null, latestCmd, null);
    wrapper = new TelemetryPluginCmdsWrapper();
    wrapper.setEntityDataCmds(Collections.singletonList(cmd));
    wsClient.send(mapper.writeValueAsString(wrapper));
    msg = wsClient.waitForReply();
    update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> listData = update.getUpdate();
    Assert.assertNotNull(listData);
    Assert.assertEquals(1, listData.size());
    Assert.assertEquals(device.getId(), listData.get(0).getEntityId());
    Assert.assertNotNull(listData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
    TsValue tsValue = listData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature");
    Assert.assertEquals(new TsValue(dataPoint1.getTs(), dataPoint1.getValueAsString()), tsValue);
    now = System.currentTimeMillis();
    TsKvEntry dataPoint2 = new BasicTsKvEntry(now, new LongDataEntry("temperature", 52L));
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate();
    update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> eData = update.getUpdate();
    Assert.assertNotNull(eData);
    Assert.assertEquals(1, eData.size());
    Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
    Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
    tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("temperature");
    Assert.assertEquals(new TsValue(dataPoint2.getTs(), dataPoint2.getValueAsString()), tsValue);
    // Sending update from the past, while latest value has new timestamp;
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint1));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
    // Sending duplicate update again
    wsClient.registerWaitForUpdate();
    sendTelemetry(device, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Aggregations

EntityDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate)13 EntityData (org.thingsboard.server.common.data.query.EntityData)12 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)11 TsValue (org.thingsboard.server.common.data.query.TsValue)10 EntityDataCmd (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd)10 EntityKey (org.thingsboard.server.common.data.query.EntityKey)9 LatestValueCmd (org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd)8 Device (org.thingsboard.server.common.data.Device)7 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)7 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)7 Test (org.junit.Test)6 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)6 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)6 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 List (java.util.List)4 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)4 TimeSeriesCmd (org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd)4