Search in sources :

Example 16 with LongDataEntry

use of org.thingsboard.server.common.data.kv.LongDataEntry in project thingsboard by thingsboard.

the class DefaultSystemDataLoaderService method loadDemoData.

@Override
public void loadDemoData() throws Exception {
    Tenant demoTenant = new Tenant();
    demoTenant.setRegion("Global");
    demoTenant.setTitle("Tenant");
    demoTenant = tenantService.saveTenant(demoTenant);
    installScripts.loadDemoRuleChains(demoTenant.getId());
    createUser(Authority.TENANT_ADMIN, demoTenant.getId(), null, "tenant@thingsboard.org", "tenant");
    Customer customerA = new Customer();
    customerA.setTenantId(demoTenant.getId());
    customerA.setTitle("Customer A");
    customerA = customerService.saveCustomer(customerA);
    Customer customerB = new Customer();
    customerB.setTenantId(demoTenant.getId());
    customerB.setTitle("Customer B");
    customerB = customerService.saveCustomer(customerB);
    Customer customerC = new Customer();
    customerC.setTenantId(demoTenant.getId());
    customerC.setTitle("Customer C");
    customerC = customerService.saveCustomer(customerC);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerA.getId(), "customer@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerA.getId(), "customerA@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerB.getId(), "customerB@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerC.getId(), "customerC@thingsboard.org", CUSTOMER_CRED);
    DeviceProfile defaultDeviceProfile = this.deviceProfileService.findOrCreateDeviceProfile(demoTenant.getId(), DEFAULT_DEVICE_TYPE);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A1", "A1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A2", "A2_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A3", "A3_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerB.getId(), defaultDeviceProfile.getId(), "Test Device B1", "B1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerC.getId(), defaultDeviceProfile.getId(), "Test Device C1", "C1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), null, defaultDeviceProfile.getId(), "DHT11 Demo Device", "DHT11_DEMO_TOKEN", "Demo device that is used in sample " + "applications that upload data from DHT11 temperature and humidity sensor");
    createDevice(demoTenant.getId(), null, defaultDeviceProfile.getId(), "Raspberry Pi Demo Device", "RASPBERRY_PI_DEMO_TOKEN", "Demo device that is used in " + "Raspberry Pi GPIO control sample application");
    DeviceProfile thermostatDeviceProfile = new DeviceProfile();
    thermostatDeviceProfile.setTenantId(demoTenant.getId());
    thermostatDeviceProfile.setDefault(false);
    thermostatDeviceProfile.setName("thermostat");
    thermostatDeviceProfile.setType(DeviceProfileType.DEFAULT);
    thermostatDeviceProfile.setTransportType(DeviceTransportType.DEFAULT);
    thermostatDeviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    thermostatDeviceProfile.setDescription("Thermostat device profile");
    thermostatDeviceProfile.setDefaultRuleChainId(ruleChainService.findTenantRuleChainsByType(demoTenant.getId(), RuleChainType.CORE, new PageLink(1, 0, "Thermostat")).getData().get(0).getId());
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
    DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration();
    DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null);
    deviceProfileData.setConfiguration(configuration);
    deviceProfileData.setTransportConfiguration(transportConfiguration);
    deviceProfileData.setProvisionConfiguration(provisionConfiguration);
    thermostatDeviceProfile.setProfileData(deviceProfileData);
    DeviceProfileAlarm highTemperature = new DeviceProfileAlarm();
    highTemperature.setId("highTemperatureAlarmID");
    highTemperature.setAlarmType("High Temperature");
    AlarmRule temperatureRule = new AlarmRule();
    AlarmCondition temperatureCondition = new AlarmCondition();
    temperatureCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter temperatureAlarmFlagAttributeFilter = new AlarmConditionFilter();
    temperatureAlarmFlagAttributeFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.ATTRIBUTE, "temperatureAlarmFlag"));
    temperatureAlarmFlagAttributeFilter.setValueType(EntityKeyValueType.BOOLEAN);
    BooleanFilterPredicate temperatureAlarmFlagAttributePredicate = new BooleanFilterPredicate();
    temperatureAlarmFlagAttributePredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
    temperatureAlarmFlagAttributePredicate.setValue(new FilterPredicateValue<>(Boolean.TRUE));
    temperatureAlarmFlagAttributeFilter.setPredicate(temperatureAlarmFlagAttributePredicate);
    AlarmConditionFilter temperatureTimeseriesFilter = new AlarmConditionFilter();
    temperatureTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    temperatureTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate temperatureTimeseriesFilterPredicate = new NumericFilterPredicate();
    temperatureTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    FilterPredicateValue<Double> temperatureTimeseriesPredicateValue = new FilterPredicateValue<>(25.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "temperatureAlarmThreshold"));
    temperatureTimeseriesFilterPredicate.setValue(temperatureTimeseriesPredicateValue);
    temperatureTimeseriesFilter.setPredicate(temperatureTimeseriesFilterPredicate);
    temperatureCondition.setCondition(Arrays.asList(temperatureAlarmFlagAttributeFilter, temperatureTimeseriesFilter));
    temperatureRule.setAlarmDetails("Current temperature = ${temperature}");
    temperatureRule.setCondition(temperatureCondition);
    highTemperature.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.MAJOR, temperatureRule)));
    AlarmRule clearTemperatureRule = new AlarmRule();
    AlarmCondition clearTemperatureCondition = new AlarmCondition();
    clearTemperatureCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter clearTemperatureTimeseriesFilter = new AlarmConditionFilter();
    clearTemperatureTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    clearTemperatureTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate clearTemperatureTimeseriesFilterPredicate = new NumericFilterPredicate();
    clearTemperatureTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.LESS_OR_EQUAL);
    FilterPredicateValue<Double> clearTemperatureTimeseriesPredicateValue = new FilterPredicateValue<>(25.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "temperatureAlarmThreshold"));
    clearTemperatureTimeseriesFilterPredicate.setValue(clearTemperatureTimeseriesPredicateValue);
    clearTemperatureTimeseriesFilter.setPredicate(clearTemperatureTimeseriesFilterPredicate);
    clearTemperatureCondition.setCondition(Collections.singletonList(clearTemperatureTimeseriesFilter));
    clearTemperatureRule.setCondition(clearTemperatureCondition);
    clearTemperatureRule.setAlarmDetails("Current temperature = ${temperature}");
    highTemperature.setClearRule(clearTemperatureRule);
    DeviceProfileAlarm lowHumidity = new DeviceProfileAlarm();
    lowHumidity.setId("lowHumidityAlarmID");
    lowHumidity.setAlarmType("Low Humidity");
    AlarmRule humidityRule = new AlarmRule();
    AlarmCondition humidityCondition = new AlarmCondition();
    humidityCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter humidityAlarmFlagAttributeFilter = new AlarmConditionFilter();
    humidityAlarmFlagAttributeFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.ATTRIBUTE, "humidityAlarmFlag"));
    humidityAlarmFlagAttributeFilter.setValueType(EntityKeyValueType.BOOLEAN);
    BooleanFilterPredicate humidityAlarmFlagAttributePredicate = new BooleanFilterPredicate();
    humidityAlarmFlagAttributePredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
    humidityAlarmFlagAttributePredicate.setValue(new FilterPredicateValue<>(Boolean.TRUE));
    humidityAlarmFlagAttributeFilter.setPredicate(humidityAlarmFlagAttributePredicate);
    AlarmConditionFilter humidityTimeseriesFilter = new AlarmConditionFilter();
    humidityTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "humidity"));
    humidityTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate humidityTimeseriesFilterPredicate = new NumericFilterPredicate();
    humidityTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.LESS);
    FilterPredicateValue<Double> humidityTimeseriesPredicateValue = new FilterPredicateValue<>(60.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "humidityAlarmThreshold"));
    humidityTimeseriesFilterPredicate.setValue(humidityTimeseriesPredicateValue);
    humidityTimeseriesFilter.setPredicate(humidityTimeseriesFilterPredicate);
    humidityCondition.setCondition(Arrays.asList(humidityAlarmFlagAttributeFilter, humidityTimeseriesFilter));
    humidityRule.setCondition(humidityCondition);
    humidityRule.setAlarmDetails("Current humidity = ${humidity}");
    lowHumidity.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.MINOR, humidityRule)));
    AlarmRule clearHumidityRule = new AlarmRule();
    AlarmCondition clearHumidityCondition = new AlarmCondition();
    clearHumidityCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter clearHumidityTimeseriesFilter = new AlarmConditionFilter();
    clearHumidityTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "humidity"));
    clearHumidityTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate clearHumidityTimeseriesFilterPredicate = new NumericFilterPredicate();
    clearHumidityTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER_OR_EQUAL);
    FilterPredicateValue<Double> clearHumidityTimeseriesPredicateValue = new FilterPredicateValue<>(60.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "humidityAlarmThreshold"));
    clearHumidityTimeseriesFilterPredicate.setValue(clearHumidityTimeseriesPredicateValue);
    clearHumidityTimeseriesFilter.setPredicate(clearHumidityTimeseriesFilterPredicate);
    clearHumidityCondition.setCondition(Collections.singletonList(clearHumidityTimeseriesFilter));
    clearHumidityRule.setCondition(clearHumidityCondition);
    clearHumidityRule.setAlarmDetails("Current humidity = ${humidity}");
    lowHumidity.setClearRule(clearHumidityRule);
    deviceProfileData.setAlarms(Arrays.asList(highTemperature, lowHumidity));
    DeviceProfile savedThermostatDeviceProfile = deviceProfileService.saveDeviceProfile(thermostatDeviceProfile);
    DeviceId t1Id = createDevice(demoTenant.getId(), null, savedThermostatDeviceProfile.getId(), "Thermostat T1", "T1_TEST_TOKEN", "Demo device for Thermostats dashboard").getId();
    DeviceId t2Id = createDevice(demoTenant.getId(), null, savedThermostatDeviceProfile.getId(), "Thermostat T2", "T2_TEST_TOKEN", "Demo device for Thermostats dashboard").getId();
    attributesService.save(demoTenant.getId(), t1Id, DataConstants.SERVER_SCOPE, Arrays.asList(new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("latitude", 37.3948)), new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("longitude", -122.1503)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("temperatureAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("humidityAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperatureAlarmThreshold", (long) 20)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("humidityAlarmThreshold", (long) 50))));
    attributesService.save(demoTenant.getId(), t2Id, DataConstants.SERVER_SCOPE, Arrays.asList(new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("latitude", 37.493801)), new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("longitude", -121.948769)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("temperatureAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("humidityAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperatureAlarmThreshold", (long) 25)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("humidityAlarmThreshold", (long) 30))));
    installScripts.loadDashboards(demoTenant.getId(), null);
}
Also used : BooleanFilterPredicate(org.thingsboard.server.common.data.query.BooleanFilterPredicate) Customer(org.thingsboard.server.common.data.Customer) AlarmRule(org.thingsboard.server.common.data.device.profile.AlarmRule) DeviceId(org.thingsboard.server.common.data.id.DeviceId) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) PageLink(org.thingsboard.server.common.data.page.PageLink) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) AlarmConditionFilterKey(org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) SimpleAlarmConditionSpec(org.thingsboard.server.common.data.device.profile.SimpleAlarmConditionSpec) AlarmConditionFilter(org.thingsboard.server.common.data.device.profile.AlarmConditionFilter) DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) AlarmCondition(org.thingsboard.server.common.data.device.profile.AlarmCondition) DefaultDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Example 17 with LongDataEntry

