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