1

I am using the com.microsoft.azure.sdk.iot:iot-service-client:1.16.0 library (Java) for working with Azure IoT Devices. I am trying to "update" some properties, however they nested and I am having trouble updating them.

My DeviceTwin Json looks like this:

enter image description here

The azure library comes with a setDesiredProperties function that takes a HashSet<Pair> as its argument. So I can easily update `firmwareVersionToDownload" , "serverConfigurationVersion", or "webContentDataType" keys because they are Top-Level entities, like this (as seen in the image above):

myIotDeviceObjectInJava.setDesiredProperties(new HashSet<Pair>() {{
            add(new Pair("myKey", "myNewValueForKey"));
        }});

I need to set the value for the keys "object1" & "object2" to the value of "null", but they are 2/3 levels deep. Can anyone help me to do this?

I've tried creating a new HashSet with other nested HashSet's until I reached the level (third) where I then passed in a new Pair("object1","null") object to set to null, but it ended up just deleting my entire "serverConfiguration" block of code (example below):

updateProperties(new HashSet<Pair>() {{
            add(new Pair("serverConfiguration", new HashSet<Pair>() {{
                add(new Pair("nestedIpData1",new HashSet<Pair>() {{
                    add(new Pair("object1","null"));
                }}));
            }}));
        }}
        );

Do I need to iterate over the top level objects and get the value object of serverConfiguration and then set it somehow that way?

asergaz
  • 996
  • 5
  • 17
ennth
  • 1,698
  • 5
  • 31
  • 63

1 Answers1

1

I went ahead and tried do it myself using the sample Get started with device twins (Java) with some tweaks in the Service App.

Note: I used com.microsoft.azure.sdk.iot:iot-service-client:1.25.0

In order to understand the type of object returned when reading a desired property with a nested Json Object I first edited the Twin directly in the Azure Portal and then debuged the code to check the type of ojbect:

DeviceTwin twinClient = DeviceTwin.createFromConnectionString(iotHubConnectionString);
DeviceTwinDevice device = new DeviceTwinDevice(deviceId);
Set<Pair> currentDesired = device.getDesiredProperties();

enter image description here

You can notice that "ServerConfiguration" and "Object3" values are of type TwinCollection (check TwinCollection.java code comments to understand it better)

Therefore, in order to set object1 and object 2 to null :

        TwinCollection nestedIpData1 = new TwinCollection();
        TwinCollection object1 = new TwinCollection();
        TwinCollection object2 = new TwinCollection();
        TwinCollection object3 = new TwinCollection();

        object1.putFinal("hostname", "my.host.name1");
        object1.putFinal("netmask", "1.1.1.1");
        object1.putFinal("ip", "1.1.1.1");
        object1.putFinal("cloudIp", "1.1.1.1");
        object1.putFinal("gateway", "1.1.1.1");

        object2.putFinal("hostname", "my.host.name2");
        object2.putFinal("netmask", "2.2.2.2");
        object2.putFinal("ip", "2.2.2.2");
        object2.putFinal("cloudIp", "2.2.2.2");
        object2.putFinal("gateway", "2.2.2.2");

        object3.putFinal("hostname", "my.host.name3");
        object3.putFinal("netmask", "3.3.3.3");
        object3.putFinal("ip", "3.3.3.3");
        object3.putFinal("cloudIp", "3.3.3.3");
        object3.putFinal("gateway", "3.3.3.3");

        nestedIpData1.putFinal("object1", null);
        nestedIpData1.putFinal("object2", null);
        nestedIpData1.putFinal("object3", object3);

        Set<Pair> desiredProperties = new HashSet<Pair>();
        desiredProperties.add(new Pair("serverConfiguration", nestedIpData1));

        device.setDesiredProperties(desiredProperties);

        // Update the device twin in IoT Hub
        System.out.println("Updating device twin with  nestedobject");
        twinClient.updateTwin(device);
asergaz
  • 996
  • 5
  • 17