Search in sources :

Example 1 with ModuleClient

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

the class ReceiveMessagesTests method receiveMessage.

public void receiveMessage(int messageSize) throws Exception {
    if (testInstance.protocol == HTTPS) {
        testInstance.identity.getClient().setOption(SET_MINIMUM_POLLING_INTERVAL, ONE_SECOND_POLLING_INTERVAL);
    }
    testInstance.identity.getClient().open();
    Message serviceMessage = createCloudToDeviceMessage(messageSize);
    com.microsoft.azure.sdk.iot.device.MessageCallback callback = new MessageCallback(serviceMessage);
    if (testInstance.protocol == MQTT || testInstance.protocol == MQTT_WS) {
        callback = new MessageCallbackMqtt(serviceMessage);
    }
    Success messageReceived = new Success();
    if (testInstance.identity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.identity.getClient()).setMessageCallback(callback, messageReceived);
    } else if (testInstance.identity.getClient() instanceof ModuleClient) {
        ((ModuleClient) testInstance.identity.getClient()).setMessageCallback(callback, messageReceived);
    }
    if (testInstance.identity.getClient() instanceof DeviceClient) {
        testInstance.serviceClient.send(testInstance.identity.getDeviceId(), serviceMessage);
    } else if (testInstance.identity.getClient() instanceof ModuleClient) {
        testInstance.serviceClient.send(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), serviceMessage);
    }
    waitForMessageToBeReceived(messageReceived, testInstance.protocol.toString());
    Thread.sleep(200);
    testInstance.identity.getClient().closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) Success(tests.integration.com.microsoft.azure.sdk.iot.helpers.Success) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)

Example 2 with ModuleClient

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

the class DeviceTwinCommon method testGetDeviceTwin.

protected void testGetDeviceTwin() throws IOException, InterruptedException, IotHubException {
    // arrange
    Map<Property, com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair<TwinPropertyCallBack, Object>> desiredPropertiesCB = new HashMap<>();
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        PropertyState propertyState = new PropertyState();
        propertyState.property = new Property(PROPERTY_KEY + i, PROPERTY_VALUE);
        testInstance.deviceUnderTest.dCDeviceForTwin.propertyStateList[i] = propertyState;
        desiredPropertiesCB.put(propertyState.property, new com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair<>(testInstance.deviceUnderTest.dCOnProperty, propertyState));
    }
    testInstance.testIdentity.getClient().subscribeToTwinDesiredProperties(desiredPropertiesCB);
    Thread.sleep(DELAY_BETWEEN_OPERATIONS);
    Set<Pair> desiredProperties = new HashSet<>();
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        desiredProperties.add(new Pair(PROPERTY_KEY + i, PROPERTY_VALUE_UPDATE + UUID.randomUUID()));
    }
    testInstance.deviceUnderTest.sCDeviceForTwin.setDesiredProperties(desiredProperties);
    testInstance.twinServiceClient.updateTwin(testInstance.deviceUnderTest.sCDeviceForTwin);
    Thread.sleep(DELAY_BETWEEN_OPERATIONS);
    for (int i = 0; i < testInstance.deviceUnderTest.dCDeviceForTwin.propertyStateList.length; i++) {
        PropertyState propertyState = testInstance.deviceUnderTest.dCDeviceForTwin.propertyStateList[i];
        propertyState.callBackTriggered = false;
        propertyState.propertyNewVersion = -1;
    }
    // act
    if (testInstance.testIdentity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.testIdentity.getClient()).getDeviceTwin();
    } else {
        ((ModuleClient) testInstance.testIdentity.getClient()).getTwin();
    }
    // assert
    assertEquals(TwinConnectionState.CONNECTED.toString(), testInstance.deviceUnderTest.sCDeviceForTwin.getConnectionState());
    waitAndVerifyTwinStatusBecomesSuccess();
    waitAndVerifyDesiredPropertyCallback(PROPERTY_VALUE_UPDATE, true);
}
Also used : HashMap(java.util.HashMap) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) HashSet(java.util.HashSet)

Example 3 with ModuleClient

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

the class Tools method getX509TestModule.

