From 25c58604e6491e25b336df709f1c005256309d6c Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 26 Feb 2021 18:57:21 +0800 Subject: [PATCH] move protocols dict to EOF to avoid confusing new users --- meshtastic/__init__.py | 84 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 2908413..5cff7d3 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -104,47 +104,6 @@ class KnownProtocol(NamedTuple): protobufFactory: Callable = None # If set, will be called to prase as a protocol buffer onReceive: Callable = None # If set, invoked as onReceive(interface, packet) -def _onTextReceive(iface, asDict): - """Special text auto parsing for received messages""" - # We don't throw if the utf8 is invalid in the text message. Instead we just don't populate - # the decoded.data.text and we log an error message. This at least allows some delivery to - # the app and the app can deal with the missing decoded representation. - # - # Usually btw this problem is caused by apps sending binary data but setting the payload type to - # text. - try: - asDict["decoded"]["text"] = meshPacket.decoded.payload.decode( - "utf-8") - except Exception as ex: - logging.error(f"Malformatted utf8 in text message: {ex}") - -def _onPositionReceive(iface, asDict): - """Special auto parsing for received messages""" - p = asDict["decoded"]["position"] - iface._fixupPosition(p) - # update node DB as needed - iface._getOrCreateByNum(asDict["from"])["position"] = p - -def _onNodeInfoReceive(iface, asDict): - """Special auto parsing for received messages""" - p = asDict["decoded"]["user"] - # decode user protobufs and update nodedb, provide decoded version as "position" in the published msg - # update node DB as needed - n = iface._getOrCreateByNum(asDict["from"]) - n["user"] = p - # We now have a node ID, make sure it is uptodate in that table - iface.nodes[p["id"]] = n - - -"""Well known message payloads can register decoders for automatic protobuf parsing""" -protocols = { - portnums_pb2.PortNum.TEXT_MESSAGE_APP: KnownProtocol("text", onReceive=_onTextReceive), - portnums_pb2.PortNum.POSITION_APP: KnownProtocol("position", mesh_pb2.Position, _onPositionReceive), - portnums_pb2.PortNum.NODEINFO_APP: KnownProtocol("user", mesh_pb2.User, _onNodeInfoReceive), - portnums_pb2.PortNum.ADMIN_APP: KnownProtocol("admin", admin_pb2.AdminMessage), - portnums_pb2.PortNum.ENVIRONMENTAL_MEASUREMENT_APP: KnownProtocol("environmental", environmental_measurement_pb2.EnvironmentalMeasurement), - portnums_pb2.PortNum.REMOTE_HARDWARE_APP: KnownProtocol("remotehw", remote_hardware_pb2.HardwareMessage) -} class MeshInterface: """Interface class for meshtastic devices @@ -870,3 +829,46 @@ class TCPInterface(StreamInterface): def _readBytes(self, len): """Read an array of bytes from our stream""" return self.socket.recv(len) + + +def _onTextReceive(iface, asDict): + """Special text auto parsing for received messages""" + # We don't throw if the utf8 is invalid in the text message. Instead we just don't populate + # the decoded.data.text and we log an error message. This at least allows some delivery to + # the app and the app can deal with the missing decoded representation. + # + # Usually btw this problem is caused by apps sending binary data but setting the payload type to + # text. + try: + asDict["decoded"]["text"] = meshPacket.decoded.payload.decode( + "utf-8") + except Exception as ex: + logging.error(f"Malformatted utf8 in text message: {ex}") + +def _onPositionReceive(iface, asDict): + """Special auto parsing for received messages""" + p = asDict["decoded"]["position"] + iface._fixupPosition(p) + # update node DB as needed + iface._getOrCreateByNum(asDict["from"])["position"] = p + +def _onNodeInfoReceive(iface, asDict): + """Special auto parsing for received messages""" + p = asDict["decoded"]["user"] + # decode user protobufs and update nodedb, provide decoded version as "position" in the published msg + # update node DB as needed + n = iface._getOrCreateByNum(asDict["from"]) + n["user"] = p + # We now have a node ID, make sure it is uptodate in that table + iface.nodes[p["id"]] = n + + +"""Well known message payloads can register decoders for automatic protobuf parsing""" +protocols = { + portnums_pb2.PortNum.TEXT_MESSAGE_APP: KnownProtocol("text", onReceive=_onTextReceive), + portnums_pb2.PortNum.POSITION_APP: KnownProtocol("position", mesh_pb2.Position, _onPositionReceive), + portnums_pb2.PortNum.NODEINFO_APP: KnownProtocol("user", mesh_pb2.User, _onNodeInfoReceive), + portnums_pb2.PortNum.ADMIN_APP: KnownProtocol("admin", admin_pb2.AdminMessage), + portnums_pb2.PortNum.ENVIRONMENTAL_MEASUREMENT_APP: KnownProtocol("environmental", environmental_measurement_pb2.EnvironmentalMeasurement), + portnums_pb2.PortNum.REMOTE_HARDWARE_APP: KnownProtocol("remotehw", remote_hardware_pb2.HardwareMessage) +}