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

huangapple go评论128阅读模式
英文:

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

问题

以下是翻译好的内容:

这是有关 服务设备 SDK 的段落摘要:

链接:https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-sdks

嗨,我有两个我正在开发的代码库或项目,一个使用了 Azure IoT Hub 的 服务 SDK(Java 文档 API 在这里:https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.sdk.iot.service?view=azure-java-stable),它使得获取 DeviceTwin 的期望属性非常容易。我只需要一个 DeviceTwinDevice 对象,然后调用 getDesiredProperties() 方法即可。这一切都来自于以下依赖项:

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

现在,我正在另一个代码库中工作,在那里我必须从设备的 Device Twin 中读取特定属性,但该项目使用了 Azure IoT Hub 的 设备 SDK(Java 文档 API 在这里:https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.sdk.iot.device?view=azure-java-stable),它的工作方式略有不同。看起来他们使用了一个 DeviceClient 对象来连接 IoT Hub 等。我没有看到任何用于检索 DeviceTwin 期望属性的方法,除了一个 getDeviceTwin() 方法,但它是一个空方法,不返回任何内容?该依赖项如下:

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

对于那些以前没有见过这些“属性”的人,它只是位于 Azure 门户网站上的 JSON 数据:
Azure Iot hub Device vs. Service SDK for getting desired properties from Device twin?

设备 SDK 中是否有一种简单的方法来获取这些属性,还是我必须在 Gradle 中添加 服务 SDK 的依赖项,然后以那种方式来做?这似乎有些多余。请帮忙解答!

英文:

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

https://learn.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://learn.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://learn.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:
Azure Iot hub Device vs. Service SDK for getting desired properties from Device twin?

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

System.out.println("订阅设备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("获取设备Twin...");
client.getDeviceTwin(); // 将触发回调。

处理接收到的属性然后在回调中完成,例如:

protected static class onProperty implements TwinPropertyCallBack
{
  @Override
  public void TwinPropertyCallBack(Property property, Object context)
  {
    System.out.println(
      "对于" + (property.getIsReported() ? "已报告" : "期望") +
      "属性的onProperty回调:" + property.getKey() +
      ",值为:" + property.getValue() +
      ",属性版本:" + property.getVersion());
  }
}

<details>
<summary>英文:</summary>

The `getDeviceTwin()` method in the Device SDK in Java works a little different from other languages. There is a really good sample [here][1]. 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:

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

client.subscribeToTwinDesiredProperties(desiredProperties);

System.out.println(&quot;Get device Twin...&quot;);
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(
      &quot;onProperty callback for &quot; + (property.getIsReported()?&quot;reported&quot;: &quot;desired&quot;) +
      &quot; property &quot; + property.getKey() +
      &quot; to &quot; + property.getValue() +
      &quot;, Properties version:&quot; + property.getVersion());
  }
}

huangapple
  • 本文由 发表于 2020年9月11日 03:49:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63836745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定