From f4bdc07c2220df2bb2137bd2b9bd98988cd7710c Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 1 May 2020 22:48:33 -0700 Subject: [PATCH] use thread for testing --- meshtastic/__init__.py | 20 +++++++++++++++++--- meshtastic/test.py | 8 +++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 38ecb84..f18e650 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -91,7 +91,7 @@ class MeshInterface: text {string} -- The text to send Keyword Arguments: - destinationId {nodeId} -- where to send this message (default: {BROADCAST_ADDR}) + destinationId {nodeId or nodeNum} -- where to send this message (default: {BROADCAST_ADDR}) """ self.sendData(text.encode("utf-8"), destinationId, dataType=mesh_pb2.Data.CLEAR_TEXT) @@ -108,7 +108,16 @@ class MeshInterface: You probably don't want this - use sendData instead.""" toRadio = mesh_pb2.ToRadio() # FIXME add support for non broadcast addresses - meshPacket.to = 255 + + if isinstance(destinationId, int): + nodeNum = destinationId + elif nodenum == BROADCAST_ADDR: + nodeNum = 255 + else: + raise Exception( + "invalid destination addr, we don't yet support nodeid lookup") + + meshPacket.to = nodeNum toRadio.packet.CopyFrom(meshPacket) self._sendToRadio(toRadio) @@ -214,6 +223,7 @@ class StreamInterface(MeshInterface): devPath = ports[0] logging.debug(f"Connecting to {devPath}") + self.devPath = devPath self._rxBuf = bytes() # empty self._wantExit = False self.stream = serial.Serial( @@ -237,6 +247,7 @@ class StreamInterface(MeshInterface): logging.debug("Closing serial stream") # pyserial cancel_read doesn't seem to work, therefore we ask the reader thread to close things for us self._wantExit = True + self._rxThread.join() # wait for it to exit def __reader(self): """The reader thread that reads bytes from our stream""" @@ -276,10 +287,13 @@ class StreamInterface(MeshInterface): try: self._handleFromRadio(self._rxBuf[HEADER_LEN:]) except Exception as ex: - logging.warn( + logging.error( f"Error handling FromRadio, possibly corrupted? {ex}") traceback.print_exc() self._rxBuf = empty + else: + # logging.debug(f"timeout on {self.devPath}") + pass logging.debug("reader is exiting") self.stream.close() self._disconnected() diff --git a/meshtastic/test.py b/meshtastic/test.py index 9ac4a5c..ff7a235 100644 --- a/meshtastic/test.py +++ b/meshtastic/test.py @@ -3,6 +3,7 @@ from . import util from . import StreamInterface from pubsub import pub import time +import threading """The interfaces we are using for our tests""" interfaces = None @@ -60,13 +61,13 @@ def testSend(fromInterface, toInterface): return False -def startTests(): +def testThread(): logging.info("Found devices, starting tests...") while True: global testNumber testNumber = testNumber + 1 testSend(interfaces[0], interfaces[1]) - time.sleep(20) + time.sleep(10) def onConnection(topic=pub.AUTO_TOPIC): @@ -76,7 +77,8 @@ def onConnection(topic=pub.AUTO_TOPIC): global testsRunning if (all(iface.isConnected for iface in interfaces) and not testsRunning): testsRunning = True - startTests() + t = threading.Thread(target=testThread, args=()) + t.start() def openDebugLog(portName):