use of org.thingsboard.server.common.data.kv.LongDataEntry in project thingsboard by thingsboard.

the class BaseWebsocketApiTest method testEntityCountWsCmd.

@Test
public void testEntityCountWsCmd() 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);
    AttributeKvEntry dataPoint1 = new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperature", 42L));
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Collections.singletonList(dataPoint1));
    DeviceTypeFilter dtf1 = new DeviceTypeFilter();
    dtf1.setDeviceNameFilter("D");
    dtf1.setDeviceType("default");
    EntityCountQuery edq1 = new EntityCountQuery(dtf1, Collections.emptyList());
    EntityCountCmd cmd1 = new EntityCountCmd(1, edq1);
    TelemetryPluginCmdsWrapper wrapper1 = new TelemetryPluginCmdsWrapper();
    wrapper1.setEntityCountCmds(Collections.singletonList(cmd1));
    wsClient.send(mapper.writeValueAsString(wrapper1));
    String msg1 = wsClient.waitForReply();
    EntityCountUpdate update1 = mapper.readValue(msg1, EntityCountUpdate.class);
    Assert.assertEquals(1, update1.getCmdId());
    Assert.assertEquals(1, update1.getCount());
    DeviceTypeFilter dtf2 = new DeviceTypeFilter();
    dtf2.setDeviceNameFilter("D");
    dtf2.setDeviceType("non-existing-device-type");
    EntityCountQuery edq2 = new EntityCountQuery(dtf2, Collections.emptyList());
    EntityCountCmd cmd2 = new EntityCountCmd(2, edq2);
    TelemetryPluginCmdsWrapper wrapper2 = new TelemetryPluginCmdsWrapper();
    wrapper2.setEntityCountCmds(Collections.singletonList(cmd2));
    wsClient.send(mapper.writeValueAsString(wrapper2));
    String msg2 = wsClient.waitForReply();
    EntityCountUpdate update2 = mapper.readValue(msg2, EntityCountUpdate.class);
    Assert.assertEquals(2, update2.getCmdId());
    Assert.assertEquals(0, update2.getCount());
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(40));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    highTemperatureFilter.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf3 = new DeviceTypeFilter();
    dtf3.setDeviceNameFilter("D");
    dtf3.setDeviceType("default");
    EntityCountQuery edq3 = new EntityCountQuery(dtf3, Collections.singletonList(highTemperatureFilter));
    EntityCountCmd cmd3 = new EntityCountCmd(3, edq3);
    TelemetryPluginCmdsWrapper wrapper3 = new TelemetryPluginCmdsWrapper();
    wrapper3.setEntityCountCmds(Collections.singletonList(cmd3));
    wsClient.send(mapper.writeValueAsString(wrapper3));
    String msg3 = wsClient.waitForReply();
    EntityCountUpdate update3 = mapper.readValue(msg3, EntityCountUpdate.class);
    Assert.assertEquals(3, update3.getCmdId());
    Assert.assertEquals(1, update3.getCount());
    KeyFilter highTemperatureFilter2 = new KeyFilter();
    highTemperatureFilter2.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate2 = new NumericFilterPredicate();
    predicate2.setValue(FilterPredicateValue.fromDouble(50));
    predicate2.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter2.setPredicate(predicate2);
    highTemperatureFilter2.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf4 = new DeviceTypeFilter();
    dtf4.setDeviceNameFilter("D");
    dtf4.setDeviceType("default");
    EntityCountQuery edq4 = new EntityCountQuery(dtf4, Collections.singletonList(highTemperatureFilter2));
    EntityCountCmd cmd4 = new EntityCountCmd(4, edq4);
    TelemetryPluginCmdsWrapper wrapper4 = new TelemetryPluginCmdsWrapper();
    wrapper4.setEntityCountCmds(Collections.singletonList(cmd4));
    wsClient.send(mapper.writeValueAsString(wrapper4));
    String msg4 = wsClient.waitForReply();
    EntityCountUpdate update4 = mapper.readValue(msg4, EntityCountUpdate.class);
    Assert.assertEquals(4, update4.getCmdId());
    Assert.assertEquals(0, update4.getCount());
}
Also used : EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityCountUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountUpdate) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Example 18 with LongDataEntry

use of org.thingsboard.server.common.data.kv.LongDataEntry 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 19 with LongDataEntry

use of org.thingsboard.server.common.data.kv.LongDataEntry 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)

Example 20 with LongDataEntry

use of org.thingsboard.server.common.data.kv.LongDataEntry in project thingsboard by thingsboard.

the class JsonConverter method parseNumericValue.

private static void parseNumericValue(List<KvEntry> result, Entry<String, JsonElement> valueEntry, JsonPrimitive value) {
    String valueAsString = value.getAsString();
    String key = valueEntry.getKey();
    var bd = new BigDecimal(valueAsString);
    if (bd.stripTrailingZeros().scale() <= 0 && !isSimpleDouble(valueAsString)) {
        try {
            result.add(new LongDataEntry(key, bd.longValueExact()));
        } catch (ArithmeticException e) {
            if (isTypeCastEnabled) {
                result.add(new StringDataEntry(key, bd.toPlainString()));
            } else {
                throw new JsonSyntaxException("Big integer values are not supported!");
            }
        }
    } else {
        if (bd.scale() <= 16) {
            result.add(new DoubleDataEntry(key, bd.doubleValue()));
        } else if (isTypeCastEnabled) {
            result.add(new StringDataEntry(key, bd.toPlainString()));
        } else {
            throw new JsonSyntaxException("Big integer values are not supported!");
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) BigDecimal(java.math.BigDecimal)

Aggregations

LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)24 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)16 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)14 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)9 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)9 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Device (org.thingsboard.server.common.data.Device)7 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)7 TelemetryPluginCmdsWrapper (org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper)7 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)6 EntityData (org.thingsboard.server.common.data.query.EntityData)6 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)6 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)6 TsValue (org.thingsboard.server.common.data.query.TsValue)6 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 JsonDataEntry (org.thingsboard.server.common.data.kv.JsonDataEntry)5 EntityKey (org.thingsboard.server.common.data.query.EntityKey)5 ApiUsageRecordKey (org.thingsboard.server.common.data.ApiUsageRecordKey)4