Search in sources :

Example 11 with EdgeEvent

use of org.thingsboard.server.common.data.edge.EdgeEvent 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 12 with EdgeEvent

use of org.thingsboard.server.common.data.edge.EdgeEvent 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 13 with EdgeEvent

use of org.thingsboard.server.common.data.edge.EdgeEvent 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)

Example 14 with EdgeEvent

use of org.thingsboard.server.common.data.edge.EdgeEvent in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method saveRpcRequestToEdgeQueue.

private void saveRpcRequestToEdgeQueue(ToDeviceRpcRequest msg, Integer requestId) {
    EdgeEvent edgeEvent = new EdgeEvent();
    edgeEvent.setTenantId(tenantId);
    edgeEvent.setAction(EdgeEventActionType.RPC_CALL);
    edgeEvent.setEntityId(deviceId.getId());
    edgeEvent.setType(EdgeEventType.DEVICE);
    ObjectNode body = mapper.createObjectNode();
    body.put("requestId", requestId);
    body.put("requestUUID", msg.getId().toString());
    body.put("oneway", msg.isOneway());
    body.put("expirationTime", msg.getExpirationTime());
    body.put("method", msg.getBody().getMethod());
    body.put("params", msg.getBody().getParams());
    edgeEvent.setBody(body);
    edgeEvent.setEdgeId(edgeId);
    systemContext.getEdgeEventService().save(edgeEvent);
    systemContext.getClusterService().onEdgeEventUpdate(tenantId, edgeId);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 15 with EdgeEvent

use of org.thingsboard.server.common.data.edge.EdgeEvent in project thingsboard by thingsboard.

the class DefaultEdgeNotificationService method saveEdgeEvent.

private void saveEdgeEvent(TenantId tenantId, EdgeId edgeId, EdgeEventType type, EdgeEventActionType action, EntityId entityId, JsonNode body) {
    log.debug("Pushing edge event to edge queue. tenantId [{}], edgeId [{}], type [{}], action[{}], entityId [{}], body [{}]", tenantId, edgeId, type, action, entityId, body);
    EdgeEvent edgeEvent = new EdgeEvent();
    edgeEvent.setEdgeId(edgeId);
    edgeEvent.setTenantId(tenantId);
    edgeEvent.setType(type);
    edgeEvent.setAction(action);
    if (entityId != null) {
        edgeEvent.setEntityId(entityId.getId());
    }
    edgeEvent.setBody(body);
    edgeEventService.save(edgeEvent);
    clusterService.onEdgeEventUpdate(tenantId, edgeId);
}
Also used : EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Aggregations

EdgeEvent (org.thingsboard.server.common.data.edge.EdgeEvent)22 Test (org.junit.Test)7 EdgeId (org.thingsboard.server.common.data.id.EdgeId)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 AbstractMessage (com.google.protobuf.AbstractMessage)5 EntityDataProto (org.thingsboard.server.gen.edge.v1.EntityDataProto)5 Device (org.thingsboard.server.common.data.Device)4 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)3 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)3 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 EdgeEventId (org.thingsboard.server.common.data.id.EdgeEventId)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 PageData (org.thingsboard.server.common.data.page.PageData)2 SortOrder (org.thingsboard.server.common.data.page.SortOrder)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1