pythonembedded-linuxiotgatewaylora

json.dumps() won't return... not even after minutes


I have a LoRa gateway that runs Linux and it can handle Python apps. I'm using a Python file provided by the manufacturer. It mimics a simple node-RED app. Details can be found here.

Messages are received by the gateway in order, but the problem is that the json.dumps() method won't want to return after the call. I figured this out with putting print() functions into the original code.

Here is the important part of the code. The point is that the onMessage() function gets called when there is an uplink from an endpoint device and the onMessage() calls the rbPayloadFormatters(). I have never seen the result of the print(self.packet) line. When I let the rbPayloadFormatters() function return with the newMsg dictionary, I was able to see its content, printed from the onMessage() function.

### formats the payload message from the endpoint
def rbPayloadFormatters(self, msg):
    msgObj = json.loads(msg)
    newMsg = {}
    msgHex = base64.b64decode(msgObj["data"])
    newMsg["payload"] = binascii.hexlify(msgHex)
    newMsg["time"] = msgObj["tmst"]
    newMsg["snr"] = msgObj["lsnr"]
    newMsg["station"] = msgObj["appeui"]
    newMsg["avgsnr"] = msgObj["lsnr"]
    newMsg["lat"] = 0
    newMsg["lng"] = 0
    newMsg["rssi"] = msgObj["rssi"]
    newMsg["seqnumber"] = msgObj["seqn"]
    newMsg["deveui"] = msgObj["deveui"]
    newMsg["authorisation"] = self.rbAuthorization
    return json.dumps(newMsg)

#callback function initiated by on_message
def onMessage(self, mqtt_client, userdata, msg):
    self.packet = self.rbPayloadFormatters(msg.payload)
    pkt = json.loads(self.packet)
    self.devEUI = pkt["deveui"]
    self.payloadData = pkt["payload"]
    
    print(self.packet)

I have read that the stdlib json.dumps() can be slow but after minutes of waiting I was not able to see any printed json object on the console.

If you have any idea what is wrong, please don't hesitate to answer to this post. Thank you.


Solution

  • Today I continued the debugging and first tried Michael Butscher’s advice and started to comment out lines which add keys to newMsg.

    Turned out that the json.dumps() returns and the rbPayloadFormatters() returns as well if I comment out the next two lines.

    msgHex = base64.b64decode(msgObj["data"])
    newMsg["payload"] = binascii.hexlify(msgHex)
    

    The explanation for the never returning json.dumps() is the data type of the msgHex and newMsg["payload"]. Both of them has type bytes and json doesn’t support this type. (I think, I should have received an error… but I didn’t.)

    To solve my problem I simply converted the msgHex to string.

    newMsg["payload"] = str(binascii.hexlify(msgHex))
    

    Thank you everyone for the contribution.