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 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,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 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(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 110 public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
82 111 return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
83 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 120 public DeviceCredentials getCredentials(DeviceId id) {
87 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 125 return restTemplate;
92 126 }
93 127
  128 + public String getToken() {
  129 + return token;
  130 + }
  131 +
94 132 @Override
95 133 public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
96 134 HttpRequest wrapper = new HttpRequestWrapper(request);
97 135 wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
98 136 return execution.execute(wrapper, bytes);
99 137 }
100   -
101   -}
  138 +}
\ No newline at end of file
... ...