Skip to content

Commit 579383c

Browse files
committed
Add new channels from an add URL with the new --ch-add-url option
1 parent 03ac322 commit 579383c

2 files changed

Lines changed: 54 additions & 17 deletions

File tree

meshtastic/__main__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ def onConnected(interface):
680680

681681
# handle changing channels
682682

683+
if args.ch_add_url:
684+
closeNow = True
685+
interface.getNode(args.dest, **getNode_kwargs).setURL(args.ch_add_url, addOnly=True)
686+
683687
if args.ch_add:
684688
channelIndex = mt_config.channel_index
685689
if channelIndex is not None:
@@ -1443,7 +1447,7 @@ def addConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
14431447
"--set-ham", help="Set licensed Ham ID and turn off encryption", action="store"
14441448
)
14451449

1446-
group.add_argument("--seturl", help="Set a channel URL", action="store")
1450+
group.add_argument("--seturl", help="Set all channels with a URL", action="store")
14471451

14481452
return parser
14491453

@@ -1461,6 +1465,13 @@ def addChannelConfigArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPa
14611465
default=None,
14621466
)
14631467

1468+
group.add_argument(
1469+
"--ch-add-url",
1470+
help="Add secondary channels from a supplied URL",
1471+
metavar="URL",
1472+
default=None,
1473+
)
1474+
14641475
group.add_argument(
14651476
"--ch-del", help="Delete the ch-index channel", action="store_true"
14661477
)

meshtastic/node.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Timeout,
1313
camel_to_snake,
1414
fromPSK,
15+
genPSK256,
1516
our_exit,
1617
pskToString,
1718
stripnl,
@@ -337,14 +338,19 @@ def getURL(self, includeAll: bool = True):
337338
s = s.replace("=", "").replace("+", "-").replace("/", "_")
338339
return f"https://meshtastic.org/e/#{s}"
339340

340-
def setURL(self, url):
341+
def setURL(self, url, addOnly: bool = False):
341342
"""Set mesh network URL"""
342343
if self.localConfig is None:
343344
our_exit("Warning: No Config has been read")
344345

345346
# URLs are of the form https://meshtastic.org/d/#{base64_channel_set}
346347
# Split on '/#' to find the base64 encoded channel settings
347-
splitURL = url.split("/#")
348+
if addOnly:
349+
splitURL = url.split("/?add=true#")
350+
else:
351+
splitURL = url.split("/#")
352+
if len(splitURL) == 1:
353+
our_exit(f"Warning: Invalid URL '{url}'")
348354
b64 = splitURL[-1]
349355

350356
# We normally strip padding to make for a shorter URL, but the python parser doesn't like
@@ -361,20 +367,40 @@ def setURL(self, url):
361367
if len(channelSet.settings) == 0:
362368
our_exit("Warning: There were no settings.")
363369

364-
i = 0
365-
for chs in channelSet.settings:
366-
ch = channel_pb2.Channel()
367-
ch.role = (
368-
channel_pb2.Channel.Role.PRIMARY
369-
if i == 0
370-
else channel_pb2.Channel.Role.SECONDARY
371-
)
372-
ch.index = i
373-
ch.settings.CopyFrom(chs)
374-
self.channels[ch.index] = ch
375-
logging.debug(f"Channel i:{i} ch:{ch}")
376-
self.writeChannel(ch.index)
377-
i = i + 1
370+
if addOnly:
371+
# Add new channels with names not already present
372+
# Don't change existing channels
373+
for ch in channelSet.settings:
374+
channelExists = self.getChannelByName(ch.name)
375+
if channelExists or ch.name == "":
376+
print("Ignoring existing channel from add URL")
377+
next
378+
else:
379+
newChannel = self.getDisabledChannel()
380+
if not newChannel:
381+
our_exit("Warning: No free channels were found")
382+
chs = channel_pb2.ChannelSettings()
383+
chs.name = ch.name
384+
chs.psk = ch.psk
385+
newChannel.settings.CopyFrom(chs)
386+
newChannel.role = channel_pb2.Channel.Role.SECONDARY
387+
print(f"Adding new channel '{ch.name}' to device")
388+
self.writeChannel(newChannel.index)
389+
else:
390+
i = 0
391+
for chs in channelSet.settings:
392+
ch = channel_pb2.Channel()
393+
ch.role = (
394+
channel_pb2.Channel.Role.PRIMARY
395+
if i == 0
396+
else channel_pb2.Channel.Role.SECONDARY
397+
)
398+
ch.index = i
399+
ch.settings.CopyFrom(chs)
400+
self.channels[ch.index] = ch
401+
logging.debug(f"Channel i:{i} ch:{ch}")
402+
self.writeChannel(ch.index)
403+
i = i + 1
378404

379405
p = admin_pb2.AdminMessage()
380406
p.set_config.lora.CopyFrom(channelSet.lora_config)

0 commit comments

Comments
 (0)