Search in sources :

Example 21 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class JpaDeviceDaoTest method testFindDevicesByTenantIdAndIdsAsync.

@Test
public void testFindDevicesByTenantIdAndIdsAsync() throws ExecutionException, InterruptedException {
    UUID tenantId1 = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    List<UUID> deviceIds = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        UUID deviceId1 = UUIDs.timeBased();
        UUID deviceId2 = UUIDs.timeBased();
        deviceDao.save(getDevice(tenantId1, customerId1, deviceId1));
        deviceDao.save(getDevice(tenantId2, customerId2, deviceId2));
        deviceIds.add(deviceId1);
        deviceIds.add(deviceId2);
    }
    ListenableFuture<List<Device>> devicesFuture = deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId1, deviceIds);
    List<Device> devices = devicesFuture.get();
    assertEquals(5, devices.size());
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 22 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class JpaDeviceDaoTest method testFindDevicesByTenantIdAndCustomerIdAndIdsAsync.

@Test
public void testFindDevicesByTenantIdAndCustomerIdAndIdsAsync() throws ExecutionException, InterruptedException {
    UUID tenantId1 = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    List<UUID> deviceIds = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        UUID deviceId1 = UUIDs.timeBased();
        UUID deviceId2 = UUIDs.timeBased();
        deviceDao.save(getDevice(tenantId1, customerId1, deviceId1));
        deviceDao.save(getDevice(tenantId2, customerId2, deviceId2));
        deviceIds.add(deviceId1);
        deviceIds.add(deviceId2);
    }
    ListenableFuture<List<Device>> devicesFuture = deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId1, customerId1, deviceIds);
    List<Device> devices = devicesFuture.get();
    assertEquals(20, devices.size());
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 23 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class JpaDeviceDaoTest method getDevice.

private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId) {
    Device device = new Device();
    device.setId(new DeviceId(deviceId));
    device.setTenantId(new TenantId(tenantId));
    device.setCustomerId(new CustomerId(customerID));
    device.setName("SEARCH_TEXT");
    return device;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Device(org.thingsboard.server.common.data.Device) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 24 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class JpaDeviceDaoTest method testFindDevicesByTenantId.

@Test
public void testFindDevicesByTenantId() {
    UUID tenantId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    createDevices(tenantId1, tenantId2, customerId1, customerId2, 40);
    TextPageLink pageLink1 = new TextPageLink(15, "SEARCH_TEXT");
    List<Device> devices1 = deviceDao.findDevicesByTenantId(tenantId1, pageLink1);
    assertEquals(15, devices1.size());
    TextPageLink pageLink2 = new TextPageLink(15, "SEARCH_TEXT", devices1.get(14).getId().getId(), null);
    List<Device> devices2 = deviceDao.findDevicesByTenantId(tenantId1, pageLink2);
    assertEquals(5, devices2.size());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Device(org.thingsboard.server.common.data.Device) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 25 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class DeviceMessagingRuleMsgHandler method processSendMsg.

private void processSendMsg(PluginContext ctx, PendingRpcRequestMetadata requestMd, ToServerRpcRequestMsg request) {
    JsonObject params = new JsonParser().parse(request.getParams()).getAsJsonObject();
    String targetDeviceIdStr = params.get(DEVICE_ID).getAsString();
    DeviceId targetDeviceId = DeviceId.fromString(targetDeviceIdStr);
    boolean oneWay = isOneWay(params);
    long timeout = getTimeout(params);
    if (timeout <= 0) {
        replyWithError(ctx, requestMd, "Timeout can't be negative!");
    } else if (timeout > configuration.getMaxTimeout()) {
        replyWithError(ctx, requestMd, "Timeout is too large!");
    } else {
        ctx.getDevice(targetDeviceId, new PluginCallback<Device>() {

            @Override
            public void onSuccess(PluginContext ctx, Device targetDevice) {
                UUID uid = UUID.randomUUID();
                if (targetDevice == null) {
                    replyWithError(ctx, requestMd, RpcError.NOT_FOUND);
                } else if (!requestMd.getCustomerId().isNullUid() && requestMd.getTenantId().equals(targetDevice.getTenantId()) && requestMd.getCustomerId().equals(targetDevice.getCustomerId())) {
                    pendingMsgs.put(uid, requestMd);
                    log.trace("[{}] Forwarding {} to [{}]", uid, params, targetDeviceId);
                    ToDeviceRpcRequestBody requestBody = new ToDeviceRpcRequestBody(ON_MSG_METHOD_NAME, GSON.toJson(params.get("body")));
                    ctx.sendRpcRequest(new ToDeviceRpcRequest(uid, null, targetDevice.getTenantId(), targetDeviceId, oneWay, System.currentTimeMillis() + timeout, requestBody));
                } else {
                    replyWithError(ctx, requestMd, RpcError.FORBIDDEN);
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                replyWithError(ctx, requestMd, RpcError.INTERNAL);
            }
        });
    }
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) JsonObject(com.google.gson.JsonObject) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) JsonParser(com.google.gson.JsonParser) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Aggregations

Device (org.thingsboard.server.common.data.Device)53 Test (org.junit.Test)31 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)18 ArrayList (java.util.ArrayList)11 DeviceId (org.thingsboard.server.common.data.id.DeviceId)11 CustomerId (org.thingsboard.server.common.data.id.CustomerId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Customer (org.thingsboard.server.common.data.Customer)7 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)7 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 TenantId (org.thingsboard.server.common.data.id.TenantId)6 UUID (java.util.UUID)5 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 Tenant (org.thingsboard.server.common.data.Tenant)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 List (java.util.List)2 Before (org.junit.Before)2