Search in sources :

Example 1 with DigitalTwinUpdateHeaders

use of com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinUpdateHeaders 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)

Aggregations

JsonObject (com.google.gson.JsonObject)1 UpdateOperationUtility (com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility)1 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)1 DigitalTwinUpdateHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinUpdateHeaders)1 HashMap (java.util.HashMap)1