Commit e571bf24b6ad2f8b1bdee0a728adf90a54e9a41e
1 parent
6ede96ef
complited attributes update and attributes delete feature
Showing
3 changed files
with
43 additions
and
8 deletions
@@ -15,6 +15,8 @@ | @@ -15,6 +15,8 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.service.edge.rpc.constructor; | 16 | package org.thingsboard.server.service.edge.rpc.constructor; |
17 | 17 | ||
18 | +import com.google.gson.Gson; | ||
19 | +import com.google.gson.JsonArray; | ||
18 | import com.google.gson.JsonElement; | 20 | import com.google.gson.JsonElement; |
19 | import com.google.gson.JsonObject; | 21 | import com.google.gson.JsonObject; |
20 | import lombok.extern.slf4j.Slf4j; | 22 | import lombok.extern.slf4j.Slf4j; |
@@ -22,8 +24,11 @@ import org.springframework.stereotype.Component; | @@ -22,8 +24,11 @@ import org.springframework.stereotype.Component; | ||
22 | import org.thingsboard.server.common.data.audit.ActionType; | 24 | import org.thingsboard.server.common.data.audit.ActionType; |
23 | import org.thingsboard.server.common.data.id.EntityId; | 25 | import org.thingsboard.server.common.data.id.EntityId; |
24 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; | 26 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
27 | +import org.thingsboard.server.gen.edge.AttributeDeleteMsg; | ||
25 | import org.thingsboard.server.gen.edge.EntityDataProto; | 28 | import org.thingsboard.server.gen.edge.EntityDataProto; |
26 | 29 | ||
30 | +import java.util.List; | ||
31 | + | ||
27 | @Component | 32 | @Component |
28 | @Slf4j | 33 | @Slf4j |
29 | public class EntityDataMsgConstructor { | 34 | public class EntityDataMsgConstructor { |
@@ -50,8 +55,19 @@ public class EntityDataMsgConstructor { | @@ -50,8 +55,19 @@ public class EntityDataMsgConstructor { | ||
50 | log.warn("Can't convert to attributes proto, entityData [{}]", entityData, e); | 55 | log.warn("Can't convert to attributes proto, entityData [{}]", entityData, e); |
51 | } | 56 | } |
52 | break; | 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 | return builder.build(); | 72 | return builder.build(); |
57 | } | 73 | } |
@@ -110,9 +110,15 @@ message EntityDataProto { | @@ -110,9 +110,15 @@ message EntityDataProto { | ||
110 | transport.PostTelemetryMsg postTelemetryMsg = 4; | 110 | transport.PostTelemetryMsg postTelemetryMsg = 4; |
111 | transport.PostAttributeMsg postAttributesMsg = 5; | 111 | transport.PostAttributeMsg postAttributesMsg = 5; |
112 | string postAttributeScope = 6; | 112 | string postAttributeScope = 6; |
113 | + AttributeDeleteMsg attributeDeleteMsg = 7; | ||
113 | // transport.ToDeviceRpcRequestMsg ??? | 114 | // transport.ToDeviceRpcRequestMsg ??? |
114 | } | 115 | } |
115 | 116 | ||
117 | +message AttributeDeleteMsg { | ||
118 | + string scope = 1; | ||
119 | + repeated string attributeNames = 2; | ||
120 | +} | ||
121 | + | ||
116 | message RuleChainUpdateMsg { | 122 | message RuleChainUpdateMsg { |
117 | UpdateMsgType msgType = 1; | 123 | UpdateMsgType msgType = 1; |
118 | int64 idMSB = 2; | 124 | int64 idMSB = 2; |
@@ -139,17 +139,15 @@ public class TbMsgPushToEdgeNode implements TbNode { | @@ -139,17 +139,15 @@ public class TbMsgPushToEdgeNode implements TbNode { | ||
139 | if (edgeEventTypeByEntityType == null) { | 139 | if (edgeEventTypeByEntityType == null) { |
140 | return null; | 140 | return null; |
141 | } | 141 | } |
142 | + ActionType actionType = getActionTypeByMsgType(msg.getType()); | ||
142 | JsonNode entityBody = null; | 143 | JsonNode entityBody = null; |
143 | JsonNode data = json.readTree(msg.getData()); | 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 | } else { | 147 | } else { |
150 | entityBody = data; | 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,6 +161,21 @@ public class TbMsgPushToEdgeNode implements TbNode { | ||
163 | return edgeEvent; | 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 | private UUID getUUIDFromMsgData(TbMsg msg) throws JsonProcessingException { | 179 | private UUID getUUIDFromMsgData(TbMsg msg) throws JsonProcessingException { |
167 | JsonNode data = json.readTree(msg.getData()).get("id"); | 180 | JsonNode data = json.readTree(msg.getData()).get("id"); |
168 | String id = json.treeToValue(data.get("id"), String.class); | 181 | String id = json.treeToValue(data.get("id"), String.class); |