use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions in project azure-iot-sdk-java by Azure.
the class DeviceMethodCommon method buildDeviceMethodClientWithAzureSasCredential.
protected static DeviceMethod buildDeviceMethodClientWithAzureSasCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
return new DeviceMethod(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions in project azure-iot-sdk-java by Azure.
the class Tools method buildDeviceMethodClientWithTokenCredential.
public static com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod buildDeviceMethodClientWithTokenCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
TokenCredential tokenCredential = buildTokenCredentialFromEnvironment();
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().build();
return new com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod(iotHubConnectionStringObj.getHostName(), tokenCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions in project azure-iot-sdk-java by Azure.
the class DeviceMethodTests method invokeMethodWithServiceSideProxy.
@Test
@StandardTierHubOnlyTest
public void invokeMethodWithServiceSideProxy() throws Exception {
if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
// when the device is using MQTT with SAS auth
return;
}
String testProxyHostname = "127.0.0.1";
int testProxyPort = 8894;
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
try {
Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(HTTP_READ_TIMEOUT).build();
this.testInstance.methodServiceClient = DeviceMethod.createFromConnectionString(iotHubConnectionString, options);
super.openDeviceClientAndSubscribeToMethods();
super.invokeMethodSucceed();
} finally {
proxyServer.stop();
}
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions in project azure-iot-sdk-java by Azure.
the class DeviceMethodTest method testOptionsDefaults.
@Test
public void testOptionsDefaults() {
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().build();
assertEquals((int) Deencapsulation.getField(DeviceMethodClientOptions.class, "DEFAULT_HTTP_READ_TIMEOUT_MS"), options.getHttpReadTimeout());
assertEquals((int) Deencapsulation.getField(DeviceMethodClientOptions.class, "DEFAULT_HTTP_CONNECT_TIMEOUT_MS"), options.getHttpConnectTimeout());
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions in project azure-iot-sdk-java by Azure.
the class AzureSasCredentialSample method runDeviceMethodClientSample.
private static void runDeviceMethodClientSample(String iotHubHostName, AzureSasCredential 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 AzureSasCredential which allows you to use symmetric key based
// authentication without giving the client your connection string.
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