Search in sources :

Example 6 with EntityDataCmd

use of org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd 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 7 with EntityDataCmd

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

the class BaseWebsocketApiTest method testEntityDataLatestAttrWsCmd.

@Test
public void testEntityDataLatestAttrWsCmd() 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.SERVER_ATTRIBUTE, "serverAttributeKey")));
    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();
    Assert.assertNotNull(msg);
    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.SERVER_ATTRIBUTE).get("serverAttributeKey"));
    Assert.assertEquals(0, pageData.getData().get(0).getLatest().get(EntityKeyType.SERVER_ATTRIBUTE).get("serverAttributeKey").getTs());
    Assert.assertEquals("", pageData.getData().get(0).getLatest().get(EntityKeyType.SERVER_ATTRIBUTE).get("serverAttributeKey").getValue());
    wsClient.registerWaitForUpdate();
    Thread.sleep(500);
    AttributeKvEntry dataPoint1 = new BaseAttributeKvEntry(now - TimeUnit.MINUTES.toMillis(1), new LongDataEntry("serverAttributeKey", 42L));
    List<AttributeKvEntry> tsData = Arrays.asList(dataPoint1);
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, tsData);
    msg = wsClient.waitForUpdate();
    Assert.assertNotNull(msg);
    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.SERVER_ATTRIBUTE));
    TsValue tsValue = listData.get(0).getLatest().get(EntityKeyType.SERVER_ATTRIBUTE).get("serverAttributeKey");
    Assert.assertEquals(new TsValue(dataPoint1.getLastUpdateTs(), dataPoint1.getValueAsString()), tsValue);
    now = System.currentTimeMillis();
    AttributeKvEntry dataPoint2 = new BaseAttributeKvEntry(now, new LongDataEntry("serverAttributeKey", 52L));
    wsClient.registerWaitForUpdate();
    Thread.sleep(500);
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate();
    Assert.assertNotNull(msg);
    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.SERVER_ATTRIBUTE));
    tsValue = eData.get(0).getLatest().get(EntityKeyType.SERVER_ATTRIBUTE).get("serverAttributeKey");
    Assert.assertEquals(new TsValue(dataPoint2.getLastUpdateTs(), dataPoint2.getValueAsString()), tsValue);
    // Sending update from the past, while latest value has new timestamp;
    wsClient.registerWaitForUpdate();
    Thread.sleep(500);
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Arrays.asList(dataPoint1));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
    // Sending duplicate update again
    wsClient.registerWaitForUpdate();
    Thread.sleep(500);
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Arrays.asList(dataPoint2));
    msg = wsClient.waitForUpdate(TimeUnit.SECONDS.toMillis(1));
    Assert.assertNull(msg);
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) 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) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Example 8 with EntityDataCmd

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

the class BaseWebsocketApiTest method testEntityDataHistoryWsCmd.

@Test
public void testEntityDataHistoryWsCmd() 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());
    EntityHistoryCmd historyCmd = new EntityHistoryCmd();
    historyCmd.setKeys(Arrays.asList("temperature"));
    historyCmd.setAgg(Aggregation.NONE);
    historyCmd.setLimit(1000);
    historyCmd.setStartTs(now - TimeUnit.HOURS.toMillis(1));
    historyCmd.setEndTs(now);
    EntityDataCmd cmd = new EntityDataCmd(1, edq, historyCmd, 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.assertEquals(0, pageData.getData().get(0).getTimeseries().get("temperature").length);
    TsKvEntry dataPoint1 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(1), new LongDataEntry("temperature", 42L));
    TsKvEntry dataPoint2 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(2), new LongDataEntry("temperature", 42L));
    TsKvEntry dataPoint3 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(3), new LongDataEntry("temperature", 42L));
    List<TsKvEntry> tsData = Arrays.asList(dataPoint1, dataPoint2, dataPoint3);
    sendTelemetry(device, tsData);
    Thread.sleep(100);
    wsClient.send(mapper.writeValueAsString(wrapper));
    msg = wsClient.waitForReply();
    update = mapper.readValue(msg, EntityDataUpdate.class);
    Assert.assertEquals(1, update.getCmdId());
    List<EntityData> dataList = update.getUpdate();
    Assert.assertNotNull(dataList);
    Assert.assertEquals(1, dataList.size());
    Assert.assertEquals(device.getId(), dataList.get(0).getEntityId());
    TsValue[] tsArray = dataList.get(0).getTimeseries().get("temperature");
    Assert.assertEquals(3, tsArray.length);
    Assert.assertEquals(new TsValue(dataPoint1.getTs(), dataPoint1.getValueAsString()), tsArray[0]);
    Assert.assertEquals(new TsValue(dataPoint2.getTs(), dataPoint2.getValueAsString()), tsArray[1]);
    Assert.assertEquals(new TsValue(dataPoint3.getTs(), dataPoint3.getValueAsString()), tsArray[2]);
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) EntityHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityHistoryCmd) 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) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) 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

EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)8 EntityDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate)8 Device (org.thingsboard.server.common.data.Device)7 EntityData (org.thingsboard.server.common.data.query.EntityData)7 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)7 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)7 EntityDataCmd (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd)7 Test (org.junit.Test)6 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)6 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)6 TsValue (org.thingsboard.server.common.data.query.TsValue)6 EntityKey (org.thingsboard.server.common.data.query.EntityKey)5 LatestValueCmd (org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd)5 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)4 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)4 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)2 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)2 ArrayList (java.util.ArrayList)1 Lwm2mDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)1 SingleEntityFilter (org.thingsboard.server.common.data.query.SingleEntityFilter)1