Commit f9713c4fce9f67a8ef0971970e480ad7347536b4
Committed by
GitHub
1 parent
b978f0f7
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
Showing
1 changed file
with
201 additions
and
2 deletions
@@ -19,18 +19,25 @@ import com.fasterxml.jackson.databind.JsonNode; | @@ -19,18 +19,25 @@ import com.fasterxml.jackson.databind.JsonNode; | ||
19 | import com.fasterxml.jackson.databind.ObjectMapper; | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
20 | import com.fasterxml.jackson.databind.node.ObjectNode; | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
21 | import org.springframework.core.ParameterizedTypeReference; | 21 | import org.springframework.core.ParameterizedTypeReference; |
22 | +import org.springframework.core.io.ByteArrayResource; | ||
23 | +import org.springframework.core.io.Resource; | ||
22 | import org.springframework.http.HttpEntity; | 24 | import org.springframework.http.HttpEntity; |
25 | +import org.springframework.http.HttpHeaders; | ||
23 | import org.springframework.http.HttpMethod; | 26 | import org.springframework.http.HttpMethod; |
24 | import org.springframework.http.HttpRequest; | 27 | import org.springframework.http.HttpRequest; |
25 | import org.springframework.http.HttpStatus; | 28 | import org.springframework.http.HttpStatus; |
29 | +import org.springframework.http.MediaType; | ||
26 | import org.springframework.http.ResponseEntity; | 30 | import org.springframework.http.ResponseEntity; |
27 | import org.springframework.http.client.ClientHttpRequestExecution; | 31 | import org.springframework.http.client.ClientHttpRequestExecution; |
28 | import org.springframework.http.client.ClientHttpRequestInterceptor; | 32 | import org.springframework.http.client.ClientHttpRequestInterceptor; |
29 | import org.springframework.http.client.ClientHttpResponse; | 33 | import org.springframework.http.client.ClientHttpResponse; |
30 | import org.springframework.http.client.support.HttpRequestWrapper; | 34 | import org.springframework.http.client.support.HttpRequestWrapper; |
35 | +import org.springframework.util.LinkedMultiValueMap; | ||
36 | +import org.springframework.util.MultiValueMap; | ||
31 | import org.springframework.util.StringUtils; | 37 | import org.springframework.util.StringUtils; |
32 | import org.springframework.web.client.HttpClientErrorException; | 38 | import org.springframework.web.client.HttpClientErrorException; |
33 | import org.springframework.web.client.RestTemplate; | 39 | import org.springframework.web.client.RestTemplate; |
40 | +import org.springframework.web.multipart.MultipartFile; | ||
34 | import org.thingsboard.common.util.ThingsBoardExecutors; | 41 | import org.thingsboard.common.util.ThingsBoardExecutors; |
35 | import org.thingsboard.rest.client.utils.RestJsonConverter; | 42 | import org.thingsboard.rest.client.utils.RestJsonConverter; |
36 | import org.thingsboard.server.common.data.AdminSettings; | 43 | import org.thingsboard.server.common.data.AdminSettings; |
@@ -48,6 +55,10 @@ import org.thingsboard.server.common.data.EntitySubtype; | @@ -48,6 +55,10 @@ import org.thingsboard.server.common.data.EntitySubtype; | ||
48 | import org.thingsboard.server.common.data.EntityView; | 55 | import org.thingsboard.server.common.data.EntityView; |
49 | import org.thingsboard.server.common.data.EntityViewInfo; | 56 | import org.thingsboard.server.common.data.EntityViewInfo; |
50 | import org.thingsboard.server.common.data.Event; | 57 | import org.thingsboard.server.common.data.Event; |
58 | +import org.thingsboard.server.common.data.OtaPackage; | ||
59 | +import org.thingsboard.server.common.data.OtaPackageInfo; | ||
60 | +import org.thingsboard.server.common.data.TbResource; | ||
61 | +import org.thingsboard.server.common.data.TbResourceInfo; | ||
51 | import org.thingsboard.server.common.data.Tenant; | 62 | import org.thingsboard.server.common.data.Tenant; |
52 | import org.thingsboard.server.common.data.TenantInfo; | 63 | import org.thingsboard.server.common.data.TenantInfo; |
53 | import org.thingsboard.server.common.data.TenantProfile; | 64 | import org.thingsboard.server.common.data.TenantProfile; |
@@ -78,8 +89,10 @@ import org.thingsboard.server.common.data.id.EdgeId; | @@ -78,8 +89,10 @@ import org.thingsboard.server.common.data.id.EdgeId; | ||
78 | import org.thingsboard.server.common.data.id.EntityId; | 89 | import org.thingsboard.server.common.data.id.EntityId; |
79 | import org.thingsboard.server.common.data.id.EntityViewId; | 90 | import org.thingsboard.server.common.data.id.EntityViewId; |
80 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | 91 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; |
92 | +import org.thingsboard.server.common.data.id.OtaPackageId; | ||
81 | import org.thingsboard.server.common.data.id.RuleChainId; | 93 | import org.thingsboard.server.common.data.id.RuleChainId; |
82 | import org.thingsboard.server.common.data.id.RuleNodeId; | 94 | import org.thingsboard.server.common.data.id.RuleNodeId; |
95 | +import org.thingsboard.server.common.data.id.TbResourceId; | ||
83 | import org.thingsboard.server.common.data.id.TenantId; | 96 | import org.thingsboard.server.common.data.id.TenantId; |
84 | import org.thingsboard.server.common.data.id.TenantProfileId; | 97 | import org.thingsboard.server.common.data.id.TenantProfileId; |
85 | import org.thingsboard.server.common.data.id.UserId; | 98 | import org.thingsboard.server.common.data.id.UserId; |
@@ -91,6 +104,8 @@ import org.thingsboard.server.common.data.kv.TsKvEntry; | @@ -91,6 +104,8 @@ import org.thingsboard.server.common.data.kv.TsKvEntry; | ||
91 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; | 104 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; |
92 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | 105 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; |
93 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | 106 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; |
107 | +import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; | ||
108 | +import org.thingsboard.server.common.data.ota.OtaPackageType; | ||
94 | import org.thingsboard.server.common.data.page.PageData; | 109 | import org.thingsboard.server.common.data.page.PageData; |
95 | import org.thingsboard.server.common.data.page.PageLink; | 110 | import org.thingsboard.server.common.data.page.PageLink; |
96 | import org.thingsboard.server.common.data.page.SortOrder; | 111 | import org.thingsboard.server.common.data.page.SortOrder; |
@@ -127,9 +142,9 @@ import java.util.HashMap; | @@ -127,9 +142,9 @@ import java.util.HashMap; | ||
127 | import java.util.List; | 142 | import java.util.List; |
128 | import java.util.Map; | 143 | import java.util.Map; |
129 | import java.util.Optional; | 144 | import java.util.Optional; |
145 | +import java.util.UUID; | ||
130 | import java.util.concurrent.ConcurrentHashMap; | 146 | import java.util.concurrent.ConcurrentHashMap; |
131 | import java.util.concurrent.ExecutorService; | 147 | import java.util.concurrent.ExecutorService; |
132 | -import java.util.concurrent.Executors; | ||
133 | import java.util.concurrent.Future; | 148 | import java.util.concurrent.Future; |
134 | import java.util.stream.Collectors; | 149 | import java.util.stream.Collectors; |
135 | 150 | ||
@@ -147,7 +162,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | @@ -147,7 +162,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | ||
147 | private final ObjectMapper objectMapper = new ObjectMapper(); | 162 | private final ObjectMapper objectMapper = new ObjectMapper(); |
148 | private ExecutorService service = ThingsBoardExecutors.newWorkStealingPool(10, getClass()); | 163 | private ExecutorService service = ThingsBoardExecutors.newWorkStealingPool(10, getClass()); |
149 | 164 | ||
150 | - | ||
151 | protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; | 165 | protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; |
152 | 166 | ||
153 | public RestClient(String baseURL) { | 167 | public RestClient(String baseURL) { |
@@ -1238,6 +1252,21 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | @@ -1238,6 +1252,21 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | ||
1238 | HttpEntity.EMPTY, Device.class, tenantId, deviceId).getBody(); | 1252 | HttpEntity.EMPTY, Device.class, tenantId, deviceId).getBody(); |
1239 | } | 1253 | } |
1240 | 1254 | ||
1255 | + public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(OtaPackageType otaPackageType, DeviceProfileId deviceProfileId) { | ||
1256 | + Map<String, String> params = new HashMap<>(); | ||
1257 | + params.put("otaPackageType", otaPackageType.name()); | ||
1258 | + params.put("deviceProfileId", deviceProfileId.getId().toString()); | ||
1259 | + | ||
1260 | + return restTemplate.exchange( | ||
1261 | + baseURL + "/api/devices/count/{otaPackageType}?deviceProfileId={deviceProfileId}", | ||
1262 | + HttpMethod.GET, | ||
1263 | + HttpEntity.EMPTY, | ||
1264 | + new ParameterizedTypeReference<Long>() { | ||
1265 | + }, | ||
1266 | + params | ||
1267 | + ).getBody(); | ||
1268 | + } | ||
1269 | + | ||
1241 | @Deprecated | 1270 | @Deprecated |
1242 | public Device createDevice(String name, String type) { | 1271 | public Device createDevice(String name, String type) { |
1243 | Device device = new Device(); | 1272 | Device device = new Device(); |
@@ -2830,6 +2859,176 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | @@ -2830,6 +2859,176 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { | ||
2830 | restTemplate.postForEntity(baseURL + "/api/edge/sync/{edgeId}", null, EdgeId.class, params); | 2859 | restTemplate.postForEntity(baseURL + "/api/edge/sync/{edgeId}", null, EdgeId.class, params); |
2831 | } | 2860 | } |
2832 | 2861 | ||
2862 | + public ResponseEntity<Resource> downloadResource(TbResourceId resourceId) { | ||
2863 | + Map<String, String> params = new HashMap<>(); | ||
2864 | + params.put("resourceId", resourceId.getId().toString()); | ||
2865 | + | ||
2866 | + return restTemplate.exchange( | ||
2867 | + baseURL + "/api/resource/{resourceId}/download", | ||
2868 | + HttpMethod.GET, | ||
2869 | + HttpEntity.EMPTY, | ||
2870 | + new ParameterizedTypeReference<>() {}, | ||
2871 | + params | ||
2872 | + ); | ||
2873 | + } | ||
2874 | + | ||
2875 | + public TbResourceInfo getResourceInfoById(TbResourceId resourceId) { | ||
2876 | + Map<String, String> params = new HashMap<>(); | ||
2877 | + params.put("resourceId", resourceId.getId().toString()); | ||
2878 | + | ||
2879 | + return restTemplate.exchange( | ||
2880 | + baseURL + "/api/resource/info/{resourceId}", | ||
2881 | + HttpMethod.GET, | ||
2882 | + HttpEntity.EMPTY, | ||
2883 | + new ParameterizedTypeReference<TbResourceInfo>() {}, | ||
2884 | + params | ||
2885 | + ).getBody(); | ||
2886 | + } | ||
2887 | + | ||
2888 | + public TbResource getResourceId(TbResourceId resourceId) { | ||
2889 | + Map<String, String> params = new HashMap<>(); | ||
2890 | + params.put("resourceId", resourceId.getId().toString()); | ||
2891 | + | ||
2892 | + return restTemplate.exchange( | ||
2893 | + baseURL + "/api/resource/{resourceId}", | ||
2894 | + HttpMethod.GET, | ||
2895 | + HttpEntity.EMPTY, | ||
2896 | + new ParameterizedTypeReference<TbResource>() {}, | ||
2897 | + params | ||
2898 | + ).getBody(); | ||
2899 | + } | ||
2900 | + | ||
2901 | + public TbResource saveResource(TbResource resource) { | ||
2902 | + return restTemplate.postForEntity( | ||
2903 | + baseURL + "/api/resource", | ||
2904 | + resource, | ||
2905 | + TbResource.class | ||
2906 | + ).getBody(); | ||
2907 | + } | ||
2908 | + | ||
2909 | + public PageData<TbResourceInfo> getResources(PageLink pageLink) { | ||
2910 | + Map<String, String> params = new HashMap<>(); | ||
2911 | + addPageLinkToParam(params, pageLink); | ||
2912 | + return restTemplate.exchange( | ||
2913 | + baseURL + "/api/resource?" + getUrlParams(pageLink), | ||
2914 | + HttpMethod.GET, | ||
2915 | + HttpEntity.EMPTY, | ||
2916 | + new ParameterizedTypeReference<PageData<TbResourceInfo>>() {}, | ||
2917 | + params | ||
2918 | + ).getBody(); | ||
2919 | + } | ||
2920 | + | ||
2921 | + public void deleteResource(TbResourceId resourceId) { | ||
2922 | + restTemplate.delete("/api/resource/{resourceId}", resourceId.getId().toString()); | ||
2923 | + } | ||
2924 | + | ||
2925 | + public ResponseEntity<Resource> downloadOtaPackage(OtaPackageId otaPackageId) { | ||
2926 | + Map<String, String> params = new HashMap<>(); | ||
2927 | + params.put("otaPackageId", otaPackageId.getId().toString()); | ||
2928 | + | ||
2929 | + return restTemplate.exchange( | ||
2930 | + baseURL + "/api/otaPackage/{otaPackageId}/download", | ||
2931 | + HttpMethod.GET, | ||
2932 | + HttpEntity.EMPTY, | ||
2933 | + new ParameterizedTypeReference<>() {}, | ||
2934 | + params | ||
2935 | + ); | ||
2936 | + } | ||
2937 | + | ||
2938 | + public OtaPackageInfo getOtaPackageInfoById(OtaPackageId otaPackageId) { | ||
2939 | + Map<String, String> params = new HashMap<>(); | ||
2940 | + params.put("otaPackageId", otaPackageId.getId().toString()); | ||
2941 | + | ||
2942 | + return restTemplate.exchange( | ||
2943 | + baseURL + "/api/otaPackage/info/{otaPackageId}", | ||
2944 | + HttpMethod.GET, | ||
2945 | + HttpEntity.EMPTY, | ||
2946 | + new ParameterizedTypeReference<OtaPackageInfo>() {}, | ||
2947 | + params | ||
2948 | + ).getBody(); | ||
2949 | + } | ||
2950 | + | ||
2951 | + public OtaPackage getOtaPackageById(OtaPackageId otaPackageId) { | ||
2952 | + Map<String, String> params = new HashMap<>(); | ||
2953 | + params.put("otaPackageId", otaPackageId.getId().toString()); | ||
2954 | + | ||
2955 | + return restTemplate.exchange( | ||
2956 | + baseURL + "/api/otaPackage/{otaPackageId}", | ||
2957 | + HttpMethod.GET, | ||
2958 | + HttpEntity.EMPTY, | ||
2959 | + new ParameterizedTypeReference<OtaPackage>() {}, | ||
2960 | + params | ||
2961 | + ).getBody(); | ||
2962 | + } | ||
2963 | + | ||
2964 | + public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo) { | ||
2965 | + return restTemplate.postForEntity(baseURL + "/api/otaPackage", otaPackageInfo, OtaPackageInfo.class).getBody(); | ||
2966 | + } | ||
2967 | + | ||
2968 | + public OtaPackage saveOtaPackageData(OtaPackageId otaPackageId, String checkSum, ChecksumAlgorithm checksumAlgorithm, MultipartFile file) throws Exception { | ||
2969 | + HttpHeaders header = new HttpHeaders(); | ||
2970 | + header.setContentType(MediaType.MULTIPART_FORM_DATA); | ||
2971 | + | ||
2972 | + MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>(); | ||
2973 | + fileMap.add(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=file; filename=" + file.getName()); | ||
2974 | + HttpEntity<ByteArrayResource> fileEntity = new HttpEntity<>(new ByteArrayResource(file.getBytes()), fileMap); | ||
2975 | + | ||
2976 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); | ||
2977 | + body.add("file", fileEntity); | ||
2978 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, header); | ||
2979 | + | ||
2980 | + Map<String, String> params = new HashMap<>(); | ||
2981 | + params.put("otaPackageId", otaPackageId.getId().toString()); | ||
2982 | + params.put("checksumAlgorithm", checksumAlgorithm.name()); | ||
2983 | + String url = "/api/otaPackage/{otaPackageId}?checksumAlgorithm={checksumAlgorithm}"; | ||
2984 | + | ||
2985 | + if(checkSum != null) { | ||
2986 | + url += "&checkSum={checkSum}"; | ||
2987 | + } | ||
2988 | + | ||
2989 | + return restTemplate.postForEntity( | ||
2990 | + baseURL + url, requestEntity, OtaPackage.class, params | ||
2991 | + ).getBody(); | ||
2992 | + } | ||
2993 | + | ||
2994 | + public PageData<OtaPackageInfo> getOtaPackages(PageLink pageLink) { | ||
2995 | + Map<String, String> params = new HashMap<>(); | ||
2996 | + addPageLinkToParam(params, pageLink); | ||
2997 | + | ||
2998 | + return restTemplate.exchange( | ||
2999 | + baseURL + "/api/otaPackages?" + getUrlParams(pageLink), | ||
3000 | + HttpMethod.GET, | ||
3001 | + HttpEntity.EMPTY, | ||
3002 | + new ParameterizedTypeReference<PageData<OtaPackageInfo>>() { | ||
3003 | + }, | ||
3004 | + params | ||
3005 | + ).getBody(); | ||
3006 | + } | ||
3007 | + | ||
3008 | + public PageData<OtaPackageInfo> getOtaPackages(DeviceProfileId deviceProfileId, | ||
3009 | + OtaPackageType otaPackageType, | ||
3010 | + boolean hasData, | ||
3011 | + PageLink pageLink) { | ||
3012 | + Map<String, String> params = new HashMap<>(); | ||
3013 | + params.put("hasData", String.valueOf(hasData)); | ||
3014 | + params.put("deviceProfileId", deviceProfileId.getId().toString()); | ||
3015 | + params.put("type", otaPackageType.name()); | ||
3016 | + addPageLinkToParam(params, pageLink); | ||
3017 | + | ||
3018 | + return restTemplate.exchange( | ||
3019 | + baseURL + "/api/otaPackages/{deviceProfileId}/{type}/{hasData}?" + getUrlParams(pageLink), | ||
3020 | + HttpMethod.GET, | ||
3021 | + HttpEntity.EMPTY, | ||
3022 | + new ParameterizedTypeReference<PageData<OtaPackageInfo>>() { | ||
3023 | + }, | ||
3024 | + params | ||
3025 | + ).getBody(); | ||
3026 | + } | ||
3027 | + | ||
3028 | + public void deleteOtaPackage(OtaPackageId otaPackageId) { | ||
3029 | + restTemplate.delete(baseURL + "/api/otaPackage/{otaPackageId}", otaPackageId.getId().toString()); | ||
3030 | + } | ||
3031 | + | ||
2833 | @Deprecated | 3032 | @Deprecated |
2834 | public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) { | 3033 | public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) { |
2835 | Map<String, String> params = new HashMap<>(); | 3034 | Map<String, String> params = new HashMap<>(); |