Search in sources :

Example 16 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDashboardControllerTest method testAssignDashboardToEdge.

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

Example 17 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testFindTenantDevices.

@Test
public void testFindTenantDevices() throws Exception {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 178; i++) {
        Device device = new Device();
        device.setName("Device" + i);
        device.setType("default");
        devices.add(doPost("/api/device", device, Device.class));
    }
    List<Device> loadedDevices = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<Device> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/devices?", new TypeReference<PageData<Device>>() {
        }, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 18 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testFindTenantDevicesByName.

@Test
public void testFindTenantDevicesByName() throws Exception {
    String title1 = "Device title 1";
    List<Device> devicesTitle1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Device device = new Device();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType("default");
        devicesTitle1.add(doPost("/api/device", device, Device.class));
    }
    String title2 = "Device title 2";
    List<Device> devicesTitle2 = new ArrayList<>();
    for (int i = 0; i < 75; i++) {
        Device device = new Device();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType("default");
        devicesTitle2.add(doPost("/api/device", device, Device.class));
    }
    List<Device> loadedDevicesTitle1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15, 0, title1);
    PageData<Device> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/devices?", new TypeReference<PageData<Device>>() {
        }, pageLink);
        loadedDevicesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle1, idComparator);
    Collections.sort(loadedDevicesTitle1, idComparator);
    Assert.assertEquals(devicesTitle1, loadedDevicesTitle1);
    List<Device> loadedDevicesTitle2 = new ArrayList<>();
    pageLink = new PageLink(4, 0, title2);
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/devices?", new TypeReference<PageData<Device>>() {
        }, pageLink);
        loadedDevicesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle2, idComparator);
    Collections.sort(loadedDevicesTitle2, idComparator);
    Assert.assertEquals(devicesTitle2, loadedDevicesTitle2);
    for (Device device : loadedDevicesTitle1) {
        doDelete("/api/device/" + device.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title1);
    pageData = doGetTypedWithPageLink("/api/tenant/devices?", new TypeReference<PageData<Device>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesTitle2) {
        doDelete("/api/device/" + device.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title2);
    pageData = doGetTypedWithPageLink("/api/tenant/devices?", new TypeReference<PageData<Device>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Device(org.thingsboard.server.common.data.Device) 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) Test(org.junit.Test)

Example 19 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testFindTenantDevicesByType.

@Test
public void testFindTenantDevicesByType() throws Exception {
    String title1 = "Device title 1";
    String type1 = "typeA";
    List<Device> devicesType1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Device device = new Device();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type1);
        devicesType1.add(doPost("/api/device", device, Device.class));
    }
    String title2 = "Device title 2";
    String type2 = "typeB";
    List<Device> devicesType2 = new ArrayList<>();
    for (int i = 0; i < 75; i++) {
        Device device = new Device();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type2);
        devicesType2.add(doPost("/api/device", device, Device.class));
    }
    List<Device> loadedDevicesType1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15);
    PageData<Device> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&", new TypeReference<PageData<Device>>() {
        }, pageLink, type1);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new PageLink(4);
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&", new TypeReference<PageData<Device>>() {
        }, pageLink, type2);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        doDelete("/api/device/" + device.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4);
    pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&", new TypeReference<PageData<Device>>() {
    }, pageLink, type1);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        doDelete("/api/device/" + device.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4);
    pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&", new TypeReference<PageData<Device>>() {
    }, pageLink, type2);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Device(org.thingsboard.server.common.data.Device) 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) Test(org.junit.Test)

Example 20 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class TbDeviceProfileNode method init.

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    this.config = TbNodeUtils.convert(configuration, TbDeviceProfileNodeConfiguration.class);
    this.cache = ctx.getDeviceProfileCache();
    this.ctx = ctx;
    scheduleAlarmHarvesting(ctx, null);
    ctx.addDeviceProfileListeners(this::onProfileUpdate, this::onDeviceUpdate);
    if (config.isFetchAlarmRulesStateOnStart()) {
        log.info("[{}] Fetching alarm rule state", ctx.getSelfId());
        int fetchCount = 0;
        PageLink pageLink = new PageLink(1024);
        while (true) {
            PageData<RuleNodeState> states = ctx.findRuleNodeStates(pageLink);
            if (!states.getData().isEmpty()) {
                for (RuleNodeState rns : states.getData()) {
                    fetchCount++;
                    if (rns.getEntityId().getEntityType().equals(EntityType.DEVICE) && ctx.isLocalEntity(rns.getEntityId())) {
                        getOrCreateDeviceState(ctx, new DeviceId(rns.getEntityId().getId()), rns);
                    }
                }
            }
            if (!states.hasNext()) {
                break;
            } else {
                pageLink = pageLink.nextPageLink();
            }
        }
        log.info("[{}] Fetched alarm rule state for {} entities", ctx.getSelfId(), fetchCount);
    }
    if (!config.isPersistAlarmRulesState() && ctx.isLocalEntity(ctx.getSelfId())) {
        log.debug("[{}] Going to cleanup rule node states", ctx.getSelfId());
        ctx.clearRuleNodeStates();
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) PageLink(org.thingsboard.server.common.data.page.PageLink) RuleNodeState(org.thingsboard.server.common.data.rule.RuleNodeState)

Aggregations

PageLink (org.thingsboard.server.common.data.page.PageLink)187 Test (org.junit.Test)117 ArrayList (java.util.ArrayList)106 TenantId (org.thingsboard.server.common.data.id.TenantId)67 TypeReference (com.fasterxml.jackson.core.type.TypeReference)54 CustomerId (org.thingsboard.server.common.data.id.CustomerId)47 Tenant (org.thingsboard.server.common.data.Tenant)45 Customer (org.thingsboard.server.common.data.Customer)43 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)34 ApiOperation (io.swagger.annotations.ApiOperation)33 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)33 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)33 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)33 Edge (org.thingsboard.server.common.data.edge.Edge)32 Device (org.thingsboard.server.common.data.Device)28 PageData (org.thingsboard.server.common.data.page.PageData)28 Matchers.containsString (org.hamcrest.Matchers.containsString)22 UUID (java.util.UUID)20 Asset (org.thingsboard.server.common.data.asset.Asset)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20