简体   繁体   English

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

[英]How to retrieve the full device twin from an iot device with the python IoT Hub Device SDK

I'm writing an azure iot device application with the IoT Hub Device SDK in python. 我正在用python的IoT中心设备SDK编写一个Azure物联网设备应用程序。

I use the desired properties of the device twin state in my application and update them via the device_twin_callback() on device twin changes coming from azure. 我在应用程序中使用了设备孪生状态的所需属性,并通过来自天蓝色的设备孪生更改通过device_twin_callback()更新了它们。 However, when I reprovision my device (eg on reboot), I get the initial device twin state specified in the DPS and not the current twin state of the IoT Hub. 但是,当我重新配置设备时(例如在重新启动时),我得到了DPS中指定的初始设备孪生状态,而不是IoT中心的当前孪生状态。

Is there a way to retrieve the current device twin state on reprovisioning with the IoT Hub Device SDK in python? 在python中使用IoT中心设备SDK重新配置时,是否有办法检索当前设备的孪生状态?

One solution I would like to avoid is to save the last state of the device in a file. 我要避免的一种解决方案是将设备的最后状态保存在文件中。

Is there a way to retrieve the current device twin state on reprovisioning with the IoT Hub Device SDK in python? 在python中使用IoT中心设备SDK重新配置时,是否有办法检索当前设备的孪生状态?

Your question is not accuracy. 您的问题不是准确性。 There is not other ways to retrieve the current device twin state, only the REST API Service - Get Twin and its related Python API iothub_twin_method.get_twin . 没有其他方法可以检索当前设备的孪生状态,只有REST API Service - Get Twin iothub_twin_method.get_twin Service - Get Twin及其相关的Python API iothub_twin_method.get_twin

The device twin included in device state data on reprovisioning is only depended on the choice of Reprovisioning policies , as below. 重新配置时,包含在设备状态数据中的设备孪生仅取决于重新配置Reprovisioning policies的选择,如下所示。

  • Re-provision and migrate data : This policy is the default for new enrollment entries. 重新供应和迁移数据 :此策略是新注册条目的默认设置。 This policy takes action when devices associated with the enrollment entry submit a new request (1). 当与注册条目关联的设备提交新请求(1)时,此策略将采取措施。 Depending on the enrollment entry configuration, the device may be reassigned to another IoT hub. 根据注册条目的配置,可能会将设备重新分配给另一个IoT中心。 If the device is changing IoT hubs, the device registration with the initial IoT hub will be removed. 如果设备正在更改IoT中心,则将删除在初始IoT中心的设备注册。 The updated device state information from that initial IoT hub will be migrated over to the new IoT hub (2). 来自该初始IoT中心的更新的设备状态信息将被迁移到新的IoT中心(2)。 During migration, the device's status will be reported as Assigning. 在迁移期间,设备的状态将报告为“正在分配”。

  • Re-provision and reset to initial config : This policy takes action when devices associated with the enrollment entry submit a new provisioning request (1). 重新配置并重置为初始配置 :当与注册条目关联的设备提交新的配置请求(1)时,此策略将采取措施。 Depending on the enrollment entry configuration, the device may be reassigned to another IoT hub. 根据注册条目的配置,可能会将设备重新分配给另一个IoT中心。 If the device is changing IoT hubs, the device registration with the initial IoT hub will be removed. 如果设备正在更改IoT中心,则将删除在初始IoT中心的设备注册。 The initial configuration data that the provisioning service instance received when the device was provisioned is provided to the new IoT hub (2). 调配设备时调配服务实例收到的初始配置数据将提供给新的IoT中心(2)。 During migration, the device's status will be reported as Assigning. 在迁移期间,设备的状态将报告为“正在分配”。 This policy is often used for a factory reset without changing IoT hubs. 此策略通常用于出厂重置,而无需更改IoT中心。

  • Never re-provision : The device is never reassigned to a different hub. 永不重新配置 :永不将设备重新分配到其他集线器。 This policy is provided for managing backwards compatibility. 提供此策略是为了管理向后兼容性。

Using Azure IoT SDK for Python, the reprovisioning policy is set by the parameters update_hub_assignment and migrate_device_data of ReprovisionPolicy object, please see the explaination below. 使用Azure的物联网SDK的Python,再供应政策是由参数设定update_hub_assignmentmigrate_device_dataReprovisionPolicy对象,请参阅下面的交代。

 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 

And the ReprovisionPolicy object is as a property initialized in the EnrollmentGroup or IndividualEnrollment object. ReprovisionPolicy对象是在EnrollmentGroupIndividualEnrollment对象中初始化的属性。

So please try to change the combination of parameters update_hub_assignment and migrate_device_data of ReprovisionPolicy to realize your needs. 所以,请尝试更改参数的组合update_hub_assignmentmigrate_device_dataReprovisionPolicy实现您的需求。 Otherwise, your workaround to cache the historial data is the only solution. 否则,唯一的解决方案就是缓存历史数据。

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

相关问题 如何使用Azure Python设备SDK从设备更新设备孪生? - How to update device twin from device with Azure Python device SDK? Python 虚拟设备句柄更改事件来自 azure IoT 集线器 - Python virtual device handle changed event from azure IoT hub 如何在物联网设备上安装 gRPC python package? - How to install gRPC python package on IoT device? 使用 aws-iot-device-sdk-python-v2 发布到设备影子的问题 - Issues publishing to device shadow using the aws-iot-device-sdk-python-v2 GCP - Python 创建 IoT 设备 PermissionDenied - GCP - Python create IoT device PermissionDenied 无法向 Azure IoT 中心向设备 ID 中带有“:”的设备发送消息 - Cannot send message to Azure IoT Hub to units with ":" in their device id “设备连接字符串不正确。”错误-Azure IoT-Hub - “Device connection string is not correct.” Error - Azure IoT-Hub 无法将云发送到设备消息到 Azure 物联网中心 - Not able to send Cloud to Device Message to Azure IoT hub 无法将我的模拟设备连接到Azure IoT中心以使用Python发送数据 - Failing to connect my simulated device to azure IoT hub to send the data using Python Azure 物联网中心设备 x509 自签名证书 (Python MQTT) - Azure IoT Hub Device x509 Self-Signed Certificate (Python MQTT)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM