Search in sources :

Example 1 with DigitalTwinGetHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders in project azure-iot-sdk-java by Azure.

the class TemperatureController method UpdateDigitalTwinComponentProperty.

private static void UpdateDigitalTwinComponentProperty() {
    // Get digital twin.
    ServiceResponseWithHeaders<String, DigitalTwinGetHeaders> getResponse = GetDigitalTwin();
    // Construct the options for conditional update.
    DigitalTwinUpdateRequestOptions options = new DigitalTwinUpdateRequestOptions();
    options.setIfMatch(getResponse.headers().eTag());
    UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility();
    // The json patch format can be found here - https://docs.microsoft.com/en-us/azure/iot-pnp/howto-manage-digital-twin#update-a-digital-twin.
    // Add a new component.
    String newComponentName = "newThermostat";
    System.out.println("--------------------------------------------------------------------------------------------");
    System.out.println("Add a new component " + newComponentName);
    System.out.println("--------------------------------------------------------------------------------------------");
    options.setIfMatch(getResponse.headers().eTag());
    String path = "/" + newComponentName;
    Map<String, Object> properties = new HashMap<>();
    properties.put(propertyName, 50);
    updateOperationUtility.appendAddComponentOperation(path, properties);
    List<Object> digitalTwinUpdateOperations = updateOperationUtility.getUpdateOperations();
    ServiceResponseWithHeaders<Void, DigitalTwinUpdateHeaders> updateResponse = client.updateDigitalTwinWithResponse(digitalTwinid, digitalTwinUpdateOperations, options);
    System.out.println("Update Digital Twin response status: " + updateResponse.response().message());
    getResponse = GetDigitalTwin();
    // Replace an existing component.
    String component1 = "thermostat1";
    String component2 = "thermostat2";
    System.out.println("--------------------------------------------------------------------------------------------");
    System.out.println("Replace existing components " + component1 + " and " + component2);
    System.out.println("--------------------------------------------------------------------------------------------");
    options.setIfMatch(getResponse.headers().eTag());
    path = "/" + component1;
    Map<String, Object> t1properties = new HashMap<>();
    t1properties.put(propertyName, 50);
    updateOperationUtility.appendReplaceComponentOperation(path, t1properties);
    path = "/" + component2;
    Map<String, Object> t2properties = new HashMap<>();
    t2properties.put(propertyName, 40);
    updateOperationUtility.appendReplaceComponentOperation(path, t2properties);
    digitalTwinUpdateOperations = updateOperationUtility.getUpdateOperations();
    updateResponse = client.updateDigitalTwinWithResponse(digitalTwinid, digitalTwinUpdateOperations, options);
    System.out.println("Update Digital Twin response status: " + updateResponse.response().message());
    getResponse = GetDigitalTwin();
    // Remove the new component.
    System.out.println("--------------------------------------------------------------------------------------------");
    System.out.println("Remove the new component " + newComponentName);
    System.out.println("--------------------------------------------------------------------------------------------");
    options.setIfMatch(getResponse.headers().eTag());
    path = "/newThermostat";
    updateOperationUtility.appendRemoveComponentOperation(path);
    digitalTwinUpdateOperations = updateOperationUtility.getUpdateOperations();
    updateResponse = client.updateDigitalTwinWithResponse(digitalTwinid, digitalTwinUpdateOperations, options);
    System.out.println("Update Digital Twin response status: " + updateResponse.response().message());
    GetDigitalTwin();
}
Also used : UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) DigitalTwinUpdateHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinUpdateHeaders) HashMap(java.util.HashMap) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) JsonObject(com.google.gson.JsonObject)

Example 2 with DigitalTwinGetHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders 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 3 with DigitalTwinGetHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientComponentTests method updateDigitalTwin.

