简体   繁体   English

用于从设备孪生获取所需属性的 Azure 物联网集线器设备与服务 SDK?

[英]Azure Iot hub Device vs. Service SDK for getting desired properties from Device twin?

This gives a paragraph summary of the service vs device sdk:这给出了服务设备sdk 的段落摘要:

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-sdks 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.嗨,我有两个存储库或我正在处理的项目,一个是使用 Azure Iot Hub服务SDK(此处为 Java 的文档 API: https : //docs.microsoft.com/en-us/java/api/com。 microsoft.azure.sdk.iot.service?view=azure-java-stable ),这使得获取 DeviceTwin 所需的属性变得非常容易。 I just need a DeviceTwinDevice object and then I call getDesiredProperties() on it.我只需要一个DeviceTwinDevice对象,然后在它上面调用getDesiredProperties() 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.现在,我正在处理另一个存储库,我必须在其中读取设备孪生中的特定属性,但该项目使用的是 Azure Iot Hub设备SDK(此处为 Java 的文档 API: https : //docs.microsoft.com/ en-us/java/api/com.microsoft.azure.sdk.iot.device?view=azure-java-stable ),它的工作原理有点不同。 It looks like they use a DeviceClient object to connect the Iot hub and such.看起来他们使用DeviceClient对象来连接物DeviceClient集线器等。 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?除了getDeviceTwin()方法之外,我没有看到任何检索 DeviceTwin 所需属性的方法,但它是一个 void 方法并且不返回任何内容? 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:对于之前没有见过这些“属性”的人,它只是位于 Azure 门户网站上的 JSON: 在此处输入图片说明

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?是否有一种简单的方法可以使用设备sdk 获取这些属性,或者我必须在 Gradle 中拖动服务sdk 的依赖项并这样做? It seems redundant.似乎是多余的。 Please help!请帮忙!

The getDeviceTwin() method in the Device SDK in Java works a little different from other languages. Java 中 Device SDK 中的getDeviceTwin()方法与其他语言的工作方式略有不同。 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.神奇的是在调用getDeviceTwin()的几行getDeviceTwin() ,您首先定义要为其注册回调的属性。 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());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM