Search in sources :

Example 51 with Edge

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

the class CustomerEdgeProcessor method processCustomerNotification.

public void processCustomerNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
    UUID uuid = new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB());
    CustomerId customerId = new CustomerId(EntityIdFactory.getByEdgeEventTypeAndUuid(type, uuid).getId());
    switch(actionType) {
        case UPDATED:
            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
            PageData<Edge> pageData;
            do {
                pageData = edgeService.findEdgesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                    for (Edge edge : pageData.getData()) {
                        saveEdgeEvent(tenantId, edge.getId(), type, actionType, customerId, null);
                    }
                    if (pageData.hasNext()) {
                        pageLink = pageLink.nextPageLink();
                    }
                }
            } while (pageData != null && pageData.hasNext());
            break;
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            saveEdgeEvent(tenantId, edgeId, type, actionType, customerId, null);
            break;
    }
}
Also used : EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 52 with Edge

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

the class BaseEdgeProcessor method processActionForAllEdges.

protected void processActionForAllEdges(TenantId tenantId, EdgeEventType type, EdgeEventActionType actionType, EntityId entityId) {
    PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
    PageData<Edge> pageData;
    do {
        pageData = edgeService.findEdgesByTenantId(tenantId, pageLink);
        if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
            for (Edge edge : pageData.getData()) {
                saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, null);
            }
            if (pageData.hasNext()) {
                pageLink = pageLink.nextPageLink();
            }
        }
    } while (pageData != null && pageData.hasNext());
}
Also used : PageLink(org.thingsboard.server.common.data.page.PageLink) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 53 with Edge

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

the class BaseDeviceControllerTest method testAssignDeviceToEdge.

@Test
public void testAssignDeviceToEdge() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    doPost("/api/edge/" + savedEdge.getId().getId().toString() + "/device/" + savedDevice.getId().getId().toString(), Device.class);
    PageData<Device> pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId().toString() + "/devices?", new TypeReference<PageData<Device>>() {
    }, new PageLink(100));
    Assert.assertEquals(1, pageData.getData().size());
    doDelete("/api/edge/" + savedEdge.getId().getId().toString() + "/device/" + savedDevice.getId().getId().toString(), Device.class);
    pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId().toString() + "/devices?", new TypeReference<PageData<Device>>() {
    }, new PageLink(100));
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) Device(org.thingsboard.server.common.data.Device) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 54 with Edge

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

the class BaseEdgeControllerTest method testSaveEdgeWithEmptyType.

@Test
public void testSaveEdgeWithEmptyType() throws Exception {
    Edge edge = constructEdge("My edge", null);
    doPost("/api/edge", edge).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Edge type should be specified")));
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 55 with Edge

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

the class BaseEdgeControllerTest method testFindTenantEdgesByType.

@Test
public void testFindTenantEdgesByType() throws Exception {
    String title1 = "Edge title 1";
    String type1 = "typeA";
    List<Edge> edgesType1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, type1);
        edgesType1.add(doPost("/api/edge", edge, Edge.class));
    }
    String title2 = "Edge title 2";
    String type2 = "typeB";
    List<Edge> edgesType2 = new ArrayList<>();
    for (int i = 0; i < 75; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, type2);
        edgesType2.add(doPost("/api/edge", edge, Edge.class));
    }
    List<Edge> loadedEdgesType1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15);
    PageData<Edge> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/edges?type={type}&", new TypeReference<PageData<Edge>>() {
        }, pageLink, type1);
        loadedEdgesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesType1, idComparator);
    Collections.sort(loadedEdgesType1, idComparator);
    Assert.assertEquals(edgesType1, loadedEdgesType1);
    List<Edge> loadedEdgesType2 = new ArrayList<>();
    pageLink = new PageLink(4);
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/edges?type={type}&", new TypeReference<PageData<Edge>>() {
        }, pageLink, type2);
        loadedEdgesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesType2, idComparator);
    Collections.sort(loadedEdgesType2, idComparator);
    Assert.assertEquals(edgesType2, loadedEdgesType2);
    for (Edge edge : loadedEdgesType1) {
        doDelete("/api/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4);
    pageData = doGetTypedWithPageLink("/api/tenant/edges?type={type}&", new TypeReference<PageData<Edge>>() {
    }, pageLink, type1);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Edge edge : loadedEdgesType2) {
        doDelete("/api/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4);
    pageData = doGetTypedWithPageLink("/api/tenant/edges?type={type}&", new TypeReference<PageData<Edge>>() {
    }, pageLink, type2);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13