private static TestModuleIdentity getX509TestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testX509ModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testX509ModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testX509ModuleWithTwinQueue");
            testModuleIdentity = testX509ModuleWithTwinQueue.remove();
        } else {
            if (testX509ModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SELF_SIGNED, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    Module module = Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SELF_SIGNED);
                    String x509Thumbprint = IntegrationTest.x509CertificateGenerator.getX509Thumbprint();
                    module.setThumbprintFinal(x509Thumbprint, x509Thumbprint);
                    modulesToAdd.add(module);
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testX509ModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testX509ModuleQueue");
            testModuleIdentity = testX509ModuleQueue.remove();
        }
        SSLContext sslContext = SSLContextBuilder.buildSSLContext(IntegrationTest.x509CertificateGenerator.getPublicCertificate(), IntegrationTest.x509CertificateGenerator.getPrivateKey());
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol, sslContext);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) SSLContext(javax.net.ssl.SSLContext) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Example 4 with ModuleClient

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

the class Tools method getSasTestModule.

private static TestModuleIdentity getSasTestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testSasModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testSasModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testSasModuleWithTwinQueue");
            testModuleIdentity = testSasModuleWithTwinQueue.remove();
        } else {
            if (testSasModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SAS, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    modulesToAdd.add(Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SAS));
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testSasModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testSasModuleQueue");
            testModuleIdentity = testSasModuleQueue.remove();
        }
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Example 5 with ModuleClient

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

the class DesiredPropertiesTests method testSubscribeToDesiredPropertiesBatch.

// This test is for the startDeviceTwin/startTwin API that takes the TwinPropertiesCallback rather than the TwinPropertyCallback
// This callback should receive the full twin update in one callback, rather than one callback per updated
// desired property
@Test
@StandardTierHubOnlyTest
public void testSubscribeToDesiredPropertiesBatch() throws Exception {
    super.setUpNewDeviceAndModule(false);
    String expectedKey1 = UUID.randomUUID().toString();
    String expectedValue1 = UUID.randomUUID().toString();
    String expectedKey2 = UUID.randomUUID().toString();
    String expectedValue2 = UUID.randomUUID().toString();
    TwinCollection expectedDesiredProperties = new TwinCollection();
    expectedDesiredProperties.putFinal(expectedKey1, expectedValue1);
    expectedDesiredProperties.putFinal(expectedKey2, expectedValue2);
    TwinPropertiesCallback twinPropertiesCallback = new TwinPropertiesCallbackImpl(expectedDesiredProperties);
    Success desiredPropertiesCallbackState = new Success();
    testInstance.testIdentity.getClient().open();
    if (testInstance.testIdentity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.testIdentity.getClient()).startDeviceTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    } else {
        ((ModuleClient) testInstance.testIdentity.getClient()).startTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    }
    long startTime = System.currentTimeMillis();
    while (testInstance.deviceUnderTest.deviceTwinStatus != IotHubStatusCode.OK) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > START_TWIN_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for twin to start");
        }
    }
    DeviceTwinDevice serviceClientTwin;
    if (testInstance.clientType == ClientType.DEVICE_CLIENT) {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId());
    } else {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId(), testInstance.testIdentity.getClient().getConfig().getModuleId());
    }
    Set<com.microsoft.azure.sdk.iot.service.devicetwin.Pair> desiredProperties = new HashSet<>();
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey1, expectedValue1));
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey2, expectedValue2));
    serviceClientTwin.setDesiredProperties(desiredProperties);
    testInstance.twinServiceClient.updateTwin(serviceClientTwin);
    startTime = System.currentTimeMillis();
    while (!desiredPropertiesCallbackState.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > DESIRED_PROPERTIES_PROPAGATION_TIME_MILLISECONDS) {
            fail("Timed out waiting for desired properties callback to execute");
        }
    }
    assertTrue("Desired properties callback executed, but with unexpected properties", desiredPropertiesCallbackState.getResult());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TwinPropertiesCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) 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)

Aggregations

ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)6 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)4 Device (com.microsoft.azure.sdk.iot.service.Device)2 Module (com.microsoft.azure.sdk.iot.service.Module)2 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)2 ArrayList (java.util.ArrayList)2 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)1 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)1 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)1 TwinPropertiesCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 SSLContext (javax.net.ssl.SSLContext)1 Test (org.junit.Test)1 Success (tests.integration.com.microsoft.azure.sdk.iot.helpers.Success)1 TestModuleIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)1