Search in sources :

Example 1 with TimeSeriesCmd

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

the class BaseWebsocketApiTest method testEntityDataTimeSeriesWsCmd.

@Test
public void testEntityDataTimeSeriesWsCmd() 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());
    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());
    TimeSeriesCmd tsCmd = new TimeSeriesCmd();
    tsCmd.setKeys(Arrays.asList("temperature"));
    tsCmd.setAgg(Aggregation.NONE);
    tsCmd.setLimit(1000);
    tsCmd.setStartTs(now - TimeUnit.HOURS.toMillis(1));
    tsCmd.setTimeWindow(TimeUnit.HOURS.toMillis(1));
    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", 43L));
    TsKvEntry dataPoint3 = new BasicTsKvEntry(now - TimeUnit.MINUTES.toMillis(3), new LongDataEntry("temperature", 44L));
    List<TsKvEntry> tsData = Arrays.asList(dataPoint1, dataPoint2, dataPoint3);
    sendTelemetry(device, tsData);
    Thread.sleep(100);
    cmd = new EntityDataCmd(1, null, null, null, tsCmd);
    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());
    TsValue[] tsArray = listData.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]);
    now = System.currentTimeMillis();
    TsKvEntry dataPoint4 = new BasicTsKvEntry(now, new LongDataEntry("temperature", 45L));
    wsClient.registerWaitForUpdate();
    Thread.sleep(100);
    sendTelemetry(device, Arrays.asList(dataPoint4));
    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).getTimeseries());
    TsValue[] tsValues = eData.get(0).getTimeseries().get("temperature");
    Assert.assertNotNull(tsValues);
    Assert.assertEquals(new TsValue(dataPoint4.getTs(), dataPoint4.getValueAsString()), tsValues[0]);
}
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) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) 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

Test (org.junit.Test)1 Device (org.thingsboard.server.common.data.Device)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)1 EntityData (org.thingsboard.server.common.data.query.EntityData)1 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)1 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)1 TsValue (org.thingsboard.server.common.data.query.TsValue)1 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)1 EntityDataCmd (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd)1 EntityDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate)1 TimeSeriesCmd (org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd)1