0
votes

This gives a paragraph summary of the service vs device sdk:

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-sdks

Hi, I have two repositories or projects I'm working on, one is using the Azure Iot Hub Service SDK (documentation API for java here: https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.sdk.iot.service?view=azure-java-stable), which makes it very easy to get the DeviceTwin desired properties. I just need a DeviceTwinDevice object and then I call getDesiredProperties() on it. This all comes from the dependency:

compile group: 'com.microsoft.azure.sdk.iot', name: 'iot-service-client', version: '1.16.0'

Now, I am working on another repo, where I have to read a specific property from the Device twin, but that project is using the Azure Iot Hub Device SDK (documentation API for Java here: https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.sdk.iot.device?view=azure-java-stable), and it works a little different. It looks like they use a DeviceClient object to connect the Iot hub and such. I don't see any methods for retrieving the desired properties for the DeviceTwin other than a getDeviceTwin() method, but it is a void method and returns nothing? The dependency for this is

 compile(group: 'com.microsoft.azure.sdk.iot', name: 'iot-device-client', version: '1.19.1')

For those of you who haven't seen these "properties" before, it's just JSON located on the Azure Portal website: enter image description here

Is there an easy way to grab these properties with the device sdk or must I drag the dependency in Gradle for the service sdk and do it that way? It seems redundant. Please help!

1

1 Answers

1
votes

The getDeviceTwin() method in the Device SDK in Java works a little different from other languages. There is a really good sample here. The magic happens a few lines about calling getDeviceTwin(), where you first define what properties you want to register a callback for. The samples below are all from the above link:

System.out.println("Subscribe to Desired properties on device Twin...");
Map<Property, Pair<TwinPropertyCallBack, Object>> desiredProperties = new HashMap<Property, Pair<TwinPropertyCallBack, Object>>()
{
  {
    put(new Property("HomeTemp(F)", null), new Pair<TwinPropertyCallBack, Object>(new onHomeTempChange(), null));
    put(new Property("LivingRoomLights", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
    put(new Property("BedroomRoomLights", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
    put(new Property("HomeSecurityCamera", null), new Pair<TwinPropertyCallBack, Object>(new onCameraActivity(), null));
  }
};

client.subscribeToTwinDesiredProperties(desiredProperties);

System.out.println("Get device Twin...");
client.getDeviceTwin(); // Will trigger the callbacks.

Handling the received property is then done in the callback, for example:

protected static class onProperty implements TwinPropertyCallBack
{
  @Override
  public void TwinPropertyCallBack(Property property, Object context)
  {
    System.out.println(
      "onProperty callback for " + (property.getIsReported()?"reported": "desired") +
      " property " + property.getKey() +
      " to " + property.getValue() +
      ", Properties version:" + property.getVersion());
  }
}