Search in sources :

Example 1 with DeviceTwinClientOptions

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

the class GetTwinTests method testGetDeviceTwinWithProxy.

@Test
@StandardTierHubOnlyTest
public void testGetDeviceTwinWithProxy() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, ModuleClientException, URISyntaxException {
    if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
        // when the device is using MQTT with SAS auth
        return;
    }
    super.setUpNewDeviceAndModule();
    String testProxyHostname = "127.0.0.1";
    int testProxyPort = 8892;
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
    try {
        Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
        DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().proxyOptions(proxyOptions).build();
        testInstance.twinServiceClient = DeviceTwin.createFromConnectionString(iotHubConnectionString, options);
        super.testGetDeviceTwin();
    } 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) InetSocketAddress(java.net.InetSocketAddress) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) 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 2 with DeviceTwinClientOptions

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

the class DeviceTwinTest method testOptionsDefaults.

@Test
public void testOptionsDefaults() {
    DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().build();
    assertEquals((int) Deencapsulation.getField(DeviceTwinClientOptions.class, "DEFAULT_HTTP_READ_TIMEOUT_MS"), options.getHttpReadTimeout());
    assertEquals((int) Deencapsulation.getField(DeviceTwinClientOptions.class, "DEFAULT_HTTP_CONNECT_TIMEOUT_MS"), options.getHttpConnectTimeout());
}
Also used : DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) Test(org.junit.Test)

Example 3 with DeviceTwinClientOptions

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

the class DeviceTwinCommon method buildDeviceTwinClientWithAzureSasCredential.

protected static DeviceTwin buildDeviceTwinClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new DeviceTwin(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

Example 4 with DeviceTwinClientOptions

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

the class Tools method buildDeviceTwinClientWithTokenCredential.

public static DeviceTwin buildDeviceTwinClientWithTokenCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    TokenCredential tokenCredential = buildTokenCredentialFromEnvironment();
    DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().build();
    return new DeviceTwin(iotHubConnectionStringObj.getHostName(), tokenCredential, options);
}
Also used : DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) TokenCredential(com.azure.core.credential.TokenCredential) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

Example 5 with DeviceTwinClientOptions

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

the class AzureSasCredentialSample method runTwinClientSample.

private static void runTwinClientSample(String iotHubHostName, AzureSasCredential 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 AzureSasCredential which allows you to use symmetric key based
    // authentication without giving the client your connection string.
    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