Commit 6ffaca2541b3aa315475308f3f855dca94645b68

Authored by Volodymyr Babak
2 parents bf5f90d2 4462455c

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

@@ -28,6 +28,7 @@ import com.google.gson.JsonObject; @@ -28,6 +28,7 @@ import com.google.gson.JsonObject;
28 import io.grpc.stub.StreamObserver; 28 import io.grpc.stub.StreamObserver;
29 import lombok.Data; 29 import lombok.Data;
30 import lombok.extern.slf4j.Slf4j; 30 import lombok.extern.slf4j.Slf4j;
  31 +import org.apache.commons.lang.RandomStringUtils;
31 import org.checkerframework.checker.nullness.qual.Nullable; 32 import org.checkerframework.checker.nullness.qual.Nullable;
32 import org.thingsboard.server.common.data.Dashboard; 33 import org.thingsboard.server.common.data.Dashboard;
33 import org.thingsboard.server.common.data.DataConstants; 34 import org.thingsboard.server.common.data.DataConstants;
@@ -878,6 +879,7 @@ public final class EdgeGrpcSession implements Closeable { @@ -878,6 +879,7 @@ public final class EdgeGrpcSession implements Closeable {
878 device.setType(deviceUpdateMsg.getType()); 879 device.setType(deviceUpdateMsg.getType());
879 device.setLabel(deviceUpdateMsg.getLabel()); 880 device.setLabel(deviceUpdateMsg.getLabel());
880 device = ctx.getDeviceService().saveDevice(device); 881 device = ctx.getDeviceService().saveDevice(device);
  882 + createDeviceCredentials(device);
881 createRelationFromEdge(device.getId()); 883 createRelationFromEdge(device.getId());
882 ctx.getDeviceStateService().onDeviceAdded(device); 884 ctx.getDeviceStateService().onDeviceAdded(device);
883 pushDeviceCreatedEventToRuleEngine(device); 885 pushDeviceCreatedEventToRuleEngine(device);
@@ -888,6 +890,14 @@ public final class EdgeGrpcSession implements Closeable { @@ -888,6 +890,14 @@ public final class EdgeGrpcSession implements Closeable {
888 return device; 890 return device;
889 } 891 }
890 892
  893 + private void createDeviceCredentials(Device device) {
  894 + DeviceCredentials deviceCredentials = new DeviceCredentials();
  895 + deviceCredentials.setDeviceId(device.getId());
  896 + deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
  897 + deviceCredentials.setCredentialsId(RandomStringUtils.randomAlphanumeric(20));
  898 + ctx.getDeviceCredentialsService().createDeviceCredentials(device.getTenantId(), deviceCredentials);
  899 + }
  900 +
891 private void pushDeviceCreatedEventToRuleEngine(Device device) { 901 private void pushDeviceCreatedEventToRuleEngine(Device device) {
892 try { 902 try {
893 ObjectNode entityNode = mapper.valueToTree(device); 903 ObjectNode entityNode = mapper.valueToTree(device);
@@ -45,7 +45,8 @@ import org.thingsboard.server.common.data.Tenant; @@ -45,7 +45,8 @@ import org.thingsboard.server.common.data.Tenant;
45 import org.thingsboard.server.common.data.UpdateMessage; 45 import org.thingsboard.server.common.data.UpdateMessage;
46 import org.thingsboard.server.common.data.User; 46 import org.thingsboard.server.common.data.User;
47 import org.thingsboard.server.common.data.alarm.Alarm; 47 import org.thingsboard.server.common.data.alarm.Alarm;
48 -import org.thingsboard.server.common.data.id.AlarmId; 48 +import org.thingsboard.server.common.data.edge.Edge;
  49 +import org.thingsboard.server.common.data.edge.EdgeSearchQuery;
49 import org.thingsboard.server.common.data.alarm.AlarmInfo; 50 import org.thingsboard.server.common.data.alarm.AlarmInfo;
50 import org.thingsboard.server.common.data.alarm.AlarmSearchStatus; 51 import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
51 import org.thingsboard.server.common.data.alarm.AlarmSeverity; 52 import org.thingsboard.server.common.data.alarm.AlarmSeverity;
@@ -56,10 +57,12 @@ import org.thingsboard.server.common.data.audit.ActionType; @@ -56,10 +57,12 @@ import org.thingsboard.server.common.data.audit.ActionType;
56 import org.thingsboard.server.common.data.audit.AuditLog; 57 import org.thingsboard.server.common.data.audit.AuditLog;
57 import org.thingsboard.server.common.data.device.DeviceSearchQuery; 58 import org.thingsboard.server.common.data.device.DeviceSearchQuery;
58 import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; 59 import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery;
  60 +import org.thingsboard.server.common.data.id.AlarmId;
59 import org.thingsboard.server.common.data.id.AssetId; 61 import org.thingsboard.server.common.data.id.AssetId;
60 import org.thingsboard.server.common.data.id.CustomerId; 62 import org.thingsboard.server.common.data.id.CustomerId;
61 import org.thingsboard.server.common.data.id.DashboardId; 63 import org.thingsboard.server.common.data.id.DashboardId;
62 import org.thingsboard.server.common.data.id.DeviceId; 64 import org.thingsboard.server.common.data.id.DeviceId;
  65 +import org.thingsboard.server.common.data.id.EdgeId;
63 import org.thingsboard.server.common.data.id.EntityId; 66 import org.thingsboard.server.common.data.id.EntityId;
64 import org.thingsboard.server.common.data.id.EntityViewId; 67 import org.thingsboard.server.common.data.id.EntityViewId;
65 import org.thingsboard.server.common.data.id.RuleChainId; 68 import org.thingsboard.server.common.data.id.RuleChainId;
@@ -1978,6 +1981,190 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { @@ -1978,6 +1981,190 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
1978 } 1981 }
1979 } 1982 }
1980 1983
  1984 + public Edge saveEdge(Edge edge) {
  1985 + return restTemplate.postForEntity(baseURL + "/api/edge", edge, Edge.class).getBody();
  1986 + }
  1987 +
  1988 + public void deleteEdge(EdgeId edgeId) {
  1989 + restTemplate.delete(baseURL + "/api/edge/{edgeId}", edgeId.getId());
  1990 + }
  1991 +
  1992 + public Optional<Edge> getEdgeById(EdgeId edgeId) {
  1993 + try {
  1994 + ResponseEntity<Edge> edge = restTemplate.getForEntity(baseURL + "/api/edge/{edgeId}", Edge.class, edgeId.getId());
  1995 + return Optional.ofNullable(edge.getBody());
  1996 + } catch (HttpClientErrorException exception) {
  1997 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  1998 + return Optional.empty();
  1999 + } else {
  2000 + throw exception;
  2001 + }
  2002 + }
  2003 + }
  2004 +
  2005 + public Optional<Edge> assignEdgeToCustomer(CustomerId customerId, EdgeId edgeId) {
  2006 + try {
  2007 + ResponseEntity<Edge> edge = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/edge/{edgeId}", null, Edge.class, customerId.getId(), edgeId.getId());
  2008 + return Optional.ofNullable(edge.getBody());
  2009 + } catch (HttpClientErrorException exception) {
  2010 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2011 + return Optional.empty();
  2012 + } else {
  2013 + throw exception;
  2014 + }
  2015 + }
  2016 + }
  2017 +
  2018 + public Optional<Edge> unassignEdgeFromCustomer(EdgeId edgeId) {
  2019 + try {
  2020 + ResponseEntity<Edge> edge = restTemplate.exchange(baseURL + "/api/customer/edge/{edgeId}", HttpMethod.DELETE, HttpEntity.EMPTY, Edge.class, edgeId.getId());
  2021 + return Optional.ofNullable(edge.getBody());
  2022 + } catch (HttpClientErrorException exception) {
  2023 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2024 + return Optional.empty();
  2025 + } else {
  2026 + throw exception;
  2027 + }
  2028 + }
  2029 + }
  2030 +
  2031 + public Optional<Device> assignDeviceToEdge(EdgeId edgeId, DeviceId deviceId) {
  2032 + try {
  2033 + ResponseEntity<Device> device = restTemplate.postForEntity(baseURL + "/api/edge/{edgeId}/device/{deviceId}", null, Device.class, edgeId.getId(), deviceId.getId());
  2034 + return Optional.ofNullable(device.getBody());
  2035 + } catch (HttpClientErrorException exception) {
  2036 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2037 + return Optional.empty();
  2038 + } else {
  2039 + throw exception;
  2040 + }
  2041 + }
  2042 + }
  2043 +
  2044 + public Optional<Device> unassignDeviceFromEdge(DeviceId deviceId) {
  2045 + try {
  2046 + ResponseEntity<Device> device = restTemplate.exchange(baseURL + "/api/edge/device/{deviceId}", HttpMethod.DELETE, HttpEntity.EMPTY, Device.class, deviceId.getId());
  2047 + return Optional.ofNullable(device.getBody());
  2048 + } catch (HttpClientErrorException exception) {
  2049 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2050 + return Optional.empty();
  2051 + } else {
  2052 + throw exception;
  2053 + }
  2054 + }
  2055 + }
  2056 +
  2057 + public TextPageData<Device> getEdgeDevices(EdgeId edgeId, String deviceType, TextPageLink pageLink) {
  2058 + Map<String, String> params = new HashMap<>();
  2059 + params.put("edgeId", edgeId.getId().toString());
  2060 + params.put("type", deviceType);
  2061 + addPageLinkToParam(params, pageLink);
  2062 + return restTemplate.exchange(
  2063 + baseURL + "/api/edge/{edgeId}/devices?type={type}&" + getUrlParams(pageLink),
  2064 + HttpMethod.GET, HttpEntity.EMPTY,
  2065 + new ParameterizedTypeReference<TextPageData<Device>>() {
  2066 + }, params).getBody();
  2067 + }
  2068 +
  2069 + public Optional<Asset> assignAssetToEdge(EdgeId edgeId, AssetId assetId) {
  2070 + try {
  2071 + ResponseEntity<Asset> asset = restTemplate.postForEntity(baseURL + "/api/edge/{edgeId}/asset/{assetId}", null, Asset.class, edgeId.getId(), assetId.getId());
  2072 + return Optional.ofNullable(asset.getBody());
  2073 + } catch (HttpClientErrorException exception) {
  2074 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2075 + return Optional.empty();
  2076 + } else {
  2077 + throw exception;
  2078 + }
  2079 + }
  2080 + }
  2081 +
  2082 + public Optional<Asset> unassignAssetFromEdge(AssetId assetId) {
  2083 + try {
  2084 + ResponseEntity<Asset> asset = restTemplate.exchange(baseURL + "/api/edge/asset/{assetId}", HttpMethod.DELETE, HttpEntity.EMPTY, Asset.class, assetId.getId());
  2085 + return Optional.ofNullable(asset.getBody());
  2086 + } catch (HttpClientErrorException exception) {
  2087 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2088 + return Optional.empty();
  2089 + } else {
  2090 + throw exception;
  2091 + }
  2092 + }
  2093 + }
  2094 +
  2095 + public TextPageData<Asset> getEdgeAssets(EdgeId edgeId, String assetType, TextPageLink pageLink) {
  2096 + Map<String, String> params = new HashMap<>();
  2097 + params.put("edgeId", edgeId.getId().toString());
  2098 + params.put("type", assetType);
  2099 + addPageLinkToParam(params, pageLink);
  2100 + return restTemplate.exchange(
  2101 + baseURL + "/api/edge/{edgeId}/assets?type={type}&" + getUrlParams(pageLink),
  2102 + HttpMethod.GET, HttpEntity.EMPTY,
  2103 + new ParameterizedTypeReference<TextPageData<Asset>>() {
  2104 + }, params).getBody();
  2105 + }
  2106 +
  2107 + public TextPageData<Edge> getTenantEdges(String type, TextPageLink pageLink) {
  2108 + Map<String, String> params = new HashMap<>();
  2109 + params.put("type", type);
  2110 + addPageLinkToParam(params, pageLink);
  2111 + return restTemplate.exchange(
  2112 + baseURL + "/api/tenant/edges?type={type}&" + getUrlParams(pageLink),
  2113 + HttpMethod.GET, HttpEntity.EMPTY,
  2114 + new ParameterizedTypeReference<TextPageData<Edge>>() {
  2115 + }, params).getBody();
  2116 + }
  2117 +
  2118 + public Optional<Edge> getTenantEdge(String edgeName) {
  2119 + try {
  2120 + ResponseEntity<Edge> edge = restTemplate.getForEntity(baseURL + "/api/tenant/edges?edgeName={edgeName}", Edge.class, edgeName);
  2121 + return Optional.ofNullable(edge.getBody());
  2122 + } catch (HttpClientErrorException exception) {
  2123 + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
  2124 + return Optional.empty();
  2125 + } else {
  2126 + throw exception;
  2127 + }
  2128 + }
  2129 + }
  2130 +
  2131 + public TextPageData<Edge> getCustomerEdges(CustomerId customerId, String edgeType, TextPageLink pageLink) {
  2132 + Map<String, String> params = new HashMap<>();
  2133 + params.put("customerId", customerId.getId().toString());
  2134 + params.put("type", edgeType);
  2135 + addPageLinkToParam(params, pageLink);
  2136 + return restTemplate.exchange(
  2137 + baseURL + "/api/customer/{customerId}/edges?type={type}&" + getUrlParams(pageLink),
  2138 + HttpMethod.GET, HttpEntity.EMPTY,
  2139 + new ParameterizedTypeReference<TextPageData<Edge>>() {
  2140 + }, params).getBody();
  2141 + }
  2142 +
  2143 + public List<Edge> getEdgesByIds(List<EdgeId> edgeIds) {
  2144 + return restTemplate.exchange(baseURL + "/api/edges?edgeIds={edgeIds}",
  2145 + HttpMethod.GET,
  2146 + HttpEntity.EMPTY, new ParameterizedTypeReference<List<Edge>>() {
  2147 + }, listIdsToString(edgeIds)).getBody();
  2148 + }
  2149 +
  2150 + public List<Edge> findByQuery(EdgeSearchQuery query) {
  2151 + return restTemplate.exchange(
  2152 + baseURL + "/api/edges",
  2153 + HttpMethod.POST,
  2154 + new HttpEntity<>(query),
  2155 + new ParameterizedTypeReference<List<Edge>>() {
  2156 + }).getBody();
  2157 + }
  2158 +
  2159 + public List<EntitySubtype> getEdgeTypes() {
  2160 + return restTemplate.exchange(
  2161 + baseURL + "/api/edge/types",
  2162 + HttpMethod.GET,
  2163 + HttpEntity.EMPTY,
  2164 + new ParameterizedTypeReference<List<EntitySubtype>>() {
  2165 + }).getBody();
  2166 + }
  2167 +
1981 @Deprecated 2168 @Deprecated
1982 public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) { 2169 public Optional<JsonNode> getAttributes(String accessToken, String clientKeys, String sharedKeys) {
1983 Map<String, String> params = new HashMap<>(); 2170 Map<String, String> params = new HashMap<>();