@Test
@StandardTierHubOnlyTest
public void updateDigitalTwin() {
    // arrange
    String newProperty = "currentTemperature";
    Integer newPropertyValue = 35;
    String newComponent = "newThermostat";
    String newComponentPath = "/newThermostat";
    Map<String, Object> componentProperties = new HashMap<>();
    componentProperties.put(newProperty, newPropertyValue);
    DigitalTwinUpdateRequestOptions optionsWithoutEtag = new DigitalTwinUpdateRequestOptions();
    optionsWithoutEtag.setIfMatch("*");
    // get digital twin and Etag before update
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
    DigitalTwinUpdateRequestOptions optionsWithEtag = new DigitalTwinUpdateRequestOptions();
    optionsWithEtag.setIfMatch(responseWithHeaders.headers().eTag());
    // act
    // Add component at root level - conditional update with max overload
    UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility().appendAddComponentOperation(newComponentPath, componentProperties);
    digitalTwinClient.updateDigitalTwinWithResponse(deviceId, updateOperationUtility.getUpdateOperations(), optionsWithEtag);
    BasicDigitalTwin digitalTwin = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class).body();
    // assert
    assertEquals(E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID, digitalTwin.getMetadata().getModelId());
    assertTrue(digitalTwin.getCustomProperties().containsKey(newComponent));
}
Also used : UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)

Example 4 with DigitalTwinGetHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method getMultiplexedDigitalTwinsRegisteredBeforeOpen.

// Open a multiplexed connection with two devices, each with a different model Id. Verify that their reported twin has
// the expected model Ids.
@Test
@StandardTierHubOnlyTest
public void getMultiplexedDigitalTwinsRegisteredBeforeOpen() throws IotHubException, IOException, URISyntaxException, MultiplexingClientException, InterruptedException {
    if (protocol == MQTT || protocol == MQTT_WS) {
        // multiplexing isn't supported over MQTT, so it can't be tested
        return;
    }
    MultiplexingClient multiplexingClient = new MultiplexingClient(deviceClient.getConfig().getIotHubHostname(), protocol);
    List<DeviceClient> multiplexedDeviceClients = new ArrayList<>();
    multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.THERMOSTAT_MODEL_ID));
    multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID));
    // register the devices before the multiplexing client has been opened
    multiplexingClient.registerDeviceClients(multiplexedDeviceClients);
    multiplexingClient.open();
    // act
    String thermostatDeviceId = multiplexedDeviceClients.get(0).getConfig().getDeviceId();
    BasicDigitalTwin thermostatResponse = digitalTwinClient.getDigitalTwin(thermostatDeviceId, BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> thermostatResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(thermostatDeviceId, BasicDigitalTwin.class);
    String temperatureControllerDeviceId = multiplexedDeviceClients.get(1).getConfig().getDeviceId();
    BasicDigitalTwin temperatureControllerResponse = digitalTwinClient.getDigitalTwin(temperatureControllerDeviceId, BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> temperatureControllerResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(temperatureControllerDeviceId, BasicDigitalTwin.class);
    // assert
    assertEquals(thermostatResponse.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
    assertEquals(thermostatResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
    assertEquals(temperatureControllerResponse.getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
    assertEquals(temperatureControllerResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 5 with DigitalTwinGetHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method getDigitalTwinWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void getDigitalTwinWithAzureSasCredential() {
    if (protocol != MQTT) {
        // This test is for the service client, so no need to rerun it for all the different device protocols
        return;
    }
    // arrange
    digitalTwinClient = buildDigitalTwinClientWithAzureSasCredential();
    // act
    BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
    // assert
    assertEquals(response.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
    assertEquals(responseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Aggregations

DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)11 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)9 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)8 DigitalTwinTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)8 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)8 Test (org.junit.Test)7 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)4 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)3 UpdateOperationUtility (com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility)3 ArrayList (java.util.ArrayList)3 JsonObject (com.google.gson.JsonObject)2 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)2 DigitalTwinClientOptions (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions)2 AzureSasCredential (com.azure.core.credential.AzureSasCredential)1 TokenCredential (com.azure.core.credential.TokenCredential)1 JsonParser (com.google.gson.JsonParser)1 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)1