繁体   English   中英

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

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

这给出了服务设备sdk 的段落摘要:

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

嗨,我有两个存储库或我正在处理的项目,一个是使用 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 所需的属性变得非常容易。 我只需要一个DeviceTwinDevice对象,然后在它上面调用getDesiredProperties() 这一切都来自依赖:

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

现在,我正在处理另一个存储库,我必须在其中读取设备孪生中的特定属性,但该项目使用的是 Azure Iot Hub设备SDK(此处为 Java 的文档 API: https : //docs.microsoft.com/ en-us/java/api/com.microsoft.azure.sdk.iot.device?view=azure-java-stable ),它的工作原理有点不同。 看起来他们使用DeviceClient对象来连接物DeviceClient集线器等。 除了getDeviceTwin()方法之外,我没有看到任何检索 DeviceTwin 所需属性的方法,但它是一个 void 方法并且不返回任何内容? 对此的依赖是

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

对于之前没有见过这些“属性”的人,它只是位于 Azure 门户网站上的 JSON: 在此处输入图片说明

是否有一种简单的方法可以使用设备sdk 获取这些属性,或者我必须在 Gradle 中拖动服务sdk 的依赖项并这样做? 似乎是多余的。 请帮忙!

Java 中 Device SDK 中的getDeviceTwin()方法与其他语言的工作方式略有不同。 有一个很好的样本在这里 神奇的是在调用getDeviceTwin()的几行getDeviceTwin() ,您首先定义要为其注册回调的属性。 以下示例均来自上述链接:

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.

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

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