Search in sources :

Example 16 with Device

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

the class AbstractMqttServerSideRpcIntegrationTest method testServerMqttTwoWayRpcDeviceOffline.

@Test
public void testServerMqttTwoWayRpcDeviceOffline() throws Exception {
    Device device = new Device();
    device.setName("Test Two-Way Server-Side RPC Device Offline");
    device.setType("default");
    Device savedDevice = getSavedDevice(device);
    DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
    assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    String accessToken = deviceCredentials.getCredentialsId();
    assertNotNull(accessToken);
    String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
    String deviceId = savedDevice.getId().getId().toString();
    doPostAsync("/api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class, status().isRequestTimeout(), asyncContextTimeoutToUseRpcPlugin);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest)

Example 17 with Device

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

the class AbstractMqttServerSideRpcIntegrationTest method testServerMqttOneWayRpc.

@Test
public void testServerMqttOneWayRpc() throws Exception {
    Device device = new Device();
    device.setName("Test One-Way Server-Side RPC");
    device.setType("default");
    Device savedDevice = getSavedDevice(device);
    DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
    assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    String accessToken = deviceCredentials.getCredentialsId();
    assertNotNull(accessToken);
    String clientId = MqttAsyncClient.generateClientId();
    MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName(accessToken);
    client.connect(options).waitForCompletion();
    client.subscribe("v1/devices/me/rpc/request/+", 1);
    client.setCallback(new TestMqttCallback(client));
    String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
    String deviceId = savedDevice.getId().getId().toString();
    String result = doPostAsync("/api/plugins/rpc/oneway/" + deviceId, setGpioRequest, String.class, status().isOk());
    Assert.assertTrue(StringUtils.isEmpty(result));
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest)

Example 18 with Device

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

the class AbstractMqttServerSideRpcIntegrationTest method testServerMqttTwoWayRpc.

@Test
public void testServerMqttTwoWayRpc() throws Exception {
    Device device = new Device();
    device.setName("Test Two-Way Server-Side RPC");
    device.setType("default");
    Device savedDevice = getSavedDevice(device);
    DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
    assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    String accessToken = deviceCredentials.getCredentialsId();
    assertNotNull(accessToken);
    String clientId = MqttAsyncClient.generateClientId();
    MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName(accessToken);
    client.connect(options).waitForCompletion();
    client.subscribe("v1/devices/me/rpc/request/+", 1);
    client.setCallback(new TestMqttCallback(client));
    String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
    String deviceId = savedDevice.getId().getId().toString();
    String result = doPostAsync("/api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class, status().isOk());
    Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest)

Example 19 with Device

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

the class BaseHttpDeviceApiTest method before.

@Before
public void before() throws Exception {
    loginTenantAdmin();
    device = new Device();
    device.setName("My device");
    device.setType("default");
    device = doPost("/api/device", device, Device.class);
    deviceCredentials = doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Before(org.junit.Before)

Example 20 with Device

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

the class JpaDeviceDaoTest method testFindAsync.

@Test
public void testFindAsync() throws ExecutionException, InterruptedException {
    UUID tenantId = UUIDs.timeBased();
    UUID customerId = UUIDs.timeBased();
    Device device = getDevice(tenantId, customerId);
    deviceDao.save(device);
    UUID uuid = device.getId().getId();
    Device entity = deviceDao.findById(uuid);
    assertNotNull(entity);
    assertEquals(uuid, entity.getId().getId());
    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
    ListenableFuture<Device> future = service.submit(() -> deviceDao.findById(uuid));
    Device asyncDevice = future.get();
    assertNotNull("Async device expected to be not null", asyncDevice);
}
Also used : Device(org.thingsboard.server.common.data.Device) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

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