Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

iot edge direct method handler in python

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 (
        "
Method callback called with:
methodName = {method_name}
payload = {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

Edit: Here is my route config enter image description here

question from:https://stackoverflow.com/questions/65865673/iot-edge-direct-method-handler-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...