From 9612aea9b9518abc2e387157cbb548fdf8caee68 Mon Sep 17 00:00:00 2001 From: Derek Arnold Date: Tue, 3 Sep 2024 21:47:07 -0500 Subject: [PATCH] Add in a retry mechanism for channel settings Attempts multiple times to fetch things over the admin channel before giving up. --- meshtastic/__main__.py | 6 ++++++ meshtastic/mesh_interface.py | 20 +++++++++++++++++--- meshtastic/node.py | 11 ++++++----- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index f78155d..ea9f6dd 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -1423,6 +1423,12 @@ def initParser(): action="append", ) + group.add_argument( + "--channel-fetch-retries", + help=("Attempt to retrieve channel settings for --ch-set this many times before giving up."), + default=3, + ) + group.add_argument( "--ch-vlongslow", help="Change to the very long-range and slow channel", diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index c59fcb3..b289668 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -314,7 +314,7 @@ class MeshInterface: # pylint: disable=R0902 return table def getNode( - self, nodeId: str, requestChannels: bool = True + self, nodeId: str, requestChannels: bool = True, requestChannelRetries: int = 3 ) -> meshtastic.node.Node: """Return a node object which contains device settings and channel info""" if nodeId in (LOCAL_ADDR, BROADCAST_ADDR): @@ -325,8 +325,22 @@ class MeshInterface: # pylint: disable=R0902 if requestChannels: logging.debug("About to requestChannels") n.requestChannels() - if not n.waitForConfig(): - our_exit("Error: Timed out waiting for channels") + retries_left = requestChannelRetries + last_index: int = 0 + while retries_left > 0: + retries_left -= 1 + if not n.waitForConfig(): + new_index: int = len(n.partialChannels) + # each time we get a new channel, reset the counter + if new_index != last_index: + retries_left = requestChannelRetries - 1 + if retries_left <= 0: + our_exit(f"Error: Timed out waiting for channels, giving up") + print("Timed out trying to retrieve channel info, retrying") + n.requestChannels(startingIndex=len(n.partialChannels)) + last_index = new_index + else: + break return n def sendText( diff --git a/meshtastic/node.py b/meshtastic/node.py index 48b9508..91c2d41 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -77,13 +77,14 @@ class Node: self.channels = channels self._fixupChannels() - def requestChannels(self): + def requestChannels(self, startingIndex: int = 0): """Send regular MeshPackets to ask channels.""" logging.debug(f"requestChannels for nodeNum:{self.nodeNum}") - self.channels = None - self.partialChannels = [] # We keep our channels in a temp array until finished - - self._requestChannel(0) + # only initialize if we're starting out fresh + if startingIndex == 0: + self.channels = None + self.partialChannels = [] # We keep our channels in a temp array until finished + self._requestChannel(startingIndex) def onResponseRequestSettings(self, p): """Handle the response packets for requesting settings _requestSettings()"""