Search in sources :

Example 6 with DeviceTwinClientOptions

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions in project azure-iot-sdk-java by Azure.

the class RoleBasedAuthenticationSample method runTwinClientSample.

private static void runTwinClientSample(String iotHubHostName, TokenCredential credential, String deviceId) {
    // DeviceTwin has some configurable options for HTTP read and connect timeouts, as well as for setting proxies.
    // For this sample, the default options will be used though.
    DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().build();
    // This constructor takes in your implementation of TokenCredential which allows you to use RBAC authentication
    // rather than symmetric key based authentication that comes with constructors that take connection strings.
    DeviceTwin twinClient = new DeviceTwin(iotHubHostName, credential, options);
    DeviceTwinDevice newDeviceTwin = new DeviceTwinDevice(deviceId);
    try {
        System.out.println("Getting twin for device " + deviceId);
        twinClient.getTwin(newDeviceTwin);
    } catch (IotHubException | IOException e) {
        System.err.println("Failed to get twin for device " + deviceId);
        e.printStackTrace();
        System.exit(-1);
    }
    System.out.println("Successfully got the twin for the new device");
    System.out.println("Device Id: " + newDeviceTwin.getDeviceId());
    System.out.println("ETag: " + newDeviceTwin.getETag());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

Aggregations

DeviceTwinClientOptions (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions)6 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 AzureSasCredential (com.azure.core.credential.AzureSasCredential)1 TokenCredential (com.azure.core.credential.TokenCredential)1 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)1 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 HttpProxyServer (org.littleshoot.proxy.HttpProxyServer)1 DefaultHttpProxyServer (org.littleshoot.proxy.impl.DefaultHttpProxyServer)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)1