python中的iot边缘直接方法处理程序

2024-06-02 20:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为Bacnet扫描创建了一个模块,它将响应一个设备列表及其地址。但是我在python中实现直接方法处理程序时遇到了问题。当我第一次尝试自己实现它时,我遇到了这个错误。这可能意味着我没有成功注册直接方法回调。我有一些参考资料,但它来自C#,azure docs没有帮助我找到注册回调的正确方法。对于IoTubModuleClient,有一个on_方法请求和一个receive方法请求。谢谢你的帮助

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!')    

enter image description here

编辑: 这是我的路线配置 enter image description here


Tags: 方法nameclienttruescanresponserequestdevice
2条回答

我能够解决我的问题或至少找到根本原因,这是我在createOptions下的网络配置。当我尝试执行NetworkMode时,似乎出现了一个问题:通过连接带连接字符串从\u edge\u环境托管并连接到IoModuleClient.connect\u。我仍在尝试调整连接配置,但至少我知道它不在代码中

    async def method_request_handler(module_client):
        while  True:
            method_request = await module_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 == "method1":
                payload = {"result": True, "data": "some data"}  # set response payload
                status = 200  # set return status code
                print("executed method1")
            elif method_request.name == "method2":
                payload = {"result": True, "data": 1234}  # set response payload
                status = 200  # set return status code
                print("executed method2")
            else:
                payload = {"result": False, "data": "unknown method"}  # set response payload
                status = 400  # set return status code
                print("executed unknown method: " + method_request.name)
                    
            # Send the response
            method_response = MethodResponse.create_from_method_request(method_request, status, payload)
            await module_client.send_method_response(method_response)
            print('Message sent!')

    def stdin_listener():
        while True:
            try:
                selection = input("Press Q to quit\n")
                if selection == "Q" or selection == "q":
                    print("Quitting...")
                    break
            except:
                time.sleep(10)

    # Schedule task for C2D Listener
    listeners = asyncio.gather(input1_listener(module_client), twin_patch_listener(module_client), method_request_handler(module_client))

相关问题 更多 >