Browse Source

Merge branch 'feature/edge' of github.com:volodymyr-babak/thingsboard into feature/edge

pull/2436/head
Volodymyr Babak 6 years ago
parent
commit
6ffaca2541
  1. 10
      application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java
  2. 189
      rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java

10
application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java

@ -28,6 +28,7 @@ import com.google.gson.JsonObject;
import io.grpc.stub.StreamObserver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.DataConstants;
@ -878,6 +879,7 @@ public final class EdgeGrpcSession implements Closeable {
device.setType(deviceUpdateMsg.getType());
device.setLabel(deviceUpdateMsg.getLabel());
device = ctx.getDeviceService().saveDevice(device);
createDeviceCredentials(device);
createRelationFromEdge(device.getId());
ctx.getDeviceStateService().onDeviceAdded(device);
pushDeviceCreatedEventToRuleEngine(device);
@ -888,6 +890,14 @@ public final class EdgeGrpcSession implements Closeable {
return device;
}
private void createDeviceCredentials(Device device) {
DeviceCredentials deviceCredentials = new DeviceCredentials();
deviceCredentials.setDeviceId(device.getId());
deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
deviceCredentials.setCredentialsId(RandomStringUtils.randomAlphanumeric(20));
ctx.getDeviceCredentialsService().createDeviceCredentials(device.getTenantId(), deviceCredentials);
}
private void pushDeviceCreatedEventToRuleEngine(Device device) {
try {
ObjectNode entityNode = mapper.valueToTree(device);

189
rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java

@ -45,7 +45,8 @@ import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.UpdateMessage;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.id.AlarmId;
import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.edge.EdgeSearchQuery;
import org.thingsboard.server.common.data.alarm.AlarmInfo;
import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
@ -56,10 +57,12 @@ import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.audit.AuditLog;
import org.thingsboard.server.common.data.device.DeviceSearchQuery;
import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery;
import org.thingsboard.server.common.data.id.AlarmId;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.RuleChainId;
@ -1978,6 +1981,190 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
}
}
public Edge saveEdge(Edge edge) {
return restTemplate.postForEntity(baseURL + "/api/edge", edge, Edge.class).getBody();
}
public void deleteEdge(EdgeId edgeId) {
restTemplate.delete(baseURL + "/api/edge/{edgeId}", edgeId.getId());
}
public Optional<Edge> getEdgeById(EdgeId edgeId) {
try {
ResponseEntity<Edge> edge = restTemplate.getForEntity(baseURL + "/api/edge/{edgeId}", Edge.class, edgeId.getId());
return Optional.ofNullable(edge.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Edge> assignEdgeToCustomer(CustomerId customerId, EdgeId edgeId) {
try {
ResponseEntity<Edge> edge = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/edge/{edgeId}", null, Edge.class, customerId.getId(), edgeId.getId());
return Optional.ofNullable(edge.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Edge> unassignEdgeFromCustomer(EdgeId edgeId) {
try {
ResponseEntity<Edge> edge = restTemplate.exchange(baseURL + "/api/customer/edge/{edgeId}", HttpMethod.DELETE, HttpEntity.EMPTY, Edge.class, edgeId.getId());
return Optional.ofNullable(edge.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Device> assignDeviceToEdge(EdgeId edgeId, DeviceId deviceId) {
try {
ResponseEntity<Device> device = restTemplate.postForEntity(baseURL + "/api/edge/{edgeId}/device/{deviceId}", null, Device.class, edgeId.getId(), deviceId.getId());
return Optional.ofNullable(device.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Device> unassignDeviceFromEdge(DeviceId deviceId) {
try {
ResponseEntity<Device> device = restTemplate.exchange(baseURL + "/api/edge/device/{deviceId}", HttpMethod.DELETE, HttpEntity.EMPTY, Device.class, deviceId.getId());
return Optional.ofNullable(device.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public TextPageData<Device> getEdgeDevices(EdgeId edgeId, String deviceType, TextPageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("edgeId", edgeId.getId().toString());
params.put("type", deviceType);
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/edge/{edgeId}/devices?type={type}&" + getUrlParams(pageLink),
HttpMethod.GET, HttpEntity.EMPTY,
new ParameterizedTypeReference<TextPageData<Device>>() {
}, params).getBody();
}
public Optional<Asset> assignAssetToEdge(EdgeId edgeId, AssetId assetId) {
try {
ResponseEntity<Asset> asset = restTemplate.postForEntity(baseURL + "/api/edge/{edgeId}/asset/{assetId}", null, Asset.class, edgeId.getId(), assetId.getId());
return Optional.ofNullable(asset.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Asset> unassignAssetFromEdge(AssetId assetId) {
try {
ResponseEntity<Asset> asset = restTemplate.exchange(baseURL + "/api/edge/asset/{assetId}", HttpMethod.DELETE, HttpEntity.EMPTY, Asset.class, assetId.getId());
return Optional.ofNullable(asset.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public TextPageData<Asset> getEdgeAssets(EdgeId edgeId, String assetType, TextPageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("edgeId", edgeId.getId().toString());
params.put("type", assetType);
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/edge/{edgeId}/assets?type={type}&" + getUrlParams(pageLink),
HttpMethod.GET, HttpEntity.EMPTY,
new ParameterizedTypeReference<TextPageData<Asset>>() {
}, params).getBody();
}
public TextPageData<Edge> getTenantEdges(String type, TextPageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("type", type);
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/tenant/edges?type={type}&" + getUrlParams(pageLink),
HttpMethod.GET, HttpEntity.EMPTY,
new ParameterizedTypeReference<TextPageData<Edge>>() {
}, params).getBody();
}
public Optional<Edge> getTenantEdge(String edgeName) {
try {
ResponseEntity<Edge> edge = restTemplate.getForEntity(baseURL + "/api/tenant/edges?edgeName={edgeName}", Edge.class, edgeName);
return Optional.ofNullable(edge.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public TextPageData<Edge> getCustomerEdges(CustomerId customerId, String edgeType, TextPageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("customerId", customerId.getId().toString());
params.put("type", edgeType);
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/customer/{customerId}/edges?type={type}&" + getUrlParams(pageLink),
HttpMethod.GET, HttpEntity.EMPTY,
new ParameterizedTypeReference<TextPageData<Edge>>() {
}, params).getBody();
}
public List<Edge> getEdgesByIds(List<EdgeId> edgeIds) {
return restTemplate.exchange(baseURL + "/api/edges?edgeIds={edgeIds}",
HttpMethod.GET,
HttpEntity.EMPTY, new ParameterizedTypeReference<List<Edge>>() {
}, listIdsToString(edgeIds)).getBody();
}
public List<Edge> findByQuery(EdgeSearchQuery query) {
return restTemplate.exchange(
baseURL + "/api/edges",
HttpMethod.POST,
new HttpEntity<>(query),
new ParameterizedTypeReference<List<Edge>>() {
}).getBody();
}
public List<EntitySubtype> getEdgeTypes() {
return restTemplate.exchange(
baseURL + "/api/edge/types",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<List<EntitySubtype>>() {
}).getBody();
}
@Deprecated
public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {
Map<String, String> params = new HashMap<>();

Loading…
Cancel
Save