Search in sources :

Example 1 with EntityDataProto

use of org.thingsboard.server.gen.edge.v1.EntityDataProto 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 EntityDataProto

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

the class BaseEdgeTest method testTimeseries.

@Test
public void testTimeseries() throws Exception {
    edgeImitator.expectMessageAmount(1);
    Device device = findDeviceByName("Edge Device 1");
    String timeseriesData = "{\"data\":{\"temperature\":25},\"ts\":" + System.currentTimeMillis() + "}";
    JsonNode timeseriesEntityData = mapper.readTree(timeseriesData);
    EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.TIMESERIES_UPDATED, device.getId().getId(), EdgeEventType.DEVICE, timeseriesEntityData);
    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(latestEntityDataMsg.getEntityIdMSB(), device.getUuidId().getMostSignificantBits());
    Assert.assertEquals(latestEntityDataMsg.getEntityIdLSB(), device.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(latestEntityDataMsg.getEntityType(), device.getId().getEntityType().name());
    Assert.assertTrue(latestEntityDataMsg.hasPostTelemetryMsg());
    TransportProtos.PostTelemetryMsg postTelemetryMsg = latestEntityDataMsg.getPostTelemetryMsg();
    Assert.assertEquals(1, postTelemetryMsg.getTsKvListCount());
    TransportProtos.TsKvListProto tsKvListProto = postTelemetryMsg.getTsKvList(0);
    Assert.assertEquals(timeseriesEntityData.get("ts").asLong(), tsKvListProto.getTs());
    Assert.assertEquals(1, tsKvListProto.getKvCount());
    TransportProtos.KeyValueProto keyValueProto = tsKvListProto.getKv(0);
    Assert.assertEquals("temperature", keyValueProto.getKey());
    Assert.assertEquals(25, keyValueProto.getLongV());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) Device(org.thingsboard.server.common.data.Device) JsonNode(com.fasterxml.jackson.databind.JsonNode) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 3 with EntityDataProto

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

the class BaseEdgeTest method testPostAttributesMsg.

private void testPostAttributesMsg(Device device) throws JsonProcessingException, InterruptedException {
    String postAttributesData = "{\"scope\":\"SERVER_SCOPE\",\"kv\":{\"key2\":\"value2\"}}";
    JsonNode postAttributesEntityData = mapper.readTree(postAttributesData);
    EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.POST_ATTRIBUTES, device.getId().getId(), EdgeEventType.DEVICE, postAttributesEntityData);
    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.assertEquals("SERVER_SCOPE", latestEntityDataMsg.getPostAttributeScope());
    Assert.assertTrue(latestEntityDataMsg.hasPostAttributesMsg());
    TransportProtos.PostAttributeMsg postAttributesMsg = latestEntityDataMsg.getPostAttributesMsg();
    Assert.assertEquals(1, postAttributesMsg.getKvCount());
    TransportProtos.KeyValueProto keyValueProto = postAttributesMsg.getKv(0);
    Assert.assertEquals("key2", keyValueProto.getKey());
    Assert.assertEquals("value2", keyValueProto.getStringV());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) JsonNode(com.fasterxml.jackson.databind.JsonNode) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 4 with EntityDataProto

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

the class BaseEdgeTest method sendAttributesRequestAndVerify.

