Rest client resources and firmware methods (#4522)

* Added methods to RestClient from Resources and Firmware Controllers

* Improvements according to last master

* Refactored for new entity names, added new method from device profile controller
This commit is contained in:
AndrewVolosytnykhThingsboard 2021-06-03 13:53:37 +03:00 committed by GitHub
parent b978f0f7ab
commit f9713c4fce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,18 +19,25 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.thingsboard.common.util.ThingsBoardExecutors;
import org.thingsboard.rest.client.utils.RestJsonConverter;
import org.thingsboard.server.common.data.AdminSettings;
@ -48,6 +55,10 @@ import org.thingsboard.server.common.data.EntitySubtype;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.EntityViewInfo;
import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.OtaPackage;
import org.thingsboard.server.common.data.OtaPackageInfo;
import org.thingsboard.server.common.data.TbResource;
import org.thingsboard.server.common.data.TbResourceInfo;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.TenantInfo;
import org.thingsboard.server.common.data.TenantProfile;
@ -78,8 +89,10 @@ import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId;
import org.thingsboard.server.common.data.id.OtaPackageId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.RuleNodeId;
import org.thingsboard.server.common.data.id.TbResourceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.TenantProfileId;
import org.thingsboard.server.common.data.id.UserId;
@ -91,6 +104,8 @@ import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams;
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm;
import org.thingsboard.server.common.data.ota.OtaPackageType;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.page.SortOrder;
@ -127,9 +142,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
@ -147,7 +162,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
private final ObjectMapper objectMapper = new ObjectMapper();
private ExecutorService service = ThingsBoardExecutors.newWorkStealingPool(10, getClass());
protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken=";
public RestClient(String baseURL) {
@ -1238,6 +1252,21 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
HttpEntity.EMPTY, Device.class, tenantId, deviceId).getBody();
}
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(OtaPackageType otaPackageType, DeviceProfileId deviceProfileId) {
Map<String, String> params = new HashMap<>();
params.put("otaPackageType", otaPackageType.name());
params.put("deviceProfileId", deviceProfileId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/devices/count/{otaPackageType}?deviceProfileId={deviceProfileId}",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Long>() {
},
params
).getBody();
}
@Deprecated
public Device createDevice(String name, String type) {
Device device = new Device();
@ -2830,6 +2859,176 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
restTemplate.postForEntity(baseURL + "/api/edge/sync/{edgeId}", null, EdgeId.class, params);
}
public ResponseEntity<Resource> downloadResource(TbResourceId resourceId) {
Map<String, String> params = new HashMap<>();
params.put("resourceId", resourceId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/resource/{resourceId}/download",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<>() {},
params
);
}
public TbResourceInfo getResourceInfoById(TbResourceId resourceId) {
Map<String, String> params = new HashMap<>();
params.put("resourceId", resourceId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/resource/info/{resourceId}",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<TbResourceInfo>() {},
params
).getBody();
}
public TbResource getResourceId(TbResourceId resourceId) {
Map<String, String> params = new HashMap<>();
params.put("resourceId", resourceId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/resource/{resourceId}",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<TbResource>() {},
params
).getBody();
}
public TbResource saveResource(TbResource resource) {
return restTemplate.postForEntity(
baseURL + "/api/resource",
resource,
TbResource.class
).getBody();
}
public PageData<TbResourceInfo> getResources(PageLink pageLink) {
Map<String, String> params = new HashMap<>();
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/resource?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<TbResourceInfo>>() {},
params
).getBody();
}
public void deleteResource(TbResourceId resourceId) {
restTemplate.delete("/api/resource/{resourceId}", resourceId.getId().toString());
}
public ResponseEntity<Resource> downloadOtaPackage(OtaPackageId otaPackageId) {
Map<String, String> params = new HashMap<>();
params.put("otaPackageId", otaPackageId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/otaPackage/{otaPackageId}/download",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<>() {},
params
);
}
public OtaPackageInfo getOtaPackageInfoById(OtaPackageId otaPackageId) {
Map<String, String> params = new HashMap<>();
params.put("otaPackageId", otaPackageId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/otaPackage/info/{otaPackageId}",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<OtaPackageInfo>() {},
params
).getBody();
}
public OtaPackage getOtaPackageById(OtaPackageId otaPackageId) {
Map<String, String> params = new HashMap<>();
params.put("otaPackageId", otaPackageId.getId().toString());
return restTemplate.exchange(
baseURL + "/api/otaPackage/{otaPackageId}",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<OtaPackage>() {},
params
).getBody();
}
public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo) {
return restTemplate.postForEntity(baseURL + "/api/otaPackage", otaPackageInfo, OtaPackageInfo.class).getBody();
}
public OtaPackage saveOtaPackageData(OtaPackageId otaPackageId, String checkSum, ChecksumAlgorithm checksumAlgorithm, MultipartFile file) throws Exception {
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=file; filename=" + file.getName());
HttpEntity<ByteArrayResource> fileEntity = new HttpEntity<>(new ByteArrayResource(file.getBytes()), fileMap);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", fileEntity);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, header);
Map<String, String> params = new HashMap<>();
params.put("otaPackageId", otaPackageId.getId().toString());
params.put("checksumAlgorithm", checksumAlgorithm.name());
String url = "/api/otaPackage/{otaPackageId}?checksumAlgorithm={checksumAlgorithm}";
if(checkSum != null) {
url += "&checkSum={checkSum}";
}
return restTemplate.postForEntity(
baseURL + url, requestEntity, OtaPackage.class, params
).getBody();
}
public PageData<OtaPackageInfo> getOtaPackages(PageLink pageLink) {
Map<String, String> params = new HashMap<>();
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/otaPackages?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<OtaPackageInfo>>() {
},
params
).getBody();
}
public PageData<OtaPackageInfo> getOtaPackages(DeviceProfileId deviceProfileId,
OtaPackageType otaPackageType,
boolean hasData,
PageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("hasData", String.valueOf(hasData));
params.put("deviceProfileId", deviceProfileId.getId().toString());
params.put("type", otaPackageType.name());
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/otaPackages/{deviceProfileId}/{type}/{hasData}?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<OtaPackageInfo>>() {
},
params
).getBody();
}
public void deleteOtaPackage(OtaPackageId otaPackageId) {
restTemplate.delete(baseURL + "/api/otaPackage/{otaPackageId}", otaPackageId.getId().toString());
}
@Deprecated
public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {
Map<String, String> params = new HashMap<>();