python icon indicating copy to clipboard operation
python copied to clipboard

mesh_interface._handlePacketFromRadio() returning MessageToDict, can't be serialized as JSON again?

Open ampledata opened this issue 1 year ago • 0 comments

I've got a test program subscribed to the meshtastic.receive topic:

import json
import time

import meshtastic
import meshtastic.serial_interface

from pubsub import pub

def onReceive(packet, interface):
    d = packet.get("decoded")
    del d["payload"]
    json.dumps(d)

def onConnection(interface, topic=pub.AUTO_TOPIC):
    pass

pub.subscribe(onReceive, "meshtastic.receive")
pub.subscribe(onConnection, "meshtastic.connection.established")

interface = meshtastic.serial_interface.SerialInterface("/dev/cu.usbserial-0001")

while 1:
    time.sleep(0.01)

Packets hit my callback function as Python dicts, which is OK for native Python - however - I'd like to post these packets to a REST API as JSON.

Unfortunately the packets returned to my callback from this topic can't be encoded with the native Python json.dumps() as it will raise TypeError for each of the Protobuf message types:

ERROR:root:Unexpected error in deferred execution <class 'TypeError'>
Traceback (most recent call last):
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/meshtastic/util.py", line 243, in _run
    o()
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/meshtastic/mesh_interface.py", line 1040, in <lambda>
    lambda: pub.sendMessage(topic, packet=asDict, interface=self)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/pubsub/core/publisher.py", line 216, in sendMessage
    topicObj.publish(**msgData)
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/pubsub/core/topicobj.py", line 452, in publish
    self.__sendMessage(msgData, topicObj, msgDataSubset)
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/pubsub/core/topicobj.py", line 482, in __sendMessage
    listener(data, self, allData)
  File "/Users/gba/.pyenv/versions/TAK-3.12.1/lib/python3.12/site-packages/pubsub/core/listener.py", line 237, in __call__
    cb(**kwargs)
  File "/Users/gba/src/SNS/meshtastic/test.py", line 19, in onReceive
    json.dumps(d)
  File "/Users/gba/.pyenv/versions/3.12.1/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gba/.pyenv/versions/3.12.1/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gba/.pyenv/versions/3.12.1/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/gba/.pyenv/versions/3.12.1/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AdminMessage is not JSON serializable

From my cursory Googling, it doesn't seem like there's an existing way to convert a MessageToDict packet back into a MessageToJSON packet.

Before I go an implement a native call to MessageToJSON in the Meshtastic Python client, is there something I'm missing?

ampledata avatar Mar 18 '24 07:03 ampledata