如何使用python IoT中心设备SDK从物联网设备中检索完整的设备孪生

时间:2019-01-28 12:57:49

标签: python azure azure-iot-hub

我正在使用python的IoT Hub设备SDK编写一个Azure物联网设备应用程序。

我在应用程序中使用了设备孪生状态的所需属性,并通过来自天蓝色的设备孪生更改通过device_twin_callback()更新了它们。 但是,当我重新配置设备时(例如在重新启动时),我得到了DPS中指定的初始设备孪生状态,而不是IoT中心的当前孪生状态。

在python中使用IoT中心设备SDK重新配置时,是否有办法检索当前设备的孪生状态?

我要避免的一种解决方案是将设备的最后状态保存在文件中。

1 个答案:

答案 0 :(得分:0)

  

在python中使用IoT中心设备SDK重新配置时,是否有办法检索当前设备的孪生状态?

您的问题不是准确性。没有其他的方法来检索当前设备双状态中,只有REST API Service - Get Twin和其相关的Python API iothub_twin_method.get_twin

重新配置时包含在设备状态数据中的设备孪生仅取决于Reprovisioning policies的选择,如下所示。

  
      
  • <强>重新提供和迁移数据:此策略为新登记的条目的默认值。当与注册条目关联的设备提交新请求(1)时,此策略将采取措施。根据注册条目的配置,可能会将设备重新分配给另一个IoT中心。如果设备正在更改IoT中心,则将删除在初始IoT中心的设备注册。从初始的IoT毂更新的设备状态信息将被迁移到新的IoT毂(2)。迁移期间,设备状态将报告为“正在分配”。

  •   
  • 重新设置并重置为初始配置:当与注册条目关联的设备提交新的设置请求(1)时,此策略将执行。根据注册条目的配置,可能会将设备重新分配给另一个IoT中心。如果设备正在更改IoT中心,则将删除在初始IoT中心的设备注册。初始配置数据,该供应服务实例处接收时,该设备被供应被提供给新的IoT毂(2)。在迁移过程中,设备的状态将被报告为勘定。   此策略通常用于恢复出厂设置,而无需更改IoT中心。

  •   
  • 永远不要重新配置:永远不会将设备重新分配到其他集线器。提供一种用于管理向后兼容性此策略。

  •   

使用适用于Python的Azure IoT SDK,重新配置策略由ReprovisionPolicy对象的参数update_hub_assignmentmigrate_device_data设置,请参见下面的说明。

The behavior of the service when a device is re-provisioned to an IoT hub.
All required parameters must be populated in order to send to Azure.
:param update_hub_assignment: Required. When set to true (default), the
 Device Provisioning Service will evaluate the device's IoT Hub assignment
 and update it if necessary for any provisioning requests beyond the first
 from a given device. If set to false, the device will stay assigned to its
 current IoT hub. Default value: True .
:type update_hub_assignment: bool
:param migrate_device_data: Required. When set to true (default), the
 Device Provisioning Service will migrate the device's data (twin, device
 capabilities, and device ID) from one IoT hub to another during an IoT hub
 assignment update. If set to false, the Device Provisioning Service will
 reset the device's data to the initial desired configuration stored in the
 corresponding enrollment list. Default value: True .
:type migrate_device_data: bool

ReprovisionPolicy对象是在EnrollmentGroupIndividualEnrollment对象中初始化的属性。

所以,请尝试更改的参数update_hub_assignment和{组合{1}} {的{1}}实现您的需求。否则,唯一的解决方案就是缓存历史数据。

相关问题