diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityDataAdapter.java b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityDataAdapter.java index 9277a3bdcd..b9e1f1e790 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityDataAdapter.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityDataAdapter.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.dao.sql.query; +import org.apache.commons.lang3.math.NumberUtils; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.UUIDConverter; import org.thingsboard.server.common.data.id.EntityId; @@ -82,7 +83,7 @@ public class EntityDataAdapter { if (value != null) { String strVal = value.toString(); // check number - if (strVal.length() > 0) { + if (strVal.length() > 0 && NumberUtils.isParsable(strVal)) { try { long longVal = Long.parseLong(strVal); return Long.toString(longVal); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java index ed8f1605e3..d04d18772d 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java @@ -25,6 +25,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntityType; @@ -45,6 +46,8 @@ import org.thingsboard.server.dao.attributes.AttributesService; import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.timeseries.TimeseriesService; +import org.thingsboard.server.dao.util.DaoTestUtil; +import org.thingsboard.server.dao.util.SqlDbType; import java.util.*; import java.util.concurrent.ExecutionException; @@ -60,6 +63,9 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { private TenantId tenantId; + @Autowired + private JdbcTemplate template; + @Before public void before() { Tenant tenant = new Tenant(); @@ -821,6 +827,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { .getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()); } List deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { + // in H2 double values are stored with E0 in the end of the string + loadedTemperatures = loadedTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); + } Assert.assertEquals(deviceTemperatures, loadedTemperatures); pageLink = new EntityDataPageLink(10, 0, null, sortOrder); @@ -848,6 +858,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList()); List deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { + // in H2 double values are stored with E0 in the end of the string + loadedHighTemperatures = loadedHighTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); + } Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures); deviceService.deleteDevicesByTenantId(tenantId); diff --git a/dao/src/test/java/org/thingsboard/server/dao/util/DaoTestUtil.java b/dao/src/test/java/org/thingsboard/server/dao/util/DaoTestUtil.java new file mode 100644 index 0000000000..9484610fc4 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/util/DaoTestUtil.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2016-2020 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.util; + +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.DriverManager; + +public class DaoTestUtil { + private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver"; + private static final String H2_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver"; + + + public static SqlDbType getSqlDbType(JdbcTemplate template){ + try { + String driverName = DriverManager.getDriver(template.getDataSource().getConnection().getMetaData().getURL()).getClass().getName(); + if (POSTGRES_DRIVER_CLASS.equals(driverName)) { + return SqlDbType.POSTGRES; + } else if (H2_DRIVER_CLASS.equals(driverName)) { + return SqlDbType.H2; + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/dao/src/test/java/org/thingsboard/server/dao/util/SqlDbType.java b/dao/src/test/java/org/thingsboard/server/dao/util/SqlDbType.java new file mode 100644 index 0000000000..4e38a48189 --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/util/SqlDbType.java @@ -0,0 +1,20 @@ +/** + * Copyright © 2016-2020 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.util; + +public enum SqlDbType { + POSTGRES, H2; +} diff --git a/ui-ngx/src/app/core/api/entity-data-subscription.ts b/ui-ngx/src/app/core/api/entity-data-subscription.ts index e9d7256520..c1b01daba6 100644 --- a/ui-ngx/src/app/core/api/entity-data-subscription.ts +++ b/ui-ngx/src/app/core/api/entity-data-subscription.ts @@ -624,7 +624,7 @@ export class EntityDataSubscription { } private convertValue(val: string): any { - if (val && this.isNumeric(val)) { + if (val && this.isNumeric(val) && Number(val).toString() === val) { return Number(val); } else { return val; diff --git a/ui-ngx/src/app/core/utils.ts b/ui-ngx/src/app/core/utils.ts index 3b26591c14..dcfb90fbbc 100644 --- a/ui-ngx/src/app/core/utils.ts +++ b/ui-ngx/src/app/core/utils.ts @@ -115,8 +115,7 @@ export function isEmpty(obj: any): boolean { } export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { - if (isDefined(value) && - value !== null && isNumeric(value)) { + if (isDefinedAndNotNull(value) && isNumeric(value) && (isDefined(dec) || isDefined(units) || Number(value).toString() === value)) { let formatted: string | number = Number(value); if (isDefined(dec)) { formatted = formatted.toFixed(dec);