Feature/rest client (#2347)

* refactored URLs

* refactored

* refactored

* refactored

* refactored

* refactored rest client

* changed executorService from RestClient

* refactored rest client and JsonConverter
This commit is contained in:
Yevhen Bondarenko 2020-01-20 15:02:00 +02:00 committed by Andrew Shvayka
parent 7d0661a826
commit e583b0a1d3
2 changed files with 258 additions and 138 deletions

View File

@ -31,7 +31,7 @@ import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.async.DeferredResult; import org.thingsboard.client.tools.utils.RestJsonConverter;
import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.ClaimRequest; import org.thingsboard.server.common.data.ClaimRequest;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
@ -57,6 +57,8 @@ import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.page.TextPageData; import org.thingsboard.server.common.data.page.TextPageData;
import org.thingsboard.server.common.data.page.TextPageLink; import org.thingsboard.server.common.data.page.TextPageLink;
import org.thingsboard.server.common.data.page.TimePageData; import org.thingsboard.server.common.data.page.TimePageData;
@ -74,6 +76,7 @@ import org.thingsboard.server.common.data.security.model.UserPasswordPolicy;
import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetType;
import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.common.data.widget.WidgetsBundle;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
@ -81,19 +84,23 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static org.springframework.util.StringUtils.isEmpty; import static org.springframework.util.StringUtils.isEmpty;
/** /**
* @author Andrew Shvayka * @author Andrew Shvayka
*/ */
public class RestClient implements ClientHttpRequestInterceptor { public class RestClient implements ClientHttpRequestInterceptor, Closeable {
private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization"; private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization";
protected final RestTemplate restTemplate; protected final RestTemplate restTemplate;
protected final String baseURL; protected final String baseURL;
private String token; private String token;
private String refreshToken; private String refreshToken;
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
private ExecutorService service = Executors.newWorkStealingPool(10);
protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken=";
@ -256,6 +263,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
return restTemplate.postForEntity(baseURL + deviceCreationUrl, device, Device.class, params).getBody(); return restTemplate.postForEntity(baseURL + deviceCreationUrl, device, Device.class, params).getBody();
} }
public Asset createAsset(Asset asset) { public Asset createAsset(Asset asset) {
return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody(); return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
} }
@ -418,17 +426,17 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
public void ackAlarm(String alarmId) { public void ackAlarm(String alarmId) {
restTemplate.postForObject(baseURL + "/api/alarm/{alarmId}/ack", new Object(), Object.class, alarmId); restTemplate.postForLocation(baseURL + "/api/alarm/{alarmId}/ack", null, alarmId);
} }
public void clearAlarm(String alarmId) { public void clearAlarm(String alarmId) {
restTemplate.postForObject(baseURL + "/api/alarm/{alarmId}/clear", new Object(), Object.class, alarmId); restTemplate.postForLocation(baseURL + "/api/alarm/{alarmId}/clear", null, alarmId);
} }
public TimePageData<AlarmInfo> getAlarms(String entityType, String entityId, String searchStatus, String status, TimePageLink pageLink, Boolean fetchOriginator) { public TimePageData<AlarmInfo> getAlarms(EntityId entityId, String searchStatus, String status, TimePageLink pageLink, Boolean fetchOriginator) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("searchStatus", searchStatus); params.put("searchStatus", searchStatus);
params.put("status", status); params.put("status", status);
params.put("fetchOriginator", String.valueOf(fetchOriginator)); params.put("fetchOriginator", String.valueOf(fetchOriginator));
@ -471,10 +479,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
return urlParams; return urlParams;
} }
public Optional<AlarmSeverity> getHighestAlarmSeverity(String entityType, String entityId, String searchStatus, String status) { public Optional<AlarmSeverity> getHighestAlarmSeverity(EntityId entityId, String searchStatus, String status) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("searchStatus", searchStatus); params.put("searchStatus", searchStatus);
params.put("status", status); params.put("status", status);
try { try {
@ -597,14 +605,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
return assets.getBody(); return assets.getBody();
} }
public List<Asset> getAssetsByIds(String[] assetIds) { public List<Asset> getAssetsByIds(List<String> assetIds) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/assets?assetIds={assetIds}", baseURL + "/api/assets?assetIds={assetIds}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<List<Asset>>() { new ParameterizedTypeReference<List<Asset>>() {
}, },
String.join(",", assetIds)).getBody(); listToString(assetIds)).getBody();
} }
public List<Asset> findByQuery(AssetSearchQuery query) { public List<Asset> findByQuery(AssetSearchQuery query) {
@ -657,10 +665,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
return auditLog.getBody(); return auditLog.getBody();
} }
public TimePageData<AuditLog> getAuditLogsByEntityId(String entityType, String entityId, String actionTypes, TimePageLink pageLink) { public TimePageData<AuditLog> getAuditLogsByEntityId(EntityId entityId, String actionTypes, TimePageLink pageLink) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("actionTypes", actionTypes); params.put("actionTypes", actionTypes);
addPageLinkToParam(params, pageLink); addPageLinkToParam(params, pageLink);
@ -700,14 +708,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
public void logout() { public void logout() {
restTemplate.exchange(URI.create(baseURL + "/api/auth/logout"), HttpMethod.POST, HttpEntity.EMPTY, Object.class); restTemplate.postForLocation(baseURL + "/api/auth/logout", null);
} }
public void changePassword(String currentPassword, String newPassword) { public void changePassword(String currentPassword, String newPassword) {
ObjectNode changePasswordRequest = objectMapper.createObjectNode(); ObjectNode changePasswordRequest = objectMapper.createObjectNode();
changePasswordRequest.put("currentPassword", currentPassword); changePasswordRequest.put("currentPassword", currentPassword);
changePasswordRequest.put("newPassword", newPassword); changePasswordRequest.put("newPassword", newPassword);
restTemplate.exchange(URI.create(baseURL + "/api/auth/changePassword"), HttpMethod.POST, new HttpEntity<>(changePasswordRequest), Object.class); restTemplate.postForLocation(baseURL + "/api/auth/changePassword", changePasswordRequest);
} }
public Optional<UserPasswordPolicy> getUserPasswordPolicy() { public Optional<UserPasswordPolicy> getUserPasswordPolicy() {
@ -731,7 +739,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
public void requestResetPasswordByEmail(String email) { public void requestResetPasswordByEmail(String email) {
ObjectNode resetPasswordByEmailRequest = objectMapper.createObjectNode(); ObjectNode resetPasswordByEmailRequest = objectMapper.createObjectNode();
resetPasswordByEmailRequest.put("email", email); resetPasswordByEmailRequest.put("email", email);
restTemplate.exchange(URI.create(baseURL + "/api/noauth/resetPasswordByEmail"), HttpMethod.POST, new HttpEntity<>(resetPasswordByEmailRequest), Object.class); restTemplate.postForLocation(baseURL + "/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest);
} }
public Optional<JsonNode> activateUser(String userId, String password) { public Optional<JsonNode> activateUser(String userId, String password) {
@ -772,14 +780,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
componentType).getBody(); componentType).getBody();
} }
public List<ComponentDescriptor> getComponentDescriptorsByTypes(String[] componentTypes) { public List<ComponentDescriptor> getComponentDescriptorsByTypes(List<String> componentTypes) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/components?componentTypes={componentTypes}", baseURL + "/api/components?componentTypes={componentTypes}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<List<ComponentDescriptor>>() { new ParameterizedTypeReference<List<ComponentDescriptor>>() {
}, },
String.join(",", componentTypes)).getBody(); listToString(componentTypes)).getBody();
} }
public Optional<Customer> getCustomerById(String customerId) { public Optional<Customer> getCustomerById(String customerId) {
@ -915,7 +923,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public Optional<Dashboard> updateDashboardCustomers(String dashboardId, String[] customerIds) { public Optional<Dashboard> updateDashboardCustomers(String dashboardId, List<String> customerIds) {
try { try {
ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers", customerIds, Dashboard.class, dashboardId); ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers", customerIds, Dashboard.class, dashboardId);
return Optional.ofNullable(dashboard.getBody()); return Optional.ofNullable(dashboard.getBody());
@ -928,7 +936,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public Optional<Dashboard> addDashboardCustomers(String dashboardId, String[] customerIds) { public Optional<Dashboard> addDashboardCustomers(String dashboardId, List<String> customerIds) {
try { try {
ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/add", customerIds, Dashboard.class, dashboardId); ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/add", customerIds, Dashboard.class, dashboardId);
return Optional.ofNullable(dashboard.getBody()); return Optional.ofNullable(dashboard.getBody());
@ -941,7 +949,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public Optional<Dashboard> removeDashboardCustomers(String dashboardId, String[] customerIds) { public Optional<Dashboard> removeDashboardCustomers(String dashboardId, List<String> customerIds) {
try { try {
ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/remove", customerIds, Dashboard.class, dashboardId); ResponseEntity<Dashboard> dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/remove", customerIds, Dashboard.class, dashboardId);
return Optional.ofNullable(dashboard.getBody()); return Optional.ofNullable(dashboard.getBody());
@ -1135,12 +1143,12 @@ public class RestClient implements ClientHttpRequestInterceptor {
.getBody(); .getBody();
} }
public List<Device> getDevicesByIds(String[] deviceIds) { public List<Device> getDevicesByIds(List<String> deviceIds) {
return restTemplate.exchange(baseURL + "/api/devices?deviceIds={deviceIds}", return restTemplate.exchange(baseURL + "/api/devices?deviceIds={deviceIds}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, new ParameterizedTypeReference<List<Device>>() { HttpEntity.EMPTY, new ParameterizedTypeReference<List<Device>>() {
}, },
String.join(",", deviceIds)).getBody(); listToString(deviceIds)).getBody();
} }
public List<Device> findByQuery(DeviceSearchQuery query) { public List<Device> findByQuery(DeviceSearchQuery query) {
@ -1161,28 +1169,22 @@ public class RestClient implements ClientHttpRequestInterceptor {
}).getBody(); }).getBody();
} }
public DeferredResult<ResponseEntity> claimDevice(String deviceName, ClaimRequest claimRequest) { public JsonNode claimDevice(String deviceName, ClaimRequest claimRequest) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/customer/device/{deviceName}/claim", baseURL + "/api/customer/device/{deviceName}/claim",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(claimRequest), new HttpEntity<>(claimRequest),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<JsonNode>() {
}, },
deviceName).getBody(); deviceName).getBody();
} }
public DeferredResult<ResponseEntity> reClaimDevice(String deviceName) { public void reClaimDevice(String deviceName) {
return restTemplate.exchange( restTemplate.delete(baseURL + "/api/customer/device/{deviceName}/claim", deviceName);
baseURL + "/api/customer/device/{deviceName}/claim",
HttpMethod.DELETE,
HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() {
},
deviceName).getBody();
} }
public void saveRelation(EntityRelation relation) { public void saveRelation(EntityRelation relation) {
restTemplate.postForEntity(baseURL + "/api/relation", relation, Object.class); restTemplate.postForLocation(baseURL + "/api/relation", null);
} }
public void deleteRelation(String fromId, String fromType, String relationType, String relationTypeGroup, String toId, String toType) { public void deleteRelation(String fromId, String fromType, String relationType, String relationTypeGroup, String toId, String toType) {
@ -1196,8 +1198,8 @@ public class RestClient implements ClientHttpRequestInterceptor {
restTemplate.delete(baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}", params); restTemplate.delete(baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}", params);
} }
public void deleteRelations(String entityId, String entityType) { public void deleteRelations(EntityId entityId) {
restTemplate.delete(baseURL + "/api/relations?entityId={entityId}&entityType={entityType}", entityId, entityType); restTemplate.delete(baseURL + "/api/relations?entityId={entityId}&entityType={entityType}", entityId.getId().toString(), entityId.getEntityType().name());
} }
public Optional<EntityRelation> getRelation(String fromId, String fromType, String relationType, String relationTypeGroup, String toId, String toType) { public Optional<EntityRelation> getRelation(String fromId, String fromType, String relationType, String relationTypeGroup, String toId, String toType) {
@ -1448,10 +1450,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public TimePageData<Event> getEvents(String entityType, String entityId, String eventType, String tenantId, TimePageLink pageLink) { public TimePageData<Event> getEvents(EntityId entityId, String eventType, String tenantId, TimePageLink pageLink) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("eventType", eventType); params.put("eventType", eventType);
params.put("tenantId", tenantId); params.put("tenantId", tenantId);
addPageLinkToParam(params, pageLink); addPageLinkToParam(params, pageLink);
@ -1465,10 +1467,10 @@ public class RestClient implements ClientHttpRequestInterceptor {
params).getBody(); params).getBody();
} }
public TimePageData<Event> getEvents(String entityType, String entityId, String tenantId, TimePageLink pageLink) { public TimePageData<Event> getEvents(EntityId entityId, String tenantId, TimePageLink pageLink) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("tenantId", tenantId); params.put("tenantId", tenantId);
addPageLinkToParam(params, pageLink); addPageLinkToParam(params, pageLink);
@ -1481,22 +1483,16 @@ public class RestClient implements ClientHttpRequestInterceptor {
params).getBody(); params).getBody();
} }
public DeferredResult<ResponseEntity> handleOneWayDeviceRPCRequest(String deviceId, String requestBody) { public void handleOneWayDeviceRPCRequest(String deviceId, JsonNode requestBody) {
return restTemplate.exchange( restTemplate.postForLocation(baseURL + "/api/plugins/rpc/oneway/{deviceId}", requestBody, deviceId);
baseURL + "/api/plugins/rpc/oneway/{deviceId}",
HttpMethod.POST,
new HttpEntity<>(requestBody),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() {
},
deviceId).getBody();
} }
public DeferredResult<ResponseEntity> handleTwoWayDeviceRPCRequest(String deviceId, String requestBody) { public JsonNode handleTwoWayDeviceRPCRequest(String deviceId, JsonNode requestBody) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/plugins/rpc/twoway/{deviceId}", baseURL + "/api/plugins/rpc/twoway/{deviceId}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(requestBody), new HttpEntity<>(requestBody),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<JsonNode>() {
}, },
deviceId).getBody(); deviceId).getBody();
} }
@ -1590,206 +1586,233 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
} }
public DeferredResult<ResponseEntity> getAttributeKeys(String entityType, String entityId) { public List<String> getAttributeKeys(EntityId entityId) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<String>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId).getBody(); entityId.getId().toString()).getBody();
} }
public DeferredResult<ResponseEntity> getAttributeKeysByScope(String entityType, String entityId, String scope) { public List<String> getAttributeKeysByScope(EntityId entityId, String scope) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes/{scope}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<String>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope).getBody(); scope).getBody();
} }
public DeferredResult<ResponseEntity> getAttributesResponseEntity(String entityType, String entityId, String keys) { public List<AttributeKvEntry> getAttributeKvEntries(EntityId entityId, List<String> keys) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes?keys={keys}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes?keys={keys}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId(),
keys).getBody(); listToString(keys)).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> getAttributesByScope(String entityType, String entityId, String scope, String keys) { public Future<List<AttributeKvEntry>> getAttributeKvEntriesAsync(EntityId entityId, List<String> keys) {
return restTemplate.exchange( return service.submit(() -> getAttributeKvEntries(entityId, keys));
}
public List<AttributeKvEntry> getAttributesByScope(EntityId entityId, String scope, List<String> keys) {
List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}?keys={keys}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}?keys={keys}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope, scope,
keys).getBody(); listToString(keys)).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> getTimeseriesKeys(String entityType, String entityId) { public List<String> getTimeseriesKeys(EntityId entityId) {
return restTemplate.exchange( return restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/timeseries", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/timeseries",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<String>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId).getBody(); entityId.getId().toString()).getBody();
} }
public DeferredResult<ResponseEntity> getLatestTimeseries(String entityType, String entityId, String keys) { public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys) {
return restTemplate.exchange( Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
keys).getBody(); listToString(keys)).getBody();
return RestJsonConverter.toTimeseries(timeseries);
} }
public DeferredResult<ResponseEntity> getTimeseries(String entityType, String entityId, String keys, Long startTs, Long endTs, Long interval, Integer limit, String agg) { public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long startTs, Long endTs, Long interval, Integer limit, String agg) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("keys", keys); params.put("keys", listToString(keys));
params.put("startTs", startTs.toString()); params.put("startTs", startTs.toString());
params.put("endTs", endTs.toString()); params.put("endTs", endTs.toString());
params.put("interval", interval == null ? "0" : interval.toString()); params.put("interval", interval == null ? "0" : interval.toString());
params.put("limit", limit == null ? "100" : limit.toString()); params.put("limit", limit == null ? "100" : limit.toString());
params.put("agg", agg == null ? "NONE" : agg); params.put("agg", agg == null ? "NONE" : agg);
return restTemplate.exchange( Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&startTs={startTs}&endTs={endTs}&interval={interval}&limit={limit}&agg={agg}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&startTs={startTs}&endTs={endTs}&interval={interval}&limit={limit}&agg={agg}",
HttpMethod.GET, HttpMethod.GET,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
}, },
params).getBody(); params).getBody();
return RestJsonConverter.toTimeseries(timeseries);
} }
public DeferredResult<ResponseEntity> saveDeviceAttributes(String deviceId, String scope, JsonNode request) { public List<AttributeKvEntry> saveDeviceAttributes(String deviceId, String scope, JsonNode request) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{deviceId}/{scope}", baseURL + "/api/plugins/telemetry/{deviceId}/{scope}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(request), new HttpEntity<>(request),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
deviceId, deviceId,
scope).getBody(); scope).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> saveEntityAttributesV1(String entityType, String entityId, String scope, JsonNode request) { public List<AttributeKvEntry> saveEntityAttributesV1(EntityId entityId, String scope, JsonNode request) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(request), new HttpEntity<>(request),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope).getBody(); scope).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> saveEntityAttributesV2(String entityType, String entityId, String scope, JsonNode request) { public List<AttributeKvEntry> saveEntityAttributesV2(EntityId entityId, String scope, JsonNode request) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/attributes/{scope}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(request), new HttpEntity<>(request),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope).getBody(); scope).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> saveEntityTelemetry(String entityType, String entityId, String scope, String requestBody) { public List<TsKvEntry> saveEntityTelemetry(EntityId entityId, String scope, String requestBody) {
return restTemplate.exchange( Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(requestBody), new HttpEntity<>(requestBody),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope).getBody(); scope).getBody();
return RestJsonConverter.toTimeseries(timeseries);
} }
public DeferredResult<ResponseEntity> saveEntityTelemetryWithTTL(String entityType, String entityId, String scope, Long ttl, String requestBody) { public List<TsKvEntry> saveEntityTelemetryWithTTL(EntityId entityId, String scope, Long ttl, String requestBody) {
return restTemplate.exchange( Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/{scope}/{ttl}",
HttpMethod.POST, HttpMethod.POST,
new HttpEntity<>(requestBody), new HttpEntity<>(requestBody),
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope, scope,
ttl).getBody(); ttl).getBody();
return RestJsonConverter.toTimeseries(timeseries);
} }
public DeferredResult<ResponseEntity> deleteEntityTimeseries(String entityType, public List<TsKvEntry> deleteEntityTimeseries(EntityId entityId,
String entityId, List<String> keys,
String keys, boolean deleteAllDataForKeys,
boolean deleteAllDataForKeys, Long startTs,
Long startTs, Long endTs,
Long endTs, boolean rewriteLatestIfDeleted) {
boolean rewriteLatestIfDeleted) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("entityType", entityType); params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId); params.put("entityId", entityId.getId().toString());
params.put("keys", keys); params.put("keys", listToString(keys));
params.put("deleteAllDataForKeys", String.valueOf(deleteAllDataForKeys)); params.put("deleteAllDataForKeys", String.valueOf(deleteAllDataForKeys));
params.put("startTs", startTs.toString()); params.put("startTs", startTs.toString());
params.put("endTs", endTs.toString()); params.put("endTs", endTs.toString());
params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted)); params.put("rewriteLatestIfDeleted", String.valueOf(rewriteLatestIfDeleted));
return restTemplate.exchange( Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/timeseries/delete?keys={keys}&deleteAllDataForKeys={deleteAllDataForKeys}&startTs={startTs}&endTs={endTs}&rewriteLatestIfDeleted={rewriteLatestIfDeleted}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
}, },
params).getBody(); params).getBody();
return RestJsonConverter.toTimeseries(timeseries);
} }
public DeferredResult<ResponseEntity> deleteEntityAttributes(String deviceId, String scope, String keys) { public List<AttributeKvEntry> deleteEntityAttributes(String deviceId, String scope, List<String> keys) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}", baseURL + "/api/plugins/telemetry/{deviceId}/{scope}?keys={keys}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
deviceId, deviceId,
scope, scope,
keys).getBody(); listToString(keys)).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public DeferredResult<ResponseEntity> deleteEntityAttributes(String entityType, String entityId, String scope, String keys) { public List<AttributeKvEntry> deleteEntityAttributes(EntityId entityId, String scope, List<String> keys) {
return restTemplate.exchange( List<JsonNode> attributes = restTemplate.exchange(
baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}", baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/{scope}?keys={keys}",
HttpMethod.DELETE, HttpMethod.DELETE,
HttpEntity.EMPTY, HttpEntity.EMPTY,
new ParameterizedTypeReference<DeferredResult<ResponseEntity>>() { new ParameterizedTypeReference<List<JsonNode>>() {
}, },
entityType, entityId.getEntityType().name(),
entityId, entityId.getId().toString(),
scope, scope,
keys).getBody(); listToString(keys)).getBody();
return RestJsonConverter.toAttributes(attributes);
} }
public Optional<Tenant> getTenantById(String tenantId) { public Optional<Tenant> getTenantById(String tenantId) {
@ -1860,7 +1883,7 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
public void sendActivationEmail(String email) { public void sendActivationEmail(String email) {
restTemplate.postForEntity(baseURL + "/api/user/sendActivationMail?email={email}", null, Object.class, email); restTemplate.postForLocation(baseURL + "/api/user/sendActivationMail?email={email}", null, email);
} }
public String getActivationLink(String userId) { public String getActivationLink(String userId) {
@ -1900,10 +1923,9 @@ public class RestClient implements ClientHttpRequestInterceptor {
} }
public void setUserCredentialsEnabled(String userId, boolean userCredentialsEnabled) { public void setUserCredentialsEnabled(String userId, boolean userCredentialsEnabled) {
restTemplate.postForEntity( restTemplate.postForLocation(
baseURL + "/api/user/{userId}/userCredentialsEnabled?serCredentialsEnabled={serCredentialsEnabled}", baseURL + "/api/user/{userId}/userCredentialsEnabled?serCredentialsEnabled={serCredentialsEnabled}",
null, null,
Object.class,
userId, userId,
userCredentialsEnabled); userCredentialsEnabled);
} }
@ -2030,4 +2052,15 @@ public class RestClient implements ClientHttpRequestInterceptor {
params.put("textOffset", pageLink.getTextOffset()); params.put("textOffset", pageLink.getTextOffset());
} }
} }
private String listToString(List<String> list) {
return String.join(",", list);
}
@Override
public void close() {
if (service != null) {
service.shutdown();
}
}
} }

