【发布时间】: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