Commit 23a2ecfe904e0247ce44540fcc7eec763ba4e732
Merge remote-tracking branch 'origin/feature/edge' into feature/edge
Showing
2 changed files
with
68 additions
and
5 deletions
... | ... | @@ -48,6 +48,8 @@ import org.thingsboard.server.common.data.asset.Asset; |
48 | 48 | import org.thingsboard.server.common.data.audit.ActionType; |
49 | 49 | import org.thingsboard.server.common.data.edge.Edge; |
50 | 50 | import org.thingsboard.server.common.data.edge.EdgeEvent; |
51 | +import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; | |
52 | +import org.thingsboard.server.common.data.exception.ThingsboardException; | |
51 | 53 | import org.thingsboard.server.common.data.id.AlarmId; |
52 | 54 | import org.thingsboard.server.common.data.id.AssetId; |
53 | 55 | import org.thingsboard.server.common.data.id.CustomerId; |
... | ... | @@ -55,6 +57,7 @@ import org.thingsboard.server.common.data.id.DashboardId; |
55 | 57 | import org.thingsboard.server.common.data.id.DeviceId; |
56 | 58 | import org.thingsboard.server.common.data.id.EdgeId; |
57 | 59 | import org.thingsboard.server.common.data.id.EntityId; |
60 | +import org.thingsboard.server.common.data.id.EntityIdFactory; | |
58 | 61 | import org.thingsboard.server.common.data.id.EntityViewId; |
59 | 62 | import org.thingsboard.server.common.data.id.RuleChainId; |
60 | 63 | import org.thingsboard.server.common.data.id.TenantId; |
... | ... | @@ -836,6 +839,11 @@ public final class EdgeGrpcSession implements Closeable { |
836 | 839 | result.add(onAlarmUpdate(alarmUpdateMsg)); |
837 | 840 | } |
838 | 841 | } |
842 | + if (uplinkMsg.getRelationUpdateMsgList() != null && !uplinkMsg.getRelationUpdateMsgList().isEmpty()) { | |
843 | + for (RelationUpdateMsg relationUpdateMsg: uplinkMsg.getRelationUpdateMsgList()) { | |
844 | + onRelationUpdate(relationUpdateMsg); | |
845 | + } | |
846 | + } | |
839 | 847 | if (uplinkMsg.getRuleChainMetadataRequestMsgList() != null && !uplinkMsg.getRuleChainMetadataRequestMsgList().isEmpty()) { |
840 | 848 | for (RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg : uplinkMsg.getRuleChainMetadataRequestMsgList()) { |
841 | 849 | result.add(ctx.getSyncEdgeService().processRuleChainMetadataRequestMsg(edge, ruleChainMetadataRequestMsg)); |
... | ... | @@ -1180,6 +1188,60 @@ public final class EdgeGrpcSession implements Closeable { |
1180 | 1188 | } |
1181 | 1189 | } |
1182 | 1190 | |
1191 | + private void onRelationUpdate(RelationUpdateMsg relationUpdateMsg) { | |
1192 | + log.info("onRelationUpdate {}", relationUpdateMsg); | |
1193 | + try { | |
1194 | + EntityRelation entityRelation = new EntityRelation(); | |
1195 | + | |
1196 | + UUID fromUUID = new UUID(relationUpdateMsg.getFromIdMSB(), relationUpdateMsg.getFromIdLSB()); | |
1197 | + EntityId fromId = EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(relationUpdateMsg.getFromEntityType()), fromUUID); | |
1198 | + entityRelation.setFrom(fromId); | |
1199 | + | |
1200 | + UUID toUUID = new UUID(relationUpdateMsg.getToIdMSB(), relationUpdateMsg.getToIdLSB()); | |
1201 | + EntityId toId = EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(relationUpdateMsg.getToEntityType()), toUUID); | |
1202 | + entityRelation.setTo(toId); | |
1203 | + | |
1204 | + entityRelation.setType(relationUpdateMsg.getType()); | |
1205 | + entityRelation.setTypeGroup(RelationTypeGroup.valueOf(relationUpdateMsg.getTypeGroup())); | |
1206 | + entityRelation.setAdditionalInfo(mapper.readTree(relationUpdateMsg.getAdditionalInfo())); | |
1207 | + switch (relationUpdateMsg.getMsgType()) { | |
1208 | + case ENTITY_CREATED_RPC_MESSAGE: | |
1209 | + case ENTITY_UPDATED_RPC_MESSAGE: | |
1210 | + if (isEntityExists(edge.getTenantId(), entityRelation.getTo()) | |
1211 | + && isEntityExists(edge.getTenantId(), entityRelation.getFrom())) { | |
1212 | + ctx.getRelationService().saveRelationAsync(edge.getTenantId(), entityRelation); | |
1213 | + } | |
1214 | + break; | |
1215 | + case ENTITY_DELETED_RPC_MESSAGE: | |
1216 | + ctx.getRelationService().deleteRelation(edge.getTenantId(), entityRelation); | |
1217 | + break; | |
1218 | + case UNRECOGNIZED: | |
1219 | + log.error("Unsupported msg type"); | |
1220 | + } | |
1221 | + } catch (Exception e) { | |
1222 | + log.error("Error during relation update msg", e); | |
1223 | + } | |
1224 | + } | |
1225 | + | |
1226 | + private boolean isEntityExists(TenantId tenantId, EntityId entityId) throws ThingsboardException { | |
1227 | + switch (entityId.getEntityType()) { | |
1228 | + case DEVICE: | |
1229 | + return ctx.getDeviceService().findDeviceById(tenantId, new DeviceId(entityId.getId())) != null; | |
1230 | + case ASSET: | |
1231 | + return ctx.getAssetService().findAssetById(tenantId, new AssetId(entityId.getId())) != null; | |
1232 | + case ENTITY_VIEW: | |
1233 | + return ctx.getEntityViewService().findEntityViewById(tenantId, new EntityViewId(entityId.getId())) != null; | |
1234 | + case CUSTOMER: | |
1235 | + return ctx.getCustomerService().findCustomerById(tenantId, new CustomerId(entityId.getId())) != null; | |
1236 | + case USER: | |
1237 | + return ctx.getUserService().findUserById(tenantId, new UserId(entityId.getId())) != null; | |
1238 | + case DASHBOARD: | |
1239 | + return ctx.getDashboardService().findDashboardById(tenantId, new DashboardId(entityId.getId())) != null; | |
1240 | + default: | |
1241 | + throw new ThingsboardException("Unsupported entity type " + entityId.getEntityType(), ThingsboardErrorCode.INVALID_ARGUMENTS); | |
1242 | + } | |
1243 | + } | |
1244 | + | |
1183 | 1245 | private ConnectResponseMsg processConnect(ConnectRequestMsg request) { |
1184 | 1246 | Optional<Edge> optional = ctx.getEdgeService().findEdgeByRoutingKey(TenantId.SYS_TENANT_ID, request.getEdgeRoutingKey()); |
1185 | 1247 | if (optional.isPresent()) { | ... | ... |
... | ... | @@ -330,11 +330,12 @@ message UplinkMsg { |
330 | 330 | repeated DeviceUpdateMsg deviceUpdateMsg = 3; |
331 | 331 | repeated DeviceCredentialsUpdateMsg deviceCredentialsUpdateMsg = 4; |
332 | 332 | repeated AlarmUpdateMsg alarmUpdateMsg = 5; |
333 | - repeated RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg = 6; | |
334 | - repeated AttributesRequestMsg attributesRequestMsg = 7; | |
335 | - repeated RelationRequestMsg relationRequestMsg = 8; | |
336 | - repeated UserCredentialsRequestMsg userCredentialsRequestMsg = 9; | |
337 | - repeated DeviceCredentialsRequestMsg deviceCredentialsRequestMsg = 10; | |
333 | + repeated RelationUpdateMsg relationUpdateMsg = 6; | |
334 | + repeated RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg = 7; | |
335 | + repeated AttributesRequestMsg attributesRequestMsg = 8; | |
336 | + repeated RelationRequestMsg relationRequestMsg = 9; | |
337 | + repeated UserCredentialsRequestMsg userCredentialsRequestMsg = 10; | |
338 | + repeated DeviceCredentialsRequestMsg deviceCredentialsRequestMsg = 11; | |
338 | 339 | } |
339 | 340 | |
340 | 341 | message UplinkResponseMsg { | ... | ... |