Search in sources :

Example 1 with DeviceMethodClientOptions

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);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)

Example 2 with DeviceMethodClientOptions

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);
}
Also used : DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) TokenCredential(com.azure.core.credential.TokenCredential)

Example 3 with DeviceMethodClientOptions

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();
    }
}
Also used : DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) InetSocketAddress(java.net.InetSocketAddress) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 4 with DeviceMethodClientOptions

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

Example 5 with DeviceMethodClientOptions

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