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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations