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