use of com.microsoft.azure.sdk.iot.service.RegistryManager in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method getDigitalTwinWithTokenCredential.
@Test
public void getDigitalTwinWithTokenCredential() throws IOException, IotHubException, URISyntaxException {
// only run tests for standard tier hubs
Assume.assumeFalse(isBasicTierHub);
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
DeviceClient deviceClient = createDeviceClient(MQTT, registryManager);
deviceClient.open();
// arrange
DigitalTwinClient digitalTwinClient = buildDigitalTwinClientWithTokenCredential();
// act
BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
// assert
assertEquals(response.getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
assertEquals(responseWithHeaders.body().getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.service.RegistryManager 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.RegistryManager 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.RegistryManager 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.RegistryManager in project azure-iot-sdk-java by Azure.
the class DeviceManagerExportSample method main.
public static void main(String[] args) throws Exception {
System.out.println("Starting export sample...");
CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerExportSample.sampleContainerName);
container.createIfNotExists();
String containerSasUri = SampleUtils.getContainerSasUri(container);
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
JobProperties exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
while (true) {
exportJob = registryManager.getJob(exportJob.getJobId());
if (exportJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
break;
}
Thread.sleep(500);
}
for (ListBlobItem blobItem : container.listBlobs()) {
if (blobItem instanceof CloudBlob) {
CloudBlob blob = (CloudBlob) blobItem;
blob.download(new FileOutputStream(SampleUtils.exportFileLocation + blob.getName()));
}
}
registryManager.close();
System.out.println("Export job completed. Results are in " + SampleUtils.exportFileLocation);
}
Aggregations