Azure functions fail to decode IoThub device connection state message

Nayangiri Goswami 45 Reputation points
2024-09-16T14:06:08.8466667+00:00

0

I've a Azure function in Python 3.11 and Linux hosting. It is throwing an error when trigger is service bus queue message, which is having IoTHub device connection state message with schema like this

schema.Result: Failure Exception: ValueError: cannot convert value of field 'SequenceNumber' in trigger metadata into int: invalid literal for int() with base 10: '000000000000000001dummy100000004000000000000000000, File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 570, in _handle__invocation_request args[pb.name] = bindings.from_incoming_proto

function code

@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="iothub-device-lifecycle-events",
                               connection="ServiceBusConnection")
def genaiot_servicebus_dlm_queue_trigger(azservicebus: func.ServiceBusMessage):
    logging.info('Python ServiceBus Queue trigger processed a message: %s',
                azservicebus.get_body().decode('utf-8'))
Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
625 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,991 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,189 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 15,321 Reputation points Microsoft Employee
    2024-09-18T15:56:31.8233333+00:00

    Hi @Nayangiri Goswami Greetings! Thank you for posting this question here.

    I could reproduce the same issue you are facing with Python application when trying to process a message triggered from Device Connection Status event generated from IoT Hub through event routing.

    I notice that the message body for the message generated through this event consists only a sequenceNumber property in the above-mentioned Base 10 format. The function app is trying to format this sequnceNumber property to an int even before entering the code which is leading to the above error.

    Instead of going with routing in IoT Hub, use Events under IoT Hub and create an Event subscription to filter Device Connected and Device Disconnected event types. Choose the end point as Service Bus Queue and point to your queue to send the events. Please find the below image for reference

    User's image

    Once you have this configured, disable any routes you may have under the IoT Hub that sends Device Connection status to the Service Bus Queue end point.

    The message delivered to the queue using the event subscription have a different template. Please find the below message body for reference

    User's image

    You can then use the below Function app code to process the message body and extract the sequence number

    @app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="lsayanaiot",
                                   connection="lsayanaservicebus_SERVICEBUS") 
    def lsayana_servicebus_queue_trigger(azservicebus: func.ServiceBusMessage):
        message_body = json.loads(azservicebus.get_body().decode('utf-8'))
        logging.info('Message body is %s', message_body)
        sequence_number = message_body['data']['deviceConnectionStateEventInfo']['sequenceNumber']
        logging.info('Python ServiceBus Queue trigger processed a message: %s',
                    sequence_number)
    
    
    

    Hope this helps! Please let us know if you have any additional questions or need further assistance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.