complited attributes update and attributes delete feature

This commit is contained in:
Bohdan Smetaniuk 2020-07-15 19:37:01 +03:00
parent 6ede96ef48
commit e571bf24b6
3 changed files with 43 additions and 8 deletions

View File

@ -15,6 +15,8 @@
*/
package org.thingsboard.server.service.edge.rpc.constructor;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
@ -22,8 +24,11 @@ import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.transport.adaptor.JsonConverter;
import org.thingsboard.server.gen.edge.AttributeDeleteMsg;
import org.thingsboard.server.gen.edge.EntityDataProto;
import java.util.List;
@Component
@Slf4j
public class EntityDataMsgConstructor {
@ -50,8 +55,19 @@ public class EntityDataMsgConstructor {
log.warn("Can't convert to attributes proto, entityData [{}]", entityData, e);
}
break;
// TODO: voba - add support for attribute delete
// case ATTRIBUTES_DELETED:
case ATTRIBUTES_DELETED:
try {
AttributeDeleteMsg.Builder attributeDeleteMsg = AttributeDeleteMsg.newBuilder();
attributeDeleteMsg.setScope(entityData.getAsJsonObject().getAsJsonPrimitive("scope").getAsString());
JsonArray jsonArray = entityData.getAsJsonObject().getAsJsonArray("keys");
List<String> keys = new Gson().fromJson(jsonArray.toString(), List.class);
attributeDeleteMsg.addAllAttributeNames(keys);
attributeDeleteMsg.build();
builder.setAttributeDeleteMsg(attributeDeleteMsg);
} catch (Exception e) {
log.warn("Can't convert to AttributeDeleteMsg proto, entityData [{}]", entityData, e);
}
break;
}
return builder.build();
}

View File

@ -110,9 +110,15 @@ message EntityDataProto {
transport.PostTelemetryMsg postTelemetryMsg = 4;
transport.PostAttributeMsg postAttributesMsg = 5;
string postAttributeScope = 6;
AttributeDeleteMsg attributeDeleteMsg = 7;
// transport.ToDeviceRpcRequestMsg ???
}
message AttributeDeleteMsg {
string scope = 1;
repeated string attributeNames = 2;
}
message RuleChainUpdateMsg {
UpdateMsgType msgType = 1;
int64 idMSB = 2;

View File

@ -139,17 +139,15 @@ public class TbMsgPushToEdgeNode implements TbNode {
if (edgeEventTypeByEntityType == null) {
return null;
}
ActionType actionType = getActionTypeByMsgType(msg.getType());
JsonNode entityBody = null;
JsonNode data = json.readTree(msg.getData());
if (SessionMsgType.POST_ATTRIBUTES_REQUEST.name().equals(msg.getType())) {
Map<String, Object> entityData = new HashMap<>();
entityData.put("kv", data);
entityData.put("scope", msg.getMetaData().getData().get("scope"));
entityBody = json.valueToTree(entityData);
if (actionType.equals(ActionType.ATTRIBUTES_UPDATED) || actionType.equals(ActionType.ATTRIBUTES_DELETED)) {
entityBody = getAttributeEntityBody(actionType, data, msg.getMetaData().getData());
} else {
entityBody = data;
}
return buildEdgeEvent(ctx.getTenantId(), getActionTypeByMsgType(msg.getType()), msg.getOriginator().getId(), edgeEventTypeByEntityType, entityBody);
return buildEdgeEvent(ctx.getTenantId(), actionType, msg.getOriginator().getId(), edgeEventTypeByEntityType, entityBody);
}
}
@ -163,6 +161,21 @@ public class TbMsgPushToEdgeNode implements TbNode {
return edgeEvent;
}
private JsonNode getAttributeEntityBody(ActionType actionType, JsonNode data, Map<String, String> metadata) throws JsonProcessingException {
Map<String, Object> entityData = new HashMap<>();
switch (actionType) {
case ATTRIBUTES_UPDATED:
entityData.put("kv", data);
break;
case ATTRIBUTES_DELETED:
List<String> keys = json.treeToValue(data.get("attributes"), List.class);
entityData.put("keys", keys);
break;
}
entityData.put("scope", metadata.get("scope"));
return json.valueToTree(entityData);
}
private UUID getUUIDFromMsgData(TbMsg msg) throws JsonProcessingException {
JsonNode data = json.readTree(msg.getData()).get("id");
String id = json.treeToValue(data.get("id"), String.class);