Search in sources :

Example 16 with RegistryManager

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);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) Test(org.junit.Test)

Example 17 with RegistryManager

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 18 with RegistryManager

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());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 19 with RegistryManager

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());
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 20 with RegistryManager

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);
}
Also used : CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) FileOutputStream(java.io.FileOutputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Aggregations

RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)39 IOException (java.io.IOException)20 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)19 Device (com.microsoft.azure.sdk.iot.service.Device)15 Test (org.junit.Test)14 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)7 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)6 AzureSasCredential (com.azure.core.credential.AzureSasCredential)5 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)5 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)5 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)5 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)5 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)5 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)5 RegistryStatistics (com.microsoft.azure.sdk.iot.service.RegistryStatistics)4 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)3 Configuration (com.microsoft.azure.sdk.iot.service.Configuration)3 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)3 Message (com.microsoft.azure.sdk.iot.service.Message)3 Module (com.microsoft.azure.sdk.iot.service.Module)3