Search in sources :

Example 1 with AttributeDeleteMsg

use of org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg in project thingsboard by thingsboard.

the class EntityDataMsgConstructor method constructEntityDataMsg.

public EntityDataProto constructEntityDataMsg(EntityId entityId, EdgeEventActionType actionType, JsonElement entityData) {
    EntityDataProto.Builder builder = EntityDataProto.newBuilder().setEntityIdMSB(entityId.getId().getMostSignificantBits()).setEntityIdLSB(entityId.getId().getLeastSignificantBits()).setEntityType(entityId.getEntityType().name());
    switch(actionType) {
        case TIMESERIES_UPDATED:
            try {
                JsonObject data = entityData.getAsJsonObject();
                long ts;
                if (data.get("ts") != null && !data.get("ts").isJsonNull()) {
                    ts = data.getAsJsonPrimitive("ts").getAsLong();
                } else {
                    ts = System.currentTimeMillis();
                }
                builder.setPostTelemetryMsg(JsonConverter.convertToTelemetryProto(data.getAsJsonObject("data"), ts));
            } catch (Exception e) {
                log.warn("[{}] Can't convert to telemetry proto, entityData [{}]", entityId, entityData, e);
            }
            break;
        case ATTRIBUTES_UPDATED:
            try {
                JsonObject data = entityData.getAsJsonObject();
                TransportProtos.PostAttributeMsg attributesUpdatedMsg = JsonConverter.convertToAttributesProto(data.getAsJsonObject("kv"));
                builder.setAttributesUpdatedMsg(attributesUpdatedMsg);
                builder.setPostAttributeScope(data.getAsJsonPrimitive("scope").getAsString());
            } catch (Exception e) {
                log.warn("[{}] Can't convert to AttributesUpdatedMsg proto, entityData [{}]", entityId, entityData, e);
            }
            break;
        case POST_ATTRIBUTES:
            try {
                JsonObject data = entityData.getAsJsonObject();
                TransportProtos.PostAttributeMsg postAttributesMsg = JsonConverter.convertToAttributesProto(data.getAsJsonObject("kv"));
                builder.setPostAttributesMsg(postAttributesMsg);
                builder.setPostAttributeScope(data.getAsJsonPrimitive("scope").getAsString());
            } catch (Exception e) {
                log.warn("[{}] Can't convert to PostAttributesMsg, entityData [{}]", entityId, entityData, e);
            }
            break;
        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(), new TypeToken<>() {
                }.getType());
                attributeDeleteMsg.addAllAttributeNames(keys);
                attributeDeleteMsg.build();
                builder.setAttributeDeleteMsg(attributeDeleteMsg);
            } catch (Exception e) {
                log.warn("[{}] Can't convert to AttributeDeleteMsg proto, entityData [{}]", entityId, entityData, e);
            }
            break;
    }
    return builder.build();
}
Also used : EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) JsonArray(com.google.gson.JsonArray) AttributeDeleteMsg(org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg) TypeToken(com.google.gson.reflect.TypeToken)

Example 2 with AttributeDeleteMsg

use of org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg in project thingsboard by thingsboard.

the class BaseEdgeTest method testAttributesDeleteMsg.

private void testAttributesDeleteMsg(Device device) throws JsonProcessingException, InterruptedException {
    String deleteAttributesData = "{\"scope\":\"SERVER_SCOPE\",\"keys\":[\"key1\",\"key2\"]}";
    JsonNode deleteAttributesEntityData = mapper.readTree(deleteAttributesData);
    EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.ATTRIBUTES_DELETED, device.getId().getId(), EdgeEventType.DEVICE, deleteAttributesEntityData);
    edgeImitator.expectMessageAmount(1);
    edgeEventService.save(edgeEvent);
    clusterService.onEdgeEventUpdate(tenantId, edge.getId());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityDataProto);
    EntityDataProto latestEntityDataMsg = (EntityDataProto) latestMessage;
    Assert.assertEquals(device.getUuidId().getMostSignificantBits(), latestEntityDataMsg.getEntityIdMSB());
    Assert.assertEquals(device.getUuidId().getLeastSignificantBits(), latestEntityDataMsg.getEntityIdLSB());
    Assert.assertEquals(device.getId().getEntityType().name(), latestEntityDataMsg.getEntityType());
    Assert.assertTrue(latestEntityDataMsg.hasAttributeDeleteMsg());
    AttributeDeleteMsg attributeDeleteMsg = latestEntityDataMsg.getAttributeDeleteMsg();
    Assert.assertEquals(attributeDeleteMsg.getScope(), deleteAttributesEntityData.get("scope").asText());
    Assert.assertEquals(2, attributeDeleteMsg.getAttributeNamesCount());
    Assert.assertEquals("key1", attributeDeleteMsg.getAttributeNames(0));
    Assert.assertEquals("key2", attributeDeleteMsg.getAttributeNames(1));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) AttributeDeleteMsg(org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg) JsonNode(com.fasterxml.jackson.databind.JsonNode) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Aggregations

AttributeDeleteMsg (org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg)2 EntityDataProto (org.thingsboard.server.gen.edge.v1.EntityDataProto)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 TypeToken (com.google.gson.reflect.TypeToken)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 EdgeEvent (org.thingsboard.server.common.data.edge.EdgeEvent)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1