@@ -337,14 +337,19 @@ def getURL(self, includeAll: bool = True):
337
337
s = s .replace ("=" , "" ).replace ("+" , "-" ).replace ("/" , "_" )
338
338
return f"https://meshtastic.org/e/#{ s } "
339
339
340
- def setURL (self , url ):
340
+ def setURL (self , url : str , addOnly : bool = False ):
341
341
"""Set mesh network URL"""
342
- if self .localConfig is None :
343
- our_exit ("Warning: No Config has been read " )
342
+ if self .localConfig is None or self . channels is None :
343
+ our_exit ("Warning: config or channels not loaded " )
344
344
345
345
# URLs are of the form https://meshtastic.org/d/#{base64_channel_set}
346
346
# Split on '/#' to find the base64 encoded channel settings
347
- splitURL = url .split ("/#" )
347
+ if addOnly :
348
+ splitURL = url .split ("/?add=true#" )
349
+ else :
350
+ splitURL = url .split ("/#" )
351
+ if len (splitURL ) == 1 :
352
+ our_exit (f"Warning: Invalid URL '{ url } '" )
348
353
b64 = splitURL [- 1 ]
349
354
350
355
# We normally strip padding to make for a shorter URL, but the python parser doesn't like
@@ -361,20 +366,36 @@ def setURL(self, url):
361
366
if len (channelSet .settings ) == 0 :
362
367
our_exit ("Warning: There were no settings." )
363
368
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
369
+ if addOnly :
370
+ # Add new channels with names not already present
371
+ # Don't change existing channels
372
+ for chs in channelSet .settings :
373
+ channelExists = self .getChannelByName (chs .name )
374
+ if channelExists or chs .name == "" :
375
+ print (f"Ignoring existing or empty channel \" { chs .name } \" from add URL" )
376
+ continue
377
+ ch = self .getDisabledChannel ()
378
+ if not ch :
379
+ our_exit ("Warning: No free channels were found" )
380
+ ch .settings .CopyFrom (chs )
381
+ ch .role = channel_pb2 .Channel .Role .SECONDARY
382
+ print (f"Adding new channel '{ chs .name } ' to device" )
383
+ self .writeChannel (ch .index )
384
+ else :
385
+ i = 0
386
+ for chs in channelSet .settings :
387
+ ch = channel_pb2 .Channel ()
388
+ ch .role = (
389
+ channel_pb2 .Channel .Role .PRIMARY
390
+ if i == 0
391
+ else channel_pb2 .Channel .Role .SECONDARY
392
+ )
393
+ ch .index = i
394
+ ch .settings .CopyFrom (chs )
395
+ self .channels [ch .index ] = ch
396
+ logging .debug (f"Channel i:{ i } ch:{ ch } " )
397
+ self .writeChannel (ch .index )
398
+ i = i + 1
378
399
379
400
p = admin_pb2 .AdminMessage ()
380
401
p .set_config .lora .CopyFrom (channelSet .lora_config )
0 commit comments