Search in sources :

Example 1 with EdgeEvent

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

the class BaseEdgeEventControllerTest method testGetEdgeEvents.

@Test
public void testGetEdgeEvents() throws Exception {
    Edge edge = constructEdge("TestEdge", "default");
    edge = doPost("/api/edge", edge, Edge.class);
    Device device = constructDevice("TestDevice", "default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    final EdgeId edgeId = edge.getId();
    doPost("/api/edge/" + edgeId.toString() + "/device/" + savedDevice.getId().toString(), Device.class);
    Asset asset = constructAsset("TestAsset", "default");
    Asset savedAsset = doPost("/api/asset", asset, Asset.class);
    doPost("/api/edge/" + edgeId.toString() + "/asset/" + savedAsset.getId().toString(), Asset.class);
    EntityRelation relation = new EntityRelation(savedAsset.getId(), savedDevice.getId(), EntityRelation.CONTAINS_TYPE);
    doPost("/api/relation", relation);
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> {
        List<EdgeEvent> edgeEvents = findEdgeEvents(edgeId);
        return edgeEvents.size() == 4;
    });
    List<EdgeEvent> edgeEvents = findEdgeEvents(edgeId);
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.RULE_CHAIN.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.DEVICE.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.ASSET.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.RELATION.equals(ee.getType())));
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) Edge(org.thingsboard.server.common.data.edge.Edge) Device(org.thingsboard.server.common.data.Device) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Tenant(org.thingsboard.server.common.data.Tenant) Test(org.junit.Test) Authority(org.thingsboard.server.common.data.security.Authority) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TimeUnit(java.util.concurrent.TimeUnit) User(org.thingsboard.server.common.data.User) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) After(org.junit.After) PageData(org.thingsboard.server.common.data.page.PageData) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert(org.junit.Assert) Awaitility(org.awaitility.Awaitility) Before(org.junit.Before) Asset(org.thingsboard.server.common.data.asset.Asset) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Device(org.thingsboard.server.common.data.Device) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Asset(org.thingsboard.server.common.data.asset.Asset) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 2 with EdgeEvent

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

the class AdminSettingsEdgeEventFetcher method fetchEdgeEvents.

@Override
public PageData<EdgeEvent> fetchEdgeEvents(TenantId tenantId, Edge edge, PageLink pageLink) throws Exception {
    List<EdgeEvent> result = new ArrayList<>();
    AdminSettings systemMailSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "mail");
    result.add(EdgeEventUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.ADMIN_SETTINGS, EdgeEventActionType.UPDATED, null, mapper.valueToTree(systemMailSettings)));
    AdminSettings tenantMailSettings = convertToTenantAdminSettings(systemMailSettings.getKey(), (ObjectNode) systemMailSettings.getJsonValue());
    result.add(EdgeEventUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.ADMIN_SETTINGS, EdgeEventActionType.UPDATED, null, mapper.valueToTree(tenantMailSettings)));
    AdminSettings systemMailTemplates = loadMailTemplates();
    result.add(EdgeEventUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.ADMIN_SETTINGS, EdgeEventActionType.UPDATED, null, mapper.valueToTree(systemMailTemplates)));
    AdminSettings tenantMailTemplates = convertToTenantAdminSettings(systemMailTemplates.getKey(), (ObjectNode) systemMailTemplates.getJsonValue());
    result.add(EdgeEventUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.ADMIN_SETTINGS, EdgeEventActionType.UPDATED, null, mapper.valueToTree(tenantMailTemplates)));
    // @voba - returns PageData object to be in sync with other fetchers
    return new PageData<>(result, 1, result.size(), false);
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) ArrayList(java.util.ArrayList) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) AdminSettings(org.thingsboard.server.common.data.AdminSettings)

Example 3 with EdgeEvent

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

the class EdgeEventEntity method toData.

@Override
public EdgeEvent toData() {
    EdgeEvent edgeEvent = new EdgeEvent(new EdgeEventId(this.getUuid()));
    edgeEvent.setCreatedTime(createdTime);
    edgeEvent.setTenantId(TenantId.fromUUID(tenantId));
    edgeEvent.setEdgeId(new EdgeId(edgeId));
    if (entityId != null) {
        edgeEvent.setEntityId(entityId);
    }
    edgeEvent.setType(edgeEventType);
    edgeEvent.setAction(edgeEventAction);
    edgeEvent.setBody(entityBody);
    edgeEvent.setUid(edgeEventUid);
    return edgeEvent;
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) EdgeEventId(org.thingsboard.server.common.data.id.EdgeEventId) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 4 with EdgeEvent

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

the class TbMsgPushToEdgeNode method processMsg.

private void processMsg(TbContext ctx, TbMsg msg) {
    if (EntityType.EDGE.equals(msg.getOriginator().getEntityType())) {
        EdgeEvent edgeEvent = buildEdgeEvent(msg, ctx);
        if (edgeEvent != null) {
            EdgeId edgeId = new EdgeId(msg.getOriginator().getId());
            notifyEdge(ctx, msg, edgeEvent, edgeId);
        }
    } else {
        PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
        PageData<EdgeId> pageData;
        do {
            pageData = ctx.getEdgeService().findRelatedEdgeIdsByEntityId(ctx.getTenantId(), msg.getOriginator(), pageLink);
            if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                for (EdgeId edgeId : pageData.getData()) {
                    EdgeEvent edgeEvent = buildEdgeEvent(msg, ctx);
                    if (edgeEvent == null) {
                        log.debug("Edge event type is null. Entity Type {}", msg.getOriginator().getEntityType());
                        ctx.tellFailure(msg, new RuntimeException("Edge event type is null. Entity Type '" + msg.getOriginator().getEntityType() + "'"));
                    } else {
                        notifyEdge(ctx, msg, edgeEvent, edgeId);
                    }
                }
                if (pageData.hasNext()) {
                    pageLink = pageLink.nextPageLink();
                }
            }
        } while (pageData != null && pageData.hasNext());
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 5 with EdgeEvent

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

the class TbMsgPushToEdgeNode method buildEdgeEvent.

private EdgeEvent buildEdgeEvent(TenantId tenantId, EdgeEventActionType edgeEventAction, UUID entityId, EdgeEventType edgeEventType, JsonNode entityBody) {
    EdgeEvent edgeEvent = new EdgeEvent();
    edgeEvent.setTenantId(tenantId);
    edgeEvent.setAction(edgeEventAction);
    edgeEvent.setEntityId(entityId);
    edgeEvent.setType(edgeEventType);
    edgeEvent.setBody(entityBody);
    return edgeEvent;
}
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