Commit 76b35010c75dd0e19df21b3ed2faff4e2dc56e3a

Authored by Bohdan Smetaniuk
1 parent 5af7eb82

WidgetTypes fetch feature

... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.controller;
17 17
  18 +import lombok.extern.slf4j.Slf4j;
18 19 import org.springframework.http.HttpStatus;
19 20 import org.springframework.security.access.prepost.PreAuthorize;
20 21 import org.springframework.web.bind.annotation.PathVariable;
... ... @@ -25,6 +26,7 @@ import org.springframework.web.bind.annotation.RequestParam;
25 26 import org.springframework.web.bind.annotation.ResponseBody;
26 27 import org.springframework.web.bind.annotation.ResponseStatus;
27 28 import org.springframework.web.bind.annotation.RestController;
  29 +import org.thingsboard.server.common.data.audit.ActionType;
28 30 import org.thingsboard.server.common.data.exception.ThingsboardException;
29 31 import org.thingsboard.server.common.data.id.TenantId;
30 32 import org.thingsboard.server.common.data.id.WidgetTypeId;
... ... @@ -37,6 +39,7 @@ import org.thingsboard.server.service.security.permission.Resource;
37 39
38 40 import java.util.List;
39 41
  42 +@Slf4j
40 43 @RestController
41 44 @TbCoreComponent
42 45 @RequestMapping("/api")
... ... @@ -67,8 +70,11 @@ public class WidgetTypeController extends BaseController {
67 70 }
68 71
69 72 checkEntity(widgetType.getId(), widgetType, Resource.WIDGET_TYPE);
  73 + WidgetType savedWidgetType = widgetTypeService.saveWidgetType(widgetType);
70 74
71   - return checkNotNull(widgetTypeService.saveWidgetType(widgetType));
  75 + sendNotificationMsgToEdgeService(savedWidgetType.getTenantId(), savedWidgetType.getId(), ActionType.UPDATED);
  76 +
  77 + return checkNotNull(savedWidgetType);
72 78 } catch (Exception e) {
73 79 throw handleException(e);
74 80 }
... ... @@ -83,6 +89,9 @@ public class WidgetTypeController extends BaseController {
83 89 WidgetTypeId widgetTypeId = new WidgetTypeId(toUUID(strWidgetTypeId));
84 90 checkWidgetTypeId(widgetTypeId, Operation.DELETE);
85 91 widgetTypeService.deleteWidgetType(getCurrentUser().getTenantId(), widgetTypeId);
  92 +
  93 + sendNotificationMsgToEdgeService(getTenantId(), widgetTypeId, ActionType.DELETED);
  94 +
86 95 } catch (Exception e) {
87 96 throw handleException(e);
88 97 }
... ...
... ... @@ -158,6 +158,7 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
158 158 case DASHBOARD:
159 159 case RULE_CHAIN:
160 160 case WIDGETS_BUNDLE:
  161 + case WIDGET_TYPE:
161 162 processEntity(tenantId, edgeNotificationMsg);
162 163 break;
163 164 case ALARM:
... ... @@ -186,7 +187,7 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
186 187 // case ADDED:
187 188 case UPDATED:
188 189 case CREDENTIALS_UPDATED:
189   - if (edgeEventType.equals(EdgeEventType.WIDGETS_BUNDLE)) {
  190 + if (edgeEventType.equals(EdgeEventType.WIDGETS_BUNDLE) || edgeEventType.equals(EdgeEventType.WIDGET_TYPE)) {
190 191 TextPageData<Edge> edgesByTenantId = edgeService.findEdgesByTenantId(tenantId, new TextPageLink(Integer.MAX_VALUE));
191 192 if (edgesByTenantId != null && edgesByTenantId.getData() != null && !edgesByTenantId.getData().isEmpty()) {
192 193 for (Edge edge : edgesByTenantId.getData()) {
... ...
... ... @@ -33,6 +33,7 @@ import org.thingsboard.server.dao.entityview.EntityViewService;
33 33 import org.thingsboard.server.dao.relation.RelationService;
34 34 import org.thingsboard.server.dao.rule.RuleChainService;
35 35 import org.thingsboard.server.dao.user.UserService;
  36 +import org.thingsboard.server.dao.widget.WidgetTypeService;
36 37 import org.thingsboard.server.dao.widget.WidgetsBundleService;
37 38 import org.thingsboard.server.queue.discovery.PartitionService;
38 39 import org.thingsboard.server.queue.provider.TbQueueProducerProvider;
... ... @@ -118,6 +119,10 @@ public class EdgeContextComponent {
118 119
119 120 @Lazy
120 121 @Autowired
  122 + private WidgetTypeService widgetTypeService;
  123 +
  124 + @Lazy
  125 + @Autowired
121 126 private DeviceStateService deviceStateService;
122 127
123 128 @Lazy
... ... @@ -166,6 +171,10 @@ public class EdgeContextComponent {
166 171
167 172 @Lazy
168 173 @Autowired
  174 + private WidgetTypeUpdateMsgConstructor widgetTypeUpdateMsgConstructor;
  175 +
  176 + @Lazy
  177 + @Autowired
169 178 private EntityDataMsgConstructor entityDataMsgConstructor;
170 179
171 180 @Lazy
... ...
... ... @@ -43,18 +43,7 @@ import org.thingsboard.server.common.data.asset.Asset;
43 43 import org.thingsboard.server.common.data.audit.ActionType;
44 44 import org.thingsboard.server.common.data.edge.Edge;
45 45 import org.thingsboard.server.common.data.edge.EdgeEvent;
46   -import org.thingsboard.server.common.data.id.AlarmId;
47   -import org.thingsboard.server.common.data.id.AssetId;
48   -import org.thingsboard.server.common.data.id.CustomerId;
49   -import org.thingsboard.server.common.data.id.DashboardId;
50   -import org.thingsboard.server.common.data.id.DeviceId;
51   -import org.thingsboard.server.common.data.id.EdgeId;
52   -import org.thingsboard.server.common.data.id.EntityId;
53   -import org.thingsboard.server.common.data.id.EntityViewId;
54   -import org.thingsboard.server.common.data.id.RuleChainId;
55   -import org.thingsboard.server.common.data.id.TenantId;
56   -import org.thingsboard.server.common.data.id.UserId;
57   -import org.thingsboard.server.common.data.id.WidgetsBundleId;
  46 +import org.thingsboard.server.common.data.id.*;
58 47 import org.thingsboard.server.common.data.kv.AttributeKvEntry;
59 48 import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
60 49 import org.thingsboard.server.common.data.kv.LongDataEntry;
... ... @@ -67,6 +56,7 @@ import org.thingsboard.server.common.data.rule.RuleChainMetaData;
67 56 import org.thingsboard.server.common.data.security.DeviceCredentials;
68 57 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
69 58 import org.thingsboard.server.common.data.security.UserCredentials;
  59 +import org.thingsboard.server.common.data.widget.WidgetType;
70 60 import org.thingsboard.server.common.data.widget.WidgetsBundle;
71 61 import org.thingsboard.server.common.msg.TbMsg;
72 62 import org.thingsboard.server.common.msg.TbMsgMetaData;
... ... @@ -74,34 +64,7 @@ import org.thingsboard.server.common.msg.queue.ServiceType;
74 64 import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
75 65 import org.thingsboard.server.common.msg.session.SessionMsgType;
76 66 import org.thingsboard.server.common.transport.util.JsonUtils;
77   -import org.thingsboard.server.gen.edge.AlarmUpdateMsg;
78   -import org.thingsboard.server.gen.edge.AssetUpdateMsg;
79   -import org.thingsboard.server.gen.edge.AttributesRequestMsg;
80   -import org.thingsboard.server.gen.edge.ConnectRequestMsg;
81   -import org.thingsboard.server.gen.edge.ConnectResponseCode;
82   -import org.thingsboard.server.gen.edge.ConnectResponseMsg;
83   -import org.thingsboard.server.gen.edge.DashboardUpdateMsg;
84   -import org.thingsboard.server.gen.edge.DeviceCredentialsRequestMsg;
85   -import org.thingsboard.server.gen.edge.DeviceCredentialsUpdateMsg;
86   -import org.thingsboard.server.gen.edge.DeviceUpdateMsg;
87   -import org.thingsboard.server.gen.edge.DownlinkMsg;
88   -import org.thingsboard.server.gen.edge.EdgeConfiguration;
89   -import org.thingsboard.server.gen.edge.EntityDataProto;
90   -import org.thingsboard.server.gen.edge.EntityUpdateMsg;
91   -import org.thingsboard.server.gen.edge.EntityViewUpdateMsg;
92   -import org.thingsboard.server.gen.edge.RelationRequestMsg;
93   -import org.thingsboard.server.gen.edge.RequestMsg;
94   -import org.thingsboard.server.gen.edge.RequestMsgType;
95   -import org.thingsboard.server.gen.edge.ResponseMsg;
96   -import org.thingsboard.server.gen.edge.RuleChainMetadataRequestMsg;
97   -import org.thingsboard.server.gen.edge.RuleChainMetadataUpdateMsg;
98   -import org.thingsboard.server.gen.edge.RuleChainUpdateMsg;
99   -import org.thingsboard.server.gen.edge.UpdateMsgType;
100   -import org.thingsboard.server.gen.edge.UplinkMsg;
101   -import org.thingsboard.server.gen.edge.UplinkResponseMsg;
102   -import org.thingsboard.server.gen.edge.UserCredentialsRequestMsg;
103   -import org.thingsboard.server.gen.edge.UserCredentialsUpdateMsg;
104   -import org.thingsboard.server.gen.edge.WidgetsBundleUpdateMsg;
  67 +import org.thingsboard.server.gen.edge.*;
105 68 import org.thingsboard.server.gen.transport.TransportProtos;
106 69 import org.thingsboard.server.queue.TbQueueCallback;
107 70 import org.thingsboard.server.queue.TbQueueMsgMetadata;
... ... @@ -341,6 +304,9 @@ public final class EdgeGrpcSession implements Closeable {
341 304 case WIDGETS_BUNDLE:
342 305 processWidgetsBundle(edgeEvent, msgType, edgeEventAction);
343 306 break;
  307 + case WIDGET_TYPE:
  308 + processWidgetType(edgeEvent, msgType, edgeEventAction);
  309 + break;
344 310 }
345 311 }
346 312
... ... @@ -624,6 +590,36 @@ public final class EdgeGrpcSession implements Closeable {
624 590 }
625 591 }
626 592
  593 + private void processWidgetType(EdgeEvent edgeEvent, UpdateMsgType msgType, ActionType edgeActionType) {
  594 + WidgetTypeId widgetTypeId = new WidgetTypeId(edgeEvent.getEntityId());
  595 + EntityUpdateMsg entityUpdateMsg = null;
  596 + switch (edgeActionType) {
  597 + case ADDED:
  598 + case UPDATED:
  599 + WidgetType widgetType = ctx.getWidgetTypeService().findWidgetTypeById(edgeEvent.getTenantId(), widgetTypeId);
  600 + if (widgetType != null) {
  601 + WidgetTypeUpdateMsg widgetTypeUpdateMsg =
  602 + ctx.getWidgetTypeUpdateMsgConstructor().constructWidgetTypeUpdateMsg(msgType, widgetType);
  603 + entityUpdateMsg = EntityUpdateMsg.newBuilder()
  604 + .setWidgetTypeUpdateMsg(widgetTypeUpdateMsg)
  605 + .build();
  606 + }
  607 + break;
  608 + case DELETED:
  609 + WidgetTypeUpdateMsg widgetTypeUpdateMsg =
  610 + ctx.getWidgetTypeUpdateMsgConstructor().constructWidgetTypeUpdateMsg(widgetTypeId);
  611 + entityUpdateMsg = EntityUpdateMsg.newBuilder()
  612 + .setWidgetTypeUpdateMsg(widgetTypeUpdateMsg)
  613 + .build();
  614 + break;
  615 + }
  616 + if (entityUpdateMsg != null) {
  617 + outputStream.onNext(ResponseMsg.newBuilder()
  618 + .setEntityUpdateMsg(entityUpdateMsg)
  619 + .build());
  620 + }
  621 + }
  622 +
627 623 private UpdateMsgType getResponseMsgType(ActionType actionType) {
628 624 switch (actionType) {
629 625 case UPDATED:
... ...
  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.constructor;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.stereotype.Component;
  20 +import org.thingsboard.server.common.data.id.WidgetTypeId;
  21 +import org.thingsboard.server.common.data.widget.WidgetType;
  22 +import org.thingsboard.server.dao.util.mapping.JacksonUtil;
  23 +import org.thingsboard.server.gen.edge.UpdateMsgType;
  24 +import org.thingsboard.server.gen.edge.WidgetTypeUpdateMsg;
  25 +
  26 +@Component
  27 +@Slf4j
  28 +public class WidgetTypeUpdateMsgConstructor {
  29 +
  30 + public WidgetTypeUpdateMsg constructWidgetTypeUpdateMsg(UpdateMsgType msgType, WidgetType widgetType) {
  31 + WidgetTypeUpdateMsg.Builder builder = WidgetTypeUpdateMsg.newBuilder()
  32 + .setMsgType(msgType)
  33 + .setIdMSB(widgetType.getId().getId().getMostSignificantBits())
  34 + .setIdLSB(widgetType.getId().getId().getLeastSignificantBits());
  35 + if (widgetType.getBundleAlias() != null) {
  36 + builder.setBundleAlias(widgetType.getBundleAlias());
  37 + }
  38 + if (widgetType.getAlias() != null) {
  39 + builder.setAlias(widgetType.getAlias());
  40 + }
  41 + if (widgetType.getName() != null) {
  42 + builder.setName(widgetType.getName());
  43 + }
  44 + if (widgetType.getDescriptor() != null) {
  45 + builder.setDescriptorJson(JacksonUtil.toString(widgetType.getDescriptor()));
  46 + }
  47 + return builder.build();
  48 + }
  49 +
  50 + public WidgetTypeUpdateMsg constructWidgetTypeUpdateMsg(WidgetTypeId widgetTypeId) {
  51 + return WidgetTypeUpdateMsg.newBuilder()
  52 + .setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE)
  53 + .setIdMSB(widgetTypeId.getId().getMostSignificantBits())
  54 + .setIdLSB(widgetTypeId.getId().getLeastSignificantBits())
  55 + .build();
  56 + }
  57 +}
... ...
... ... @@ -38,6 +38,8 @@ public final class EdgeUtils {
38 38 return EdgeEventType.ALARM;
39 39 case WIDGETS_BUNDLE:
40 40 return EdgeEventType.WIDGETS_BUNDLE;
  41 + case WIDGET_TYPE:
  42 + return EdgeEventType.WIDGET_TYPE;
41 43 default:
42 44 return null;
43 45 }
... ...
... ... @@ -27,5 +27,6 @@ public enum EdgeEventType {
27 27 USER,
28 28 CUSTOMER,
29 29 RELATION,
30   - WIDGETS_BUNDLE
  30 + WIDGETS_BUNDLE,
  31 + WIDGET_TYPE
31 32 }
... ...
... ... @@ -89,6 +89,8 @@ public class EntityIdFactory {
89 89 return new EntityViewId(uuid);
90 90 case WIDGETS_BUNDLE:
91 91 return new WidgetsBundleId(uuid);
  92 + case WIDGET_TYPE:
  93 + return new WidgetTypeId(uuid);
92 94 case EDGE:
93 95 return new EdgeId(uuid);
94 96 }
... ...
... ... @@ -60,6 +60,7 @@ message EntityUpdateMsg {
60 60 CustomerUpdateMsg customerUpdateMsg = 11;
61 61 RelationUpdateMsg relationUpdateMsg = 12;
62 62 WidgetsBundleUpdateMsg widgetsBundleUpdateMsg = 13;
  63 + WidgetTypeUpdateMsg widgetTypeUpdateMsg = 14;
63 64 }
64 65
65 66 enum RequestMsgType {
... ... @@ -276,6 +277,16 @@ message WidgetsBundleUpdateMsg {
276 277 bytes image = 6;
277 278 }
278 279
  280 +message WidgetTypeUpdateMsg {
  281 + UpdateMsgType msgType = 1;
  282 + int64 idMSB = 2;
  283 + int64 idLSB = 3;
  284 + string bundleAlias = 4;
  285 + string alias = 5;
  286 + string name = 6;
  287 + string descriptorJson = 7;
  288 +}
  289 +
279 290 message UserCredentialsUpdateMsg {
280 291 int64 userIdMSB = 1;
281 292 int64 userIdLSB = 2;
... ...