Commit 71dd7de6cd6fc537bbddda342f6f71da51dd6ef6

Authored by Dima Landiak
1 parent 1b525cfe

added some funcs to rest client

@@ -26,9 +26,16 @@ import org.springframework.http.client.ClientHttpResponse; @@ -26,9 +26,16 @@ import org.springframework.http.client.ClientHttpResponse;
26 import org.springframework.http.client.support.HttpRequestWrapper; 26 import org.springframework.http.client.support.HttpRequestWrapper;
27 import org.springframework.web.client.HttpClientErrorException; 27 import org.springframework.web.client.HttpClientErrorException;
28 import org.springframework.web.client.RestTemplate; 28 import org.springframework.web.client.RestTemplate;
  29 +import org.thingsboard.server.common.data.Customer;
29 import org.thingsboard.server.common.data.Device; 30 import org.thingsboard.server.common.data.Device;
  31 +import org.thingsboard.server.common.data.alarm.Alarm;
  32 +import org.thingsboard.server.common.data.alarm.AlarmSeverity;
  33 +import org.thingsboard.server.common.data.alarm.AlarmStatus;
  34 +import org.thingsboard.server.common.data.asset.Asset;
  35 +import org.thingsboard.server.common.data.id.AssetId;
30 import org.thingsboard.server.common.data.id.CustomerId; 36 import org.thingsboard.server.common.data.id.CustomerId;
31 import org.thingsboard.server.common.data.id.DeviceId; 37 import org.thingsboard.server.common.data.id.DeviceId;
  38 +import org.thingsboard.server.common.data.id.EntityId;
32 import org.thingsboard.server.common.data.security.DeviceCredentials; 39 import org.thingsboard.server.common.data.security.DeviceCredentials;
33 40
34 import java.io.IOException; 41 import java.io.IOException;
@@ -71,18 +78,45 @@ public class RestClient implements ClientHttpRequestInterceptor { @@ -71,18 +78,45 @@ public class RestClient implements ClientHttpRequestInterceptor {
71 } 78 }
72 } 79 }
73 80
74 - public Device createDevice(String name) { 81 + public Customer createCustomer(String title) {
  82 + Customer customer = new Customer();
  83 + customer.setTitle(title);
  84 + return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
  85 + }
  86 +
  87 + public Device createDevice(String name, String type) {
75 Device device = new Device(); 88 Device device = new Device();
76 device.setName(name); 89 device.setName(name);
  90 + device.setType(type);
77 return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody(); 91 return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
78 } 92 }
79 93
  94 + public Asset createAsset(String name, String type) {
  95 + Asset asset = new Asset();
  96 + asset.setName(name);
  97 + asset.setType(type);
  98 + return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
  99 + }
  100 +
  101 + public Alarm createAlarm(String type, AlarmSeverity severity, AlarmStatus status, EntityId originator) {
  102 + Alarm alarm = new Alarm();
  103 + alarm.setOriginator(originator);
  104 + alarm.setStatus(status);
  105 + alarm.setSeverity(severity);
  106 + alarm.setType(type);
  107 + return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
  108 + }
80 109
81 public Device assignDevice(CustomerId customerId, DeviceId deviceId) { 110 public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
82 return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class, 111 return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
83 customerId.toString(), deviceId.toString()).getBody(); 112 customerId.toString(), deviceId.toString()).getBody();
84 } 113 }
85 114
  115 + public Asset assignAsset(CustomerId customerId, AssetId assetId) {
  116 + return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", null, Asset.class,
  117 + customerId.toString(), assetId.toString()).getBody();
  118 + }
  119 +
86 public DeviceCredentials getCredentials(DeviceId id) { 120 public DeviceCredentials getCredentials(DeviceId id) {
87 return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody(); 121 return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
88 } 122 }
@@ -91,11 +125,14 @@ public class RestClient implements ClientHttpRequestInterceptor { @@ -91,11 +125,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
91 return restTemplate; 125 return restTemplate;
92 } 126 }
93 127
  128 + public String getToken() {
  129 + return token;
  130 + }
  131 +
94 @Override 132 @Override
95 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException { 133 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
96 HttpRequest wrapper = new HttpRequestWrapper(request); 134 HttpRequest wrapper = new HttpRequestWrapper(request);
97 wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token); 135 wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
98 return execution.execute(wrapper, bytes); 136 return execution.execute(wrapper, bytes);
99 } 137 }
100 -  
101 -} 138 +}