Search in sources :

Example 6 with DeviceMethodClientOptions

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

the class RoleBasedAuthenticationSample method runDeviceMethodClientSample.

private static void runDeviceMethodClientSample(String iotHubHostName, TokenCredential credential, String deviceId) {
    // JobClient 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.
    DeviceMethodClientOptions options = DeviceMethodClientOptions.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.
    DeviceMethod deviceMethod = new DeviceMethod(iotHubHostName, credential, options);
    try {
        System.out.println("Invoking method on device if it is online");
        deviceMethod.invoke(deviceId, "someMethodName", 5L, 2L, "Some method invocation payload");
    } catch (IotHubException e) {
        if (e.getErrorCodeDescription() == ErrorCodeDescription.DeviceNotOnline) {
            System.out.println("Device was not online, so the method invocation failed.");
        } else {
            System.err.println("Failed to invoke a method on your device");
            e.printStackTrace();
            System.exit(-1);
        }
    } catch (IOException e) {
        System.err.println("Failed to invoke a method on your device");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) IOException(java.io.IOException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Aggregations

DeviceMethodClientOptions (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions)6 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)3 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 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)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