本文介绍了如何使用Python发送Device Twin的动态报告属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期更新IoT设备的所需属性.我要发送的所需属性如下:

I want to regularly update the desired properties for IoT Device. The desired properties that I am sending is as follows:

desired = {
    "para1" : {"time": [11,22,33]},
    "para2" : {"site":"demo.com"}
}

以下是根据文档

import time
import threading
from azure.iot.device import IoTHubModuleClient

CONNECTION_STRING = "IOTHUB Device Connection String"


def twin_update_listener(client):
    while True:
        patch = client.receive_twin_desired_properties_patch()  # blocking call
        print("Twin patch received:")
        print(patch)

def iothub_client_init():
    client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
    return client

def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(client,))
        twin_update_listener_thread.daemon = True
        twin_update_listener_thread.start()

        # Send reported
        print ( "Sending data as reported property..." )
        reported_patch = {"connectivity": "cellular"}
        client.patch_twin_reported_properties(reported_patch)
        print ( "Reported properties updated" )

        while True:
            time.sleep(1000000)
    except KeyboardInterrupt:
        print ( "IoT Hub Device Twin device sample stopped" )

if __name__ == '__main__':
    print ( "Starting the Python IoT Hub Device Twin device sample..." )
    print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
    iothub_client_sample_run()

在这里,它会将report_properties更新为一个静态值(reported_pa​​tch = {"connectivity":"cellular"}),但与此相反,我想上传与期望值相同的报告值:

Here it's updating reported_properties as a static value(reported_patch = {"connectivity": "cellular"}), but instead of this I want to upload the reported values same like the desired value:

{
        "para1" : {"time": [11,22,33]},
        "para2" : {"site":"demo.com"}
    }

请让我知道如何实现这一目标.

Please let me know how to achieve this.

推荐答案

尝试一下,一旦所需值更改,就将所需值上传到 reported :

Try this, upload desired value to reported once desired value changes :

import time
import threading
from azure.iot.device import IoTHubModuleClient

CONNECTION_STRING = ""


def twin_update_listener(client):
    while True:
        patch = client.receive_twin_desired_properties_patch()  # blocking call
        print("Twin patch received")

        patch.pop('$version',None)
        print ( "Sending Twin as reported property..." )
        print(patch)
        client.patch_twin_reported_properties(patch)
        print ( "Reported properties updated" )


def iothub_client_init():
    client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
    return client

def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(client,))
        twin_update_listener_thread.daemon = True
        twin_update_listener_thread.start()

        while True:
            time.sleep(1000)
    except KeyboardInterrupt:
        print ( "IoT Hub Device Twin device sample stopped" )

if __name__ == '__main__':
    print ( "Starting the Python IoT Hub Device Twin device sample..." )
    print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
    iothub_client_sample_run()

这篇关于如何使用Python发送Device Twin的动态报告属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 17:59