From a7fbfcb9485199f9b597064e34d903f6c6a58060 Mon Sep 17 00:00:00 2001 From: Gunter Tim Date: Sun, 25 Oct 2020 11:24:09 -0700 Subject: [PATCH] Add setURL() method to MeshInterface --- meshtastic/__init__.py | 13 +++++++++++++ meshtastic/__main__.py | 6 +----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 340adc0..6e8ecd1 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -208,6 +208,19 @@ class MeshInterface: s = base64.urlsafe_b64encode(bytes).decode('ascii') return f"https://www.meshtastic.org/c/#{s}" + def setURL(self, url, write=True): + """Set mesh network URL""" + if self.radioConfig == None: + raise Exception("No RadioConfig has been read") + + # URLs are of the form https://www.meshtastic.org/c/#{base64_channel_settings} + # Split on '/#' to find the base64 encoded channel settings + splitURL = url.split("/#") + decodedURL = base64.urlsafe_b64decode(splitURL[-1]) + self.radioConfig.channel_settings.ParseFromString(decodedURL) + if write: + self.writeConfig() + def _generatePacketId(self): """Get a new unique packet ID""" if self.currentPacketId is None: diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index b667361..4334eef 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -176,11 +176,7 @@ def onConnected(interface): # Handle set URL if args.seturl: - # URLs are of the form https://www.meshtastic.org/c/#{base64_channel_settings} - # Split on '/#' to find the base64 encoded channel settings - splitURL = args.seturl.split("/#") - bytes = base64.urlsafe_b64decode(splitURL[-1]) - interface.radioConfig.channel_settings.ParseFromString(bytes) + interface.setURL(args.seturl, False) print("Writing modified preferences to device") interface.writeConfig()