[英]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_assignment
和migrate_device_data
的ReprovisionPolicy
对象,请参阅下面的交代。
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
对象是在EnrollmentGroup
或IndividualEnrollment
对象中初始化的属性。
So please try to change the combination of parameters update_hub_assignment
and migrate_device_data
of ReprovisionPolicy
to realize your needs. 所以,请尝试更改参数的组合update_hub_assignment
和migrate_device_data
的ReprovisionPolicy
实现您的需求。 Otherwise, your workaround to cache the historial data is the only solution. 否则,唯一的解决方案就是缓存历史数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.