Commit 9c5e3c769d55fac05e0aee2780114f460e73fa1f

Authored by Igor Kulikov
2 parents 44a938d8 620b66b7

Merge branch 'develop/3.0' of github.com:thingsboard/thingsboard into develop/3.0

... ... @@ -182,11 +182,12 @@ public class TelemetryController extends BaseController {
182 182 @ResponseBody
183 183 public DeferredResult<ResponseEntity> getLatestTimeseries(
184 184 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
185   - @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException {
  185 + @RequestParam(name = "keys", required = false) String keysStr,
  186 + @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
186 187 SecurityUser user = getCurrentUser();
187 188
188 189 return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
189   - (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr));
  190 + (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr, useStrictDataTypes));
190 191 }
191 192
192 193
... ... @@ -200,8 +201,8 @@ public class TelemetryController extends BaseController {
200 201 @RequestParam(name = "endTs") Long endTs,
201 202 @RequestParam(name = "interval", defaultValue = "0") Long interval,
202 203 @RequestParam(name = "limit", defaultValue = "100") Integer limit,
203   - @RequestParam(name = "agg", defaultValue = "NONE") String aggStr
204   - ) throws ThingsboardException {
  204 + @RequestParam(name = "agg", defaultValue = "NONE") String aggStr,
  205 + @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
205 206 return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
206 207 (result, tenantId, entityId) -> {
207 208 // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
... ... @@ -209,7 +210,7 @@ public class TelemetryController extends BaseController {
209 210 List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg))
210 211 .collect(Collectors.toList());
211 212
212   - Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result));
  213 + Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictDataTypes));
213 214 });
214 215 }
215 216
... ... @@ -454,14 +455,14 @@ public class TelemetryController extends BaseController {
454 455 });
455 456 }
456 457
457   - private void getLatestTimeseriesValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String keys) {
  458 + private void getLatestTimeseriesValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String keys, Boolean useStrictDataTypes) {
458 459 ListenableFuture<List<TsKvEntry>> future;
459 460 if (StringUtils.isEmpty(keys)) {
460 461 future = tsService.findAllLatest(user.getTenantId(), entityId);
461 462 } else {
462 463 future = tsService.findLatest(user.getTenantId(), entityId, toKeysList(keys));
463 464 }
464   - Futures.addCallback(future, getTsKvListCallback(result));
  465 + Futures.addCallback(future, getTsKvListCallback(result, useStrictDataTypes));
465 466 }
466 467
467 468 private void getAttributeValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String scope, String keys) {
... ... @@ -544,7 +545,7 @@ public class TelemetryController extends BaseController {
544 545 @Override
545 546 public void onSuccess(List<AttributeKvEntry> attributes) {
546 547 List<AttributeData> values = attributes.stream().map(attribute ->
547   - new AttributeData(attribute.getLastUpdateTs(), attribute.getKey(), getKvValue(attribute))
  548 + new AttributeData(attribute.getLastUpdateTs(), attribute.getKey(), getKvValue(attribute))
548 549 ).collect(Collectors.toList());
549 550 logAttributesRead(user, entityId, scope, keyList, null);
550 551 response.setResult(new ResponseEntity<>(values, HttpStatus.OK));
... ... @@ -559,14 +560,14 @@ public class TelemetryController extends BaseController {
559 560 };
560 561 }
561 562
562   - private FutureCallback<List<TsKvEntry>> getTsKvListCallback(final DeferredResult<ResponseEntity> response) {
  563 + private FutureCallback<List<TsKvEntry>> getTsKvListCallback(final DeferredResult<ResponseEntity> response, Boolean useStrictDataTypes) {
563 564 return new FutureCallback<List<TsKvEntry>>() {
564 565 @Override
565 566 public void onSuccess(List<TsKvEntry> data) {
566 567 Map<String, List<TsData>> result = new LinkedHashMap<>();
567 568 for (TsKvEntry entry : data) {
568   - result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>())
569   - .add(new TsData(entry.getTs(), entry.getValueAsString()));
  569 + Object value = useStrictDataTypes ? getKvValue(entry) : entry.getValueAsString();
  570 + result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(new TsData(entry.getTs(), value));
570 571 }
571 572 response.setResult(new ResponseEntity<>(result, HttpStatus.OK));
572 573 }
... ...
... ... @@ -18,9 +18,9 @@ package org.thingsboard.server.service.telemetry;
18 18 public class TsData implements Comparable<TsData>{
19 19
20 20 private final long ts;
21   - private final String value;
  21 + private final Object value;
22 22
23   - public TsData(long ts, String value) {
  23 + public TsData(long ts, Object value) {
24 24 super();
25 25 this.ts = ts;
26 26 this.value = value;
... ... @@ -30,7 +30,7 @@ public class TsData implements Comparable<TsData>{
30 30 return ts;
31 31 }
32 32
33   - public String getValue() {
  33 + public Object getValue() {
34 34 return value;
35 35 }
36 36
... ...
... ... @@ -97,6 +97,7 @@ import java.util.HashMap;
97 97 import java.util.List;
98 98 import java.util.Map;
99 99 import java.util.Optional;
  100 +import java.util.UUID;
100 101 import java.util.concurrent.ExecutorService;
101 102 import java.util.concurrent.Executors;
102 103 import java.util.concurrent.Future;
... ... @@ -1611,31 +1612,40 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1611 1612 }
1612 1613
1613 1614 public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys) {
  1615 + return getLatestTimeseries(entityId, keys, true);
  1616 + }
  1617 +
  1618 + public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys, boolean useStrictDataTypes) {
1614 1619 Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
1615   - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}",
  1620 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&useStrictDataTypes={useStrictDataTypes}",
1616 1621 HttpMethod.GET,
1617 1622 HttpEntity.EMPTY,
1618 1623 new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
1619 1624 },
1620 1625 entityId.getEntityType().name(),
1621 1626 entityId.getId().toString(),
1622   - listToString(keys)).getBody();
  1627 + listToString(keys),
  1628 + useStrictDataTypes).getBody();
1623 1629
1624 1630 return RestJsonConverter.toTimeseries(timeseries);
1625 1631 }
1626 1632
1627   -
1628 1633 public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long interval, Aggregation agg, TimePageLink pageLink) {
  1634 + return getTimeseries(entityId, keys, interval, agg, pageLink, true);
  1635 + }
  1636 +
  1637 + public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long interval, Aggregation agg, TimePageLink pageLink, boolean useStrictDataTypes) {
1629 1638 Map<String, String> params = new HashMap<>();
1630   - addPageLinkToParam(params, pageLink);
1631 1639 params.put("entityType", entityId.getEntityType().name());
1632 1640 params.put("entityId", entityId.getId().toString());
1633 1641 params.put("keys", listToString(keys));
1634 1642 params.put("interval", interval == null ? "0" : interval.toString());
1635 1643 params.put("agg", agg == null ? "NONE" : agg.name());
  1644 + params.put("useStrictDataTypes", Boolean.toString(useStrictDataTypes));
  1645 + addPageLinkToParam(params, pageLink);
1636 1646
1637 1647 Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
1638   - baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&interval={interval}&agg={agg}&" + getUrlParams(pageLink),
  1648 + baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&interval={interval}&agg={agg}&useStrictDataTypes={useStrictDataTypes}&" + getUrlParams(pageLink),
1639 1649 HttpMethod.GET,
1640 1650 HttpEntity.EMPTY,
1641 1651 new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
... ...