Skip to content

Commit 71dd7de

Browse files
added some funcs to rest client
1 parent 1b525cf commit 71dd7de

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

tools/src/main/java/org/thingsboard/client/tools/RestClient.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@
2626
import org.springframework.http.client.support.HttpRequestWrapper;
2727
import org.springframework.web.client.HttpClientErrorException;
2828
import org.springframework.web.client.RestTemplate;
29+
import org.thingsboard.server.common.data.Customer;
2930
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;
3036
import org.thingsboard.server.common.data.id.CustomerId;
3137
import org.thingsboard.server.common.data.id.DeviceId;
38+
import org.thingsboard.server.common.data.id.EntityId;
3239
import org.thingsboard.server.common.data.security.DeviceCredentials;
3340

3441
import java.io.IOException;
@@ -71,18 +78,45 @@ public Optional<Device> findDevice(String name) {
7178
}
7279
}
7380

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) {
7588
Device device = new Device();
7689
device.setName(name);
90+
device.setType(type);
7791
return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
7892
}
7993

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+
}
80109

81110
public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
82111
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
83112
customerId.toString(), deviceId.toString()).getBody();
84113
}
85114

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+
86120
public DeviceCredentials getCredentials(DeviceId id) {
87121
return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
88122
}
@@ -91,11 +125,14 @@ public RestTemplate getRestTemplate() {
91125
return restTemplate;
92126
}
93127

128+
public String getToken() {
129+
return token;
130+
}
131+
94132
@Override
95133
public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
96134
HttpRequest wrapper = new HttpRequestWrapper(request);
97135
wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
98136
return execution.execute(wrapper, bytes);
99137
}
100-
101-
}
138+
}

0 commit comments

Comments
 (0)