Search in sources :

Example 1 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class SendMessagesIT method SendMessagesOverHttps.

@Test
public void SendMessagesOverHttps() throws URISyntaxException, IOException {
    String messageString = "Java client e2e test message over Https protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceHttps), IotHubClientProtocol.HTTPS);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over HTTPS protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over HTTPS protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 2 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class SendMessagesIT method SendMessagesOverAmqps.

@Test
public void SendMessagesOverAmqps() throws URISyntaxException, IOException, InterruptedException {
    String messageString = "Java client e2e test message over Amqps protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceAmqps), IotHubClientProtocol.AMQPS);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over AMQPS protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over AMQPS protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 3 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method getDigitalTwinWithTokenCredential.

@Test
public void getDigitalTwinWithTokenCredential() throws IOException, IotHubException, URISyntaxException {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    DeviceClient deviceClient = createDeviceClient(MQTT, registryManager);
    deviceClient.open();
    // arrange
    DigitalTwinClient digitalTwinClient = buildDigitalTwinClientWithTokenCredential();
    // act
    BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
    // assert
    assertEquals(response.getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
    assertEquals(responseWithHeaders.body().getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) Test(org.junit.Test)

Example 4 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method createDeviceClient.

private DeviceClient createDeviceClient(IotHubClientProtocol protocol, RegistryManager registryManager) throws IOException, IotHubException, URISyntaxException {
    ClientOptions options = new ClientOptions();
    options.setModelId(THERMOSTAT_MODEL_ID);
    String deviceId = "some-device-" + UUID.randomUUID();
    Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
    Device registeredDevice = registryManager.addDevice(device);
    String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
    return new DeviceClient(deviceConnectionString, protocol, options);
}
Also used : ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 5 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method invokeMethodSucceedWithTokenCredential.

@Test
public void invokeMethodSucceedWithTokenCredential() throws Exception {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    DeviceMethod methodServiceClient = buildDeviceMethodClientWithTokenCredential();
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
    registryManager.addDevice(device);
    DeviceClient deviceClient = new DeviceClient(registryManager.getDeviceConnectionString(device), MQTT);
    deviceClient.open();
    final int successStatusCode = 200;
    final AtomicBoolean methodsSubscriptionComplete = new AtomicBoolean(false);
    final AtomicBoolean methodsSubscribedSuccessfully = new AtomicBoolean(false);
    deviceClient.subscribeToDeviceMethod((methodName, methodData, context) -> new DeviceMethodData(successStatusCode, "success"), null, (responseStatus, callbackContext) -> {
        if (responseStatus == IotHubStatusCode.OK_EMPTY || responseStatus == IotHubStatusCode.OK) {
            methodsSubscribedSuccessfully.set(true);
        }
        methodsSubscriptionComplete.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!methodsSubscriptionComplete.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > METHOD_SUBSCRIPTION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Method subscription callback fired with non 200 status code", methodsSubscribedSuccessfully.get());
    MethodResult result = methodServiceClient.invoke(device.getDeviceId(), "someMethod", 5l, 5l, null);
    assertEquals((long) successStatusCode, (long) result.getStatus());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Aggregations

DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)38 Test (org.junit.Test)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)14 Device (com.microsoft.azure.sdk.iot.service.Device)10 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)6 URISyntaxException (java.net.URISyntaxException)6 Message (com.microsoft.azure.sdk.iot.device.Message)5 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)5 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)4 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)3 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)3 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)3 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)3