Commit b8b55a98670a2d5d9301029e751e1e346a640de9
Merge remote-tracking branch 'origin/feature/edge' into feature/edge
Showing
6 changed files
with
92 additions
and
10 deletions
... | ... | @@ -66,6 +66,7 @@ import org.thingsboard.server.dao.user.UserService; |
66 | 66 | import org.thingsboard.server.queue.discovery.PartitionService; |
67 | 67 | import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
68 | 68 | import org.thingsboard.server.service.component.ComponentDiscoveryService; |
69 | +import org.thingsboard.server.service.edge.rpc.EdgeRpcService; | |
69 | 70 | import org.thingsboard.server.service.encoding.DataDecodingEncodingService; |
70 | 71 | import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
71 | 72 | import org.thingsboard.server.service.executors.ExternalCallExecutorService; |
... | ... | @@ -254,15 +255,14 @@ public class ActorSystemContext { |
254 | 255 | @Getter |
255 | 256 | private TbCoreDeviceRpcService tbCoreDeviceRpcService; |
256 | 257 | |
257 | - @Lazy | |
258 | - @Autowired | |
259 | - @Getter | |
260 | - private EdgeService edgeService; | |
258 | + @Autowired(required = false) | |
259 | + @Getter private EdgeService edgeService; | |
261 | 260 | |
262 | - @Lazy | |
263 | - @Autowired | |
264 | - @Getter | |
265 | - private EdgeEventService edgeEventService; | |
261 | + @Autowired(required = false) | |
262 | + @Getter private EdgeEventService edgeEventService; | |
263 | + | |
264 | + @Autowired(required = false) | |
265 | + @Getter private EdgeRpcService edgeRpcService; | |
266 | 266 | |
267 | 267 | @Value("${actors.session.max_concurrent_sessions_per_device:1}") |
268 | 268 | @Getter | ... | ... |
... | ... | @@ -31,10 +31,13 @@ import org.thingsboard.server.actors.service.ContextBasedCreator; |
31 | 31 | import org.thingsboard.server.actors.service.DefaultActorService; |
32 | 32 | import org.thingsboard.server.common.data.EntityType; |
33 | 33 | import org.thingsboard.server.common.data.Tenant; |
34 | +import org.thingsboard.server.common.data.edge.Edge; | |
34 | 35 | import org.thingsboard.server.common.data.id.DeviceId; |
36 | +import org.thingsboard.server.common.data.id.EdgeId; | |
35 | 37 | import org.thingsboard.server.common.data.id.EntityId; |
36 | 38 | import org.thingsboard.server.common.data.id.RuleChainId; |
37 | 39 | import org.thingsboard.server.common.data.id.TenantId; |
40 | +import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; | |
38 | 41 | import org.thingsboard.server.common.data.rule.RuleChain; |
39 | 42 | import org.thingsboard.server.common.data.rule.RuleChainType; |
40 | 43 | import org.thingsboard.server.common.msg.MsgType; |
... | ... | @@ -47,6 +50,7 @@ import org.thingsboard.server.common.msg.queue.PartitionChangeMsg; |
47 | 50 | import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg; |
48 | 51 | import org.thingsboard.server.common.msg.queue.RuleEngineException; |
49 | 52 | import org.thingsboard.server.common.msg.queue.ServiceType; |
53 | +import org.thingsboard.server.service.edge.rpc.EdgeRpcService; | |
50 | 54 | |
51 | 55 | import java.util.List; |
52 | 56 | import java.util.Optional; |
... | ... | @@ -202,7 +206,18 @@ public class TenantActor extends RuleChainManagerActor { |
202 | 206 | } |
203 | 207 | |
204 | 208 | private void onComponentLifecycleMsg(ComponentLifecycleMsg msg) { |
205 | - if (isRuleEngineForCurrentTenant) { | |
209 | + if (msg.getEntityId().getEntityType() == EntityType.EDGE) { | |
210 | + EdgeId edgeId = new EdgeId(msg.getEntityId().getId()); | |
211 | + EdgeRpcService edgeRpcService = systemContext.getEdgeRpcService(); | |
212 | + if (msg.getEvent() == ComponentLifecycleEvent.DELETED) { | |
213 | + edgeRpcService.deleteEdge(edgeId); | |
214 | + } else { | |
215 | + Edge edge = systemContext.getEdgeService().findEdgeById(tenantId, edgeId); | |
216 | + if (msg.getEvent() == ComponentLifecycleEvent.UPDATED) { | |
217 | + edgeRpcService.updateEdge(edge); | |
218 | + } | |
219 | + } | |
220 | + } else if (isRuleEngineForCurrentTenant) { | |
206 | 221 | TbActorRef target = getEntityActorRef(msg.getEntityId()); |
207 | 222 | if (target != null) { |
208 | 223 | if (msg.getEntityId().getEntityType() == EntityType.RULE_CHAIN) { | ... | ... |
... | ... | @@ -40,6 +40,7 @@ import org.thingsboard.server.common.data.id.RuleChainId; |
40 | 40 | import org.thingsboard.server.common.data.id.TenantId; |
41 | 41 | import org.thingsboard.server.common.data.page.TextPageData; |
42 | 42 | import org.thingsboard.server.common.data.page.TextPageLink; |
43 | +import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; | |
43 | 44 | import org.thingsboard.server.common.data.rule.RuleChain; |
44 | 45 | import org.thingsboard.server.dao.exception.DataValidationException; |
45 | 46 | import org.thingsboard.server.dao.exception.IncorrectParameterException; |
... | ... | @@ -101,6 +102,9 @@ public class EdgeController extends BaseController { |
101 | 102 | edgeService.assignDefaultRuleChainsToEdge(tenantId, savedEdge.getId()); |
102 | 103 | } |
103 | 104 | |
105 | + tbClusterService.onEntityStateChange(savedEdge.getTenantId(), savedEdge.getId(), | |
106 | + created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); | |
107 | + | |
104 | 108 | logEntityAction(savedEdge.getId(), savedEdge, null, created ? ActionType.ADDED : ActionType.UPDATED, null); |
105 | 109 | return savedEdge; |
106 | 110 | } catch (Exception e) { |
... | ... | @@ -120,6 +124,9 @@ public class EdgeController extends BaseController { |
120 | 124 | Edge edge = checkEdgeId(edgeId, Operation.DELETE); |
121 | 125 | edgeService.deleteEdge(getTenantId(), edgeId); |
122 | 126 | |
127 | + tbClusterService.onEntityStateChange(getTenantId(), edgeId, | |
128 | + ComponentLifecycleEvent.DELETED); | |
129 | + | |
123 | 130 | logEntityAction(edgeId, edge, |
124 | 131 | null, |
125 | 132 | ActionType.DELETED, null, strEdgeId); |
... | ... | @@ -284,6 +291,8 @@ public class EdgeController extends BaseController { |
284 | 291 | |
285 | 292 | Edge updatedEdge = edgeNotificationService.setEdgeRootRuleChain(getTenantId(), edge, ruleChainId); |
286 | 293 | |
294 | + tbClusterService.onEntityStateChange(updatedEdge.getTenantId(), updatedEdge.getId(), ComponentLifecycleEvent.UPDATED); | |
295 | + | |
287 | 296 | logEntityAction(updatedEdge.getId(), updatedEdge, null, ActionType.UPDATED, null); |
288 | 297 | |
289 | 298 | return updatedEdge; | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value; |
27 | 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
28 | 28 | import org.springframework.stereotype.Service; |
29 | 29 | import org.thingsboard.server.common.data.DataConstants; |
30 | +import org.thingsboard.server.common.data.edge.Edge; | |
30 | 31 | import org.thingsboard.server.common.data.id.EdgeId; |
31 | 32 | import org.thingsboard.server.common.data.id.TenantId; |
32 | 33 | import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
... | ... | @@ -52,7 +53,7 @@ import java.util.concurrent.Executors; |
52 | 53 | @Service |
53 | 54 | @Slf4j |
54 | 55 | @ConditionalOnProperty(prefix = "edges.rpc", value = "enabled", havingValue = "true") |
55 | -public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase { | |
56 | +public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase implements EdgeRpcService { | |
56 | 57 | |
57 | 58 | private final Map<EdgeId, EdgeGrpcSession> sessions = new ConcurrentHashMap<>(); |
58 | 59 | private static final ObjectMapper mapper = new ObjectMapper(); |
... | ... | @@ -117,6 +118,23 @@ public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase { |
117 | 118 | return new EdgeGrpcSession(ctx, outputStream, this::onEdgeConnect, this::onEdgeDisconnect, mapper).getInputStream(); |
118 | 119 | } |
119 | 120 | |
121 | + @Override | |
122 | + public void updateEdge(Edge edge) { | |
123 | + EdgeGrpcSession session = sessions.get(edge.getId()); | |
124 | + if (session != null && session.isConnected()) { | |
125 | + session.onConfigurationUpdate(edge); | |
126 | + } | |
127 | + } | |
128 | + | |
129 | + @Override | |
130 | + public void deleteEdge(EdgeId edgeId) { | |
131 | + EdgeGrpcSession session = sessions.get(edgeId); | |
132 | + if (session != null && session.isConnected()) { | |
133 | + session.close(); | |
134 | + sessions.remove(edgeId); | |
135 | + } | |
136 | + } | |
137 | + | |
120 | 138 | private void onEdgeConnect(EdgeId edgeId, EdgeGrpcSession edgeGrpcSession) { |
121 | 139 | sessions.put(edgeId, edgeGrpcSession); |
122 | 140 | save(edgeId, DefaultDeviceStateService.ACTIVITY_STATE, true); | ... | ... |
... | ... | @@ -192,6 +192,20 @@ public final class EdgeGrpcSession implements Closeable { |
192 | 192 | }; |
193 | 193 | } |
194 | 194 | |
195 | + void onConfigurationUpdate(Edge edge) { | |
196 | + try { | |
197 | + this.edge = edge; | |
198 | + // TODO: voba - push edge configuration update to edge | |
199 | +// outputStream.onNext(org.thingsboard.server.gen.integration.ResponseMsg.newBuilder() | |
200 | +// .setIntegrationUpdateMsg(IntegrationUpdateMsg.newBuilder() | |
201 | +// .setConfiguration(constructIntegrationConfigProto(configuration, defaultConverterProto, downLinkConverterProto)) | |
202 | +// .build()) | |
203 | +// .build()); | |
204 | + } catch (Exception e) { | |
205 | + log.error("Failed to construct proto objects!", e); | |
206 | + } | |
207 | + } | |
208 | + | |
195 | 209 | void processHandleMessages() throws ExecutionException, InterruptedException { |
196 | 210 | Long queueStartTs = getQueueStartTs().get(); |
197 | 211 | TimePageLink pageLink = new TimePageLink(ctx.getEdgeEventStorageSettings().getMaxReadRecordsCount(), queueStartTs, null, true); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.service.edge.rpc; | |
17 | + | |
18 | +import org.thingsboard.server.common.data.edge.Edge; | |
19 | +import org.thingsboard.server.common.data.id.EdgeId; | |
20 | + | |
21 | +public interface EdgeRpcService { | |
22 | + | |
23 | + void updateEdge(Edge edge); | |
24 | + | |
25 | + void deleteEdge(EdgeId edgeId); | |
26 | +} | ... | ... |