Commit 23f81fe450f828f20197b4afe7d17c13470c473e

Authored by Andrew Shvayka
Committed by GitHub
2 parents 57c0fff3 52edc152

Merge pull request #480 from dmytro-landiak/master

added some funcs to rest client
... ... @@ -26,9 +26,16 @@ import org.springframework.http.client.ClientHttpResponse;
26 26 import org.springframework.http.client.support.HttpRequestWrapper;
27 27 import org.springframework.web.client.HttpClientErrorException;
28 28 import org.springframework.web.client.RestTemplate;
  29 +import org.thingsboard.server.common.data.Customer;
29 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 36 import org.thingsboard.server.common.data.id.CustomerId;
31 37 import org.thingsboard.server.common.data.id.DeviceId;
  38 +import org.thingsboard.server.common.data.id.EntityId;
32 39 import org.thingsboard.server.common.data.security.DeviceCredentials;
33 40
34 41 import java.io.IOException;
... ... @@ -71,18 +78,40 @@ 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 88 Device device = new Device();
76 89 device.setName(name);
  90 + device.setType(type);
77 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(Alarm alarm) {
  102 + return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
  103 + }
80 104
81 105 public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
82 106 return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
83 107 customerId.toString(), deviceId.toString()).getBody();
84 108 }
85 109
  110 + public Asset assignAsset(CustomerId customerId, AssetId assetId) {
  111 + return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", null, Asset.class,
  112 + customerId.toString(), assetId.toString()).getBody();
  113 + }
  114 +
86 115 public DeviceCredentials getCredentials(DeviceId id) {
87 116 return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
88 117 }
... ... @@ -91,11 +120,14 @@ public class RestClient implements ClientHttpRequestInterceptor {
91 120 return restTemplate;
92 121 }
93 122
  123 + public String getToken() {
  124 + return token;
  125 + }
  126 +
94 127 @Override
95 128 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
96 129 HttpRequest wrapper = new HttpRequestWrapper(request);
97 130 wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
98 131 return execution.execute(wrapper, bytes);
99 132 }
100   -
101   -}
  133 +}
\ No newline at end of file
... ...