python中的IoT边缘直接方法处理程序 [英] iot edge direct method handler in python

查看:80
本文介绍了python中的IoT边缘直接方法处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用于Bacnet扫描的模块,它将以设备列表及其地址作为响应.但是我在用python实现直接方法处理程序时遇到了麻烦.当我自己第一次尝试实现它时,我遇到了这个错误.这可能意味着我没有成功注册直接方法回调.我有一些参考,但是它来自C#,而azure docs不能帮助我找出正确的方法来注册回调.对于IoTHubModuleClient,有一个on_method_request_received和一个receive_method_request.感谢您的帮助!

I have created a module for a Bacnet scan and it will respond with a list of devices and its address as a result. But I'm having trouble implementing a direct method handler in python. When i first tried implementing it myself i got this error. Which could mean I didn't successfully register the direct method callback. I have some references but it was from C# and azure docs is not helping me figure out the right method to register the callback. for IoTHubModuleClient there's a on_method_request_received and a receive_method_request. appreciate any help!

def iothub_client_scan_run():
try:
    iot_client = iothub_client_init()

    bacnet_scan_listener_thread = threading.Thread(target=device_method_listener, args=(iot_client,))
    bacnet_scan_listener_thread.daemon = True
    bacnet_scan_listener_thread.start()

    while True:
        time.sleep(1000)
    
def device_method_listener(iot_client):
 while True:
        # Receive the direct method request
    method_request = iot_client.receive_method_request()
    
    print (
        "\nMethod callback called with:\nmethodName = {method_name}\npayload = {payload}".format(
            method_name=method_request.name,
            payload=method_request.payload
        )
    )
    if method_request.name == "runBacnetScan":
        response = bacnet_scan_device(method_request)
    else:
        response_payload = {"Response": "Direct method {} not defined".format(method_request.name)}
        response_status = 404
            
    # Send a method response indicating the method request was resolved
    print('Sending method response')
    iot_client.send_method_response(response)
    print('Message sent!')    

这是我的路线配置

推荐答案

我能够解决我的问题,或者至少找到了根本原因,这是我在createOptions下的网络配置.尝试执行NetworkMode时似乎出现问题:主机并通过连接字符串连接到IotModuleClient.connect_from_edge_environment.我仍在尝试调整连接配置,但至少我知道它不在代码中.

I was able to resolve my issue or at least find the root cause and it was my network configuration under the createOptions. It seems like there's an issue when I'm trying to do NetworkMode: host and connects to the IotModuleClient.connect_from_edge_environment via connect with connection string. I'm still trying to tweak the connection configuration but at least i know its not on the code.

这篇关于python中的IoT边缘直接方法处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