Search in sources :

Example 51 with Device

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

the class MultiplexingClientTests method disableDeviceAfterOpenAndAfterRegistration.

// If you disable a device on an active multiplexed connection, that device session should drop and all the other
// device sessions should be unaffected.
@ContinuousIntegrationTest
@Test
public void disableDeviceAfterOpenAndAfterRegistration() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    // Disable a device that is on the multiplexed connection and that already has an open session
    Device deviceToDisable = registryManager.getDevice(testInstance.deviceIdentityArray.get(0).getDeviceId());
    deviceToDisable.setStatus(DeviceStatus.Disabled);
    registryManager.updateDevice(deviceToDisable);
    try {
        // verify that the disabled device loses its device session
        long startTime = System.currentTimeMillis();
        while (!connectionStatusChangeTrackers[0].wentDisconnectedRetrying) {
            Thread.sleep(200);
            if (System.currentTimeMillis() - startTime > FAULT_INJECTION_TIMEOUT_MILLIS) {
                fail("Timed out waiting for the disabled device's client to report DISCONNECTED_RETRYING");
            }
        }
        assertFalse(connectionStatusChangeTrackers[0].isOpen);
        // Verify that the other devices on the multiplexed connection were unaffected
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            assertFalse(connectionStatusChangeTrackers[i].wentDisconnectedRetrying);
            assertTrue(connectionStatusChangeTrackers[i].isOpen);
        }
        // Verify that the multiplexed connection itself was unaffected
        assertFalse(multiplexedConnectionStatusChangeTracker.wentDisconnectedRetrying);
        assertTrue(multiplexedConnectionStatusChangeTracker.isOpen);
        // Verify that the other devices can still send telemetry
        testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray.subList(1, DEVICE_MULTIPLEX_COUNT));
        testInstance.multiplexingClient.close();
    } finally {
        // re enable the device in case it gets recycled
        deviceToDisable.setStatus(DeviceStatus.Enabled);
        registryManager.updateDevice(deviceToDisable);
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 52 with Device

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

the class Tools method addDeviceWithRetry.

public static Device addDeviceWithRetry(RegistryManager registryManager, Device device) throws IotHubException, IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    Device ret = null;
    while (System.currentTimeMillis() - startTime < RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
        try {
            log.debug("Attempting to add device {} to registry", device.getDeviceId());
            ret = registryManager.addDevice(device);
            log.debug("Successfully added device {} to registry", device.getDeviceId());
            break;
        } catch (UnknownHostException | SocketException | SocketTimeoutException e) {
            log.warn("Failed to add device " + device.getDeviceId());
            e.printStackTrace();
            Thread.sleep(WAIT_FOR_RETRY);
            if (System.currentTimeMillis() - startTime >= RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
                throw e;
            }
        }
    }
    return ret;
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) Device(com.microsoft.azure.sdk.iot.service.Device)

Example 53 with Device

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

the class Tools method getSasTestModule.

private static TestModuleIdentity getSasTestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testSasModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testSasModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testSasModuleWithTwinQueue");
            testModuleIdentity = testSasModuleWithTwinQueue.remove();
        } else {
            if (testSasModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SAS, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    modulesToAdd.add(Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SAS));
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testSasModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testSasModuleQueue");
            testModuleIdentity = testSasModuleQueue.remove();
        }
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Example 54 with Device

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

the class Tools method getDeviceWithRetry.

public static Device getDeviceWithRetry(RegistryManager registryManager, String id) throws IotHubException, IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    Device ret = null;
    while (System.currentTimeMillis() - startTime < RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
        try {
            ret = registryManager.getDevice(id);
            break;
        } catch (UnknownHostException | SocketException e) {
            System.out.println("Failed to get device ");
            e.printStackTrace();
            Thread.sleep(WAIT_FOR_RETRY);
            if (System.currentTimeMillis() - startTime >= RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
                throw e;
            }
        }
    }
    return ret;
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) Device(com.microsoft.azure.sdk.iot.service.Device)

Example 55 with Device

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

the class Tools method removeDevices.

private static void removeDevices(Iterable<Device> devices, String connectionString) throws IOException, IotHubException {
    if (devices == null) {
        throw new IllegalArgumentException("devices cannot be null");
    }
    IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
    URL url = getBulkDeviceAddUrl(iotHubConnectionString);
    List<ExportImportDeviceParser> parsers = new ArrayList<>();
    for (Device device : devices) {
        ExportImportDeviceParser exportImportDevice = new ExportImportDeviceParser();
        exportImportDevice.setId(device.getDeviceId());
        exportImportDevice.setImportMode(IMPORT_MODE_DELETE);
        parsers.add(exportImportDevice);
    }
    ExportImportDevicesParser body = new ExportImportDevicesParser();
    body.setExportImportDevices(parsers);
    bulkRegistryOperation(body.toJson(), url, connectionString);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

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