diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index b24b1ab..da43f4a 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -184,8 +184,9 @@ class MeshInterface: - meshtastic.receive.data(packet = MeshPacket dictionary) """ # FIXME, update node DB as needed - # We provide our objects as DotMaps - which work with . notation or as dictionaries - asObj = DotMap(google.protobuf.json_format.MessageToDict(meshPacket)) + asDict = google.protobuf.json_format.MessageToDict(meshPacket) + # We could provide our objects as DotMaps - which work with . notation or as dictionaries + #asObj = DotMap(asDict) topic = None if meshPacket.payload.HasField("position"): topic = "meshtastic.receive.position" @@ -198,7 +199,7 @@ class MeshInterface: # asObj.payload.data.text = asObj.payload.data.payload.decode( # "utf-8") - pub.sendMessage(topic, packet=asObj, interface=self) + pub.sendMessage(topic, packet=asDict, interface=self) class StreamInterface(MeshInterface): diff --git a/meshtastic/test.py b/meshtastic/test.py index daa9705..88fdc8f 100644 --- a/meshtastic/test.py +++ b/meshtastic/test.py @@ -5,6 +5,7 @@ from pubsub import pub import time import sys import threading +from dotmap import DotMap """The interfaces we are using for our tests""" interfaces = None @@ -17,12 +18,14 @@ testsRunning = False testNumber = 0 -def onReceive(packet): +def onReceive(packet, interface): """Callback invoked when a packet arrives""" - print(f"Received: {packet}") - if packet.payload.data.typ == "CLEAR_TEXT": + print(f"From {interface.devPath}: {packet}") + p = DotMap(packet) + + if p.payload.data.typ == "CLEAR_TEXT": # We only care a about clear text packets - receivedPackets.append(packet) + receivedPackets.append(p) def onNode(node): diff --git a/meshtastic/util.py b/meshtastic/util.py index 967f289..34130d3 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -1,4 +1,5 @@ +from collections import defaultdict import serial import serial.tools.list_ports @@ -14,3 +15,10 @@ def findPorts(): serial.tools.list_ports.comports()))) l.sort() return l + + +class dotdict(dict): + """dot.notation access to dictionary attributes""" + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__