View File

@ -0,0 +1,87 @@
/**
* 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.client.tools.utils;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.util.CollectionUtils;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.BooleanDataEntry;
import org.thingsboard.server.common.data.kv.DoubleDataEntry;
import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.LongDataEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class RestJsonConverter {
private static final String KEY = "key";
private static final String VALUE = "value";
private static final String LAST_UPDATE_TS = "lastUpdateTs";
private static final String TS = "ts";
private static final String CAN_T_PARSE_VALUE = "Can't parse value: ";
public static List<AttributeKvEntry> toAttributes(List<JsonNode> attributes) {
if (!CollectionUtils.isEmpty(attributes)) {
return attributes.stream().map(attr -> {
KvEntry entry = parseValue(attr.get(KEY).asText(), attr.get(VALUE));
return new BaseAttributeKvEntry(entry, attr.get(LAST_UPDATE_TS).asLong());
}
).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
public static List<TsKvEntry> toTimeseries(Map<String, List<JsonNode>> timeseries) {
if (!CollectionUtils.isEmpty(timeseries)) {
List<TsKvEntry> result = new ArrayList<>();
timeseries.forEach((key, values) ->
result.addAll(values.stream().map(ts -> {
KvEntry entry = parseValue(key, ts.get(VALUE));
return new BasicTsKvEntry(ts.get(TS).asLong(), entry);
}
).collect(Collectors.toList()))
);
return result;
} else {
return Collections.emptyList();
}
}
private static KvEntry parseValue(String key, JsonNode value) {
if (!value.isObject()) {
if (value.isBoolean()) {
return new BooleanDataEntry(key, value.asBoolean());
} else if (value.isDouble()) {
return new DoubleDataEntry(key, value.asDouble());
} else if (value.isLong()) {
return new LongDataEntry(key, value.asLong());
} else {
return new StringDataEntry(key, value.asText());
}
} else {
throw new RuntimeException(CAN_T_PARSE_VALUE + value);
}
}
}