private void sendAttributesRequestAndVerify(Device device, String scope, String attributesDataStr, String expectedKey, String expectedValue) throws Exception {
    JsonNode attributesData = mapper.readTree(attributesDataStr);
    doPost("/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/attributes/" + scope, attributesData);
    // Wait before device attributes saved to database before requesting them from edge
    Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
        String urlTemplate = "/api/plugins/telemetry/DEVICE/" + device.getId() + "/keys/attributes/" + scope;
        List<String> actualKeys = doGetAsyncTyped(urlTemplate, new TypeReference<>() {
        });
        return actualKeys != null && !actualKeys.isEmpty() && actualKeys.contains(expectedKey);
    });
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    AttributesRequestMsg.Builder attributesRequestMsgBuilder = AttributesRequestMsg.newBuilder();
    attributesRequestMsgBuilder.setEntityIdMSB(device.getUuidId().getMostSignificantBits());
    attributesRequestMsgBuilder.setEntityIdLSB(device.getUuidId().getLeastSignificantBits());
    attributesRequestMsgBuilder.setEntityType(EntityType.DEVICE.name());
    attributesRequestMsgBuilder.setScope(scope);
    testAutoGeneratedCodeByProtobuf(attributesRequestMsgBuilder);
    uplinkMsgBuilder.addAttributesRequestMsg(attributesRequestMsgBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    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.assertEquals(scope, latestEntityDataMsg.getPostAttributeScope());
    Assert.assertTrue(latestEntityDataMsg.hasAttributesUpdatedMsg());
    TransportProtos.PostAttributeMsg attributesUpdatedMsg = latestEntityDataMsg.getAttributesUpdatedMsg();
    boolean found = false;
    for (TransportProtos.KeyValueProto keyValueProto : attributesUpdatedMsg.getKvList()) {
        if (keyValueProto.getKey().equals(expectedKey)) {
            Assert.assertEquals(expectedKey, keyValueProto.getKey());
            Assert.assertEquals(expectedValue, keyValueProto.getStringV());
            found = true;
        }
    }
    Assert.assertTrue("Expected key and value must be found", found);
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) JsonNode(com.fasterxml.jackson.databind.JsonNode) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) AttributesRequestMsg(org.thingsboard.server.gen.edge.v1.AttributesRequestMsg)

Example 5 with EntityDataProto

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

the class BaseEdgeTest method testTimeseriesWithFailures.

@Test
public void testTimeseriesWithFailures() throws Exception {
    int numberOfTimeseriesToSend = 1000;
    edgeImitator.setRandomFailuresOnTimeseriesDownlink(true);
    // imitator will generate failure in 5% of cases
    edgeImitator.setFailureProbability(5.0);
    edgeImitator.expectMessageAmount(numberOfTimeseriesToSend);
    Device device = findDeviceByName("Edge Device 1");
    for (int idx = 1; idx <= numberOfTimeseriesToSend; idx++) {
        String timeseriesData = "{\"data\":{\"idx\":" + idx + "},\"ts\":" + System.currentTimeMillis() + "}";
        JsonNode timeseriesEntityData = mapper.readTree(timeseriesData);
        EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.TIMESERIES_UPDATED, device.getId().getId(), EdgeEventType.DEVICE, timeseriesEntityData);
        edgeEventService.save(edgeEvent);
        clusterService.onEdgeEventUpdate(tenantId, edge.getId());
    }
    Assert.assertTrue(edgeImitator.waitForMessages(120));
    List<EntityDataProto> allTelemetryMsgs = edgeImitator.findAllMessagesByType(EntityDataProto.class);
    Assert.assertEquals(numberOfTimeseriesToSend, allTelemetryMsgs.size());
    for (int idx = 1; idx <= numberOfTimeseriesToSend; idx++) {
        Assert.assertTrue(isIdxExistsInTheDownlinkList(idx, allTelemetryMsgs));
    }
    edgeImitator.setRandomFailuresOnTimeseriesDownlink(false);
}
Also used : EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) Device(org.thingsboard.server.common.data.Device) JsonNode(com.fasterxml.jackson.databind.JsonNode) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Aggregations

EntityDataProto (org.thingsboard.server.gen.edge.v1.EntityDataProto)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)6 AbstractMessage (com.google.protobuf.AbstractMessage)5 EdgeEvent (org.thingsboard.server.common.data.edge.EdgeEvent)5 Test (org.junit.Test)2 Device (org.thingsboard.server.common.data.Device)2 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)2 AttributeDeleteMsg (org.thingsboard.server.gen.edge.v1.AttributeDeleteMsg)2 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 AttributesRequestMsg (org.thingsboard.server.gen.edge.v1.AttributesRequestMsg)1 UplinkMsg (org.thingsboard.server.gen.edge.v1.UplinkMsg)1