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