【问题标题】:iot edge direct method handler in pythonpython中的iot edge直接方法处理程序
【发布时间】:2021-01-23 23:36:00
【问题描述】:

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

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

编辑: 这是我的路线配置

【问题讨论】:

  • 你检查过this sample吗?它使用设备客户端,但模块客户端应具有相同的on_method_request_received 属性以分配方法处理程序。
  • 我也试过了。但它仍然返回相同的错误。我无法在本地运行它来测试,所以我总是先将它部署到设备上。我想知道我的路线是不是错了?

标签: python iot azure-iot-hub azure-iot-edge


【解决方案1】:

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

【讨论】:

    【解决方案2】:
        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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多