Commit e571bf24b6ad2f8b1bdee0a728adf90a54e9a41e

Authored by Bohdan Smetaniuk
1 parent 6ede96ef

complited attributes update and attributes delete feature

... ... @@ -15,6 +15,8 @@
15 15 */
16 16 package org.thingsboard.server.service.edge.rpc.constructor;
17 17
  18 +import com.google.gson.Gson;
  19 +import com.google.gson.JsonArray;
18 20 import com.google.gson.JsonElement;
19 21 import com.google.gson.JsonObject;
20 22 import lombok.extern.slf4j.Slf4j;
... ... @@ -22,8 +24,11 @@ import org.springframework.stereotype.Component;
22 24 import org.thingsboard.server.common.data.audit.ActionType;
23 25 import org.thingsboard.server.common.data.id.EntityId;
24 26 import org.thingsboard.server.common.transport.adaptor.JsonConverter;
  27 +import org.thingsboard.server.gen.edge.AttributeDeleteMsg;
25 28 import org.thingsboard.server.gen.edge.EntityDataProto;
26 29
  30 +import java.util.List;
  31 +
27 32 @Component
28 33 @Slf4j
29 34 public class EntityDataMsgConstructor {
... ... @@ -50,8 +55,19 @@ public class EntityDataMsgConstructor {
50 55 log.warn("Can't convert to attributes proto, entityData [{}]", entityData, e);
51 56 }
52 57 break;
53   - // TODO: voba - add support for attribute delete
54   - // case ATTRIBUTES_DELETED:
  58 + case ATTRIBUTES_DELETED:
  59 + try {
  60 + AttributeDeleteMsg.Builder attributeDeleteMsg = AttributeDeleteMsg.newBuilder();
  61 + attributeDeleteMsg.setScope(entityData.getAsJsonObject().getAsJsonPrimitive("scope").getAsString());
  62 + JsonArray jsonArray = entityData.getAsJsonObject().getAsJsonArray("keys");
  63 + List<String> keys = new Gson().fromJson(jsonArray.toString(), List.class);
  64 + attributeDeleteMsg.addAllAttributeNames(keys);
  65 + attributeDeleteMsg.build();
  66 + builder.setAttributeDeleteMsg(attributeDeleteMsg);
  67 + } catch (Exception e) {
  68 + log.warn("Can't convert to AttributeDeleteMsg proto, entityData [{}]", entityData, e);
  69 + }
  70 + break;
55 71 }
56 72 return builder.build();
57 73 }
... ...
... ... @@ -110,9 +110,15 @@ message EntityDataProto {
110 110 transport.PostTelemetryMsg postTelemetryMsg = 4;
111 111 transport.PostAttributeMsg postAttributesMsg = 5;
112 112 string postAttributeScope = 6;
  113 + AttributeDeleteMsg attributeDeleteMsg = 7;
113 114 // transport.ToDeviceRpcRequestMsg ???
114 115 }
115 116
  117 +message AttributeDeleteMsg {
  118 + string scope = 1;
  119 + repeated string attributeNames = 2;
  120 +}
  121 +
116 122 message RuleChainUpdateMsg {
117 123 UpdateMsgType msgType = 1;
118 124 int64 idMSB = 2;
... ...
... ... @@ -139,17 +139,15 @@ public class TbMsgPushToEdgeNode implements TbNode {
139 139 if (edgeEventTypeByEntityType == null) {
140 140 return null;
141 141 }
  142 + ActionType actionType = getActionTypeByMsgType(msg.getType());
142 143 JsonNode entityBody = null;
143 144 JsonNode data = json.readTree(msg.getData());
144   - if (SessionMsgType.POST_ATTRIBUTES_REQUEST.name().equals(msg.getType())) {
145   - Map<String, Object> entityData = new HashMap<>();
146   - entityData.put("kv", data);
147   - entityData.put("scope", msg.getMetaData().getData().get("scope"));
148   - entityBody = json.valueToTree(entityData);
  145 + if (actionType.equals(ActionType.ATTRIBUTES_UPDATED) || actionType.equals(ActionType.ATTRIBUTES_DELETED)) {
  146 + entityBody = getAttributeEntityBody(actionType, data, msg.getMetaData().getData());
149 147 } else {
150 148 entityBody = data;
151 149 }
152   - return buildEdgeEvent(ctx.getTenantId(), getActionTypeByMsgType(msg.getType()), msg.getOriginator().getId(), edgeEventTypeByEntityType, entityBody);
  150 + return buildEdgeEvent(ctx.getTenantId(), actionType, msg.getOriginator().getId(), edgeEventTypeByEntityType, entityBody);
153 151 }
154 152 }
155 153
... ... @@ -163,6 +161,21 @@ public class TbMsgPushToEdgeNode implements TbNode {
163 161 return edgeEvent;
164 162 }
165 163
  164 + private JsonNode getAttributeEntityBody(ActionType actionType, JsonNode data, Map<String, String> metadata) throws JsonProcessingException {
  165 + Map<String, Object> entityData = new HashMap<>();
  166 + switch (actionType) {
  167 + case ATTRIBUTES_UPDATED:
  168 + entityData.put("kv", data);
  169 + break;
  170 + case ATTRIBUTES_DELETED:
  171 + List<String> keys = json.treeToValue(data.get("attributes"), List.class);
  172 + entityData.put("keys", keys);
  173 + break;
  174 + }
  175 + entityData.put("scope", metadata.get("scope"));
  176 + return json.valueToTree(entityData);
  177 + }
  178 +
166 179 private UUID getUUIDFromMsgData(TbMsg msg) throws JsonProcessingException {
167 180 JsonNode data = json.readTree(msg.getData()).get("id");
168 181 String id = json.treeToValue(data.get("id"), String.class);
... ...