|
|
|
@ -18,11 +18,13 @@ package org.thingsboard.client.tools; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import org.springframework.http.HttpRequest; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
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.web.client.HttpClientErrorException; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
@ -32,6 +34,7 @@ import java.io.IOException; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author Andrew Shvayka |
|
|
|
@ -52,6 +55,21 @@ public class RestClient implements ClientHttpRequestInterceptor { |
|
|
|
restTemplate.setInterceptors(Collections.singletonList(this)); |
|
|
|
} |
|
|
|
|
|
|
|
public Optional<Device> findDevice(String name) { |
|
|
|
Map<String, String> params = new HashMap<String, String>(); |
|
|
|
params.put("deviceName", name); |
|
|
|
try { |
|
|
|
ResponseEntity<Device> deviceEntity = restTemplate.getForEntity(baseURL + "/api/tenant/devices?deviceName={deviceName}", Device.class, params); |
|
|
|
return Optional.of(deviceEntity.getBody()); |
|
|
|
} catch (HttpClientErrorException exception) { |
|
|
|
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { |
|
|
|
return Optional.empty(); |
|
|
|
} else { |
|
|
|
throw exception; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Device createDevice(String name) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName(name); |
|
|
|
|