Search in sources :

Example 21 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class DeviceTwinIT method setUpTwin.

private void setUpTwin(DeviceState deviceState) throws IOException, URISyntaxException, IotHubException, InterruptedException {
    /*
            Because of the bug in device client on MQTT, we cannot have multiple device client open
            in the main thread at the same time. Hence restricting to use single device client.
         */
    if (deviceClient == null) {
        deviceState.dCDeviceForTwin = new DeviceExtension();
        deviceClient = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceState.sCDeviceForRegistryManager), IotHubClientProtocol.MQTT);
        deviceClient.open();
        deviceClient.startDeviceTwin(new DeviceTwinStatusCallBack(), deviceState, deviceState.dCDeviceForTwin, deviceState);
        deviceState.deviceTwinStatus = STATUS.SUCCESS;
    }
    // set up twin on ServiceClient
    if (sCDeviceTwin != null) {
        deviceState.sCDeviceForTwin = new DeviceTwinDevice(deviceState.sCDeviceForRegistryManager.getDeviceId());
        sCDeviceTwin.getTwin(deviceState.sCDeviceForTwin);
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    }
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient)

Example 22 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsAfterOpenThrows.

// Attempt to register a batch of devices, all with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsAfterOpenThrows() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    testInstance.multiplexingClient.open();
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).getDeviceId();
            assertTrue(registrationExceptions.containsKey(deviceId));
            assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        }
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) ArrayList(java.util.ArrayList) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 23 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerClientAfterOpen.

// Unregister a single device from an active multiplexed connection, test that other devices on that connection
// can still be used to send telemetry.
@Test
@StandardTierHubOnlyTest
public void registerClientAfterOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    // Unregister one client so that it can be registered after the open call
    DeviceClient clientToRegisterAfterOpen = testInstance.deviceClientArray.get(DEVICE_MULTIPLEX_COUNT - 1);
    testInstance.multiplexingClient.unregisterDeviceClient(clientToRegisterAfterOpen);
    testInstance.multiplexingClient.open();
    ConnectionStatusChangeTracker connectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    clientToRegisterAfterOpen.registerConnectionStatusChangeCallback(connectionStatusChangeTracker, null);
    testInstance.multiplexingClient.registerDeviceClient(clientToRegisterAfterOpen);
    assertConnectionStateCallbackFiredConnected(connectionStatusChangeTracker, DEVICE_SESSION_OPEN_TIMEOUT);
    testSendingMessageFromDeviceClient(clientToRegisterAfterOpen);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 24 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method unregisterClientAfterOpen.

// Unregister a single device from an active multiplexed connection, test that other devices on that connection
// can still be used to send telemetry.
@Test
@StandardTierHubOnlyTest
public void unregisterClientAfterOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    // pick the 0th client to unregister after open. This will help make sure we don't have any dependencies on the 0th registered client
    DeviceClient clientToUnregisterAfterOpen = testInstance.deviceClientArray.get(0);
    ConnectionStatusChangeTracker connectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    clientToUnregisterAfterOpen.registerConnectionStatusChangeCallback(connectionStatusChangeTracker, null);
    testInstance.multiplexingClient.open();
    assertConnectionStateCallbackFiredConnected(connectionStatusChangeTracker, DEVICE_SESSION_OPEN_TIMEOUT);
    testInstance.multiplexingClient.unregisterDeviceClient(clientToUnregisterAfterOpen);
    assertDeviceSessionClosesGracefully(connectionStatusChangeTracker, DEVICE_SESSION_CLOSE_TIMEOUT);
    // start index from 1 since the 0th client was deliberately unregistered
    for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testSendingMessageFromDeviceClient(testInstance.deviceClientArray.get(i));
    }
    // verify that unregistered clients don't attempt to send messages on the active multiplexed connection after unregistration
    boolean exceptionThrown;
    try {
        testInstance.deviceClientArray.get(0).sendEventAsync(new Message("This message shouldn't be sent"), new EventCallback(IotHubStatusCode.OK_EMPTY), null);
        exceptionThrown = false;
    } catch (UnsupportedOperationException e) {
        exceptionThrown = true;
    }
    assertTrue("Expected exception to be thrown when sending a message from an unregistered client", exceptionThrown);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.helpers.EventCallback) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 25 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registrationsUnwindForMqttClient.

@Test
public void registrationsUnwindForMqttClient() throws Exception {
    Device mqttDevice = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.MQTT, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(mqttDevice);
    // MQTT clients should throw UnsupportedOperationException when registered
    DeviceClient mqttDeviceClient = new DeviceClient(deviceConnectionString, IotHubClientProtocol.MQTT);
    registrationsUnwindForUnsupportedOperationExceptions(mqttDeviceClient);
}
Also used : 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) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Aggregations

DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)38 Test (org.junit.Test)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)14 Device (com.microsoft.azure.sdk.iot.service.Device)10 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)6 URISyntaxException (java.net.URISyntaxException)6 Message (com.microsoft.azure.sdk.iot.device.Message)5 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)5 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)4 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)3 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)3 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)3 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)3