Search in sources :

Example 16 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method createDeviceClient.

private DeviceClient createDeviceClient(IotHubClientProtocol protocol, RegistryManager registryManager) throws IOException, IotHubException, URISyntaxException {
    ClientOptions options = new ClientOptions();
    options.setModelId(THERMOSTAT_MODEL_ID);
    String deviceId = "some-device-" + UUID.randomUUID();
    Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
    Device registeredDevice = registryManager.addDevice(device);
    String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
    return new DeviceClient(deviceConnectionString, protocol, options);
}
Also used : ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 17 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method invokeMethodSucceedWithTokenCredential.

@Test
public void invokeMethodSucceedWithTokenCredential() throws Exception {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    DeviceMethod methodServiceClient = buildDeviceMethodClientWithTokenCredential();
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
    registryManager.addDevice(device);
    DeviceClient deviceClient = new DeviceClient(registryManager.getDeviceConnectionString(device), MQTT);
    deviceClient.open();
    final int successStatusCode = 200;
    final AtomicBoolean methodsSubscriptionComplete = new AtomicBoolean(false);
    final AtomicBoolean methodsSubscribedSuccessfully = new AtomicBoolean(false);
    deviceClient.subscribeToDeviceMethod((methodName, methodData, context) -> new DeviceMethodData(successStatusCode, "success"), null, (responseStatus, callbackContext) -> {
        if (responseStatus == IotHubStatusCode.OK_EMPTY || responseStatus == IotHubStatusCode.OK) {
            methodsSubscribedSuccessfully.set(true);
        }
        methodsSubscriptionComplete.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!methodsSubscriptionComplete.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > METHOD_SUBSCRIPTION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Method subscription callback fired with non 200 status code", methodsSubscribedSuccessfully.get());
    MethodResult result = methodServiceClient.invoke(device.getDeviceId(), "someMethod", 5l, 5l, null);
    assertEquals((long) successStatusCode, (long) result.getStatus());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 18 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method testGetDeviceTwinWithTokenCredential.

@Test
public void testGetDeviceTwinWithTokenCredential() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, ModuleClientException, URISyntaxException {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    DeviceTwin twinServiceClient = buildDeviceTwinClientWithTokenCredential();
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
    registryManager.addDevice(device);
    DeviceTwinDevice twin = new DeviceTwinDevice(device.getDeviceId());
    twinServiceClient.getTwin(twin);
    assertNotNull(twin.getETag());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 19 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method deviceLifecycleWithTokenCredential.

@Test
public void deviceLifecycleWithTokenCredential() throws Exception {
    // -Create-//
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    String deviceId = "some-device-" + UUID.randomUUID();
    Device deviceAdded = Device.createFromId(deviceId, DeviceStatus.Enabled, null);
    registryManager.addDevice(deviceAdded);
    // -Read-//
    Device deviceRetrieved = registryManager.getDevice(deviceId);
    // -Update-//
    Device deviceUpdated = registryManager.getDevice(deviceId);
    deviceUpdated.setStatus(DeviceStatus.Disabled);
    deviceUpdated = registryManager.updateDevice(deviceUpdated);
    // -Delete-//
    registryManager.removeDevice(deviceId);
    // Assert
    assertEquals(deviceId, deviceAdded.getDeviceId());
    assertEquals(deviceId, deviceRetrieved.getDeviceId());
    assertEquals(DeviceStatus.Disabled, deviceUpdated.getStatus());
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 20 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method deviceLifecycle.

public static void deviceLifecycle(RegistryManagerTestInstance testInstance) throws Exception {
    // -Create-//
    Device deviceAdded = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
    Tools.addDeviceWithRetry(testInstance.registryManager, deviceAdded);
    // -Read-//
    Device deviceRetrieved = testInstance.registryManager.getDevice(testInstance.deviceId);
    // -Update-//
    Device deviceUpdated = testInstance.registryManager.getDevice(testInstance.deviceId);
    deviceUpdated.setStatus(DeviceStatus.Disabled);
    deviceUpdated = testInstance.registryManager.updateDevice(deviceUpdated);
    // -Delete-//
    testInstance.registryManager.removeDevice(testInstance.deviceId);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceAdded.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceRetrieved.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), DeviceStatus.Disabled, deviceUpdated.getStatus());
    assertTrue(buildExceptionMessage("", hostName), deviceWasDeletedSuccessfully(testInstance.registryManager, testInstance.deviceId));
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device)

Aggregations

Device (com.microsoft.azure.sdk.iot.service.Device)68 Test (org.junit.Test)46 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)17 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)15 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)12 IOException (java.io.IOException)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)11 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)10 ArrayList (java.util.ArrayList)10 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)7 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)5 AzureSasCredential (com.azure.core.credential.AzureSasCredential)4 DeviceCapabilities (com.microsoft.azure.sdk.iot.deps.twin.DeviceCapabilities)4 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)4 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)4 Module (com.microsoft.azure.sdk.iot.service.Module)4