From b02138934d0c04103870f8e4bb9885be7109341f Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sun, 21 Mar 2021 11:17:44 +0800 Subject: [PATCH] clean up option names --- meshtastic/__init__.py | 30 ++++++++++++++-------- meshtastic/__main__.py | 58 ++++++++++++++++++++++++------------------ meshtastic/util.py | 5 ++-- 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 27660b2..ad817db 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -136,15 +136,24 @@ class Node: self.radioConfig = None self.channels = None - def showInfo(self): - """Show human readable description of our node""" - print(self.radioConfig) + def showChannels(self): + """Show human readable description of our channels""" print("Channels:") for c in self.channels: if c.role != channel_pb2.Channel.Role.DISABLED: - cStr = MessageToJson(c.settings).replace("\n", "") + cStr = stripnl(MessageToJson(c.settings)) print(f" {channel_pb2.Channel.Role.Name(c.role)} {cStr}") - print(f"\nChannel URL {self.channelURL}") + publicURL = self.getURL(includeAll = False) + adminURL = self.getURL(includeAll = True) + print(f"\nPrimary channel URL: {publicURL}") + if adminURL != publicURL: + print(f"Admin URL (includes all channels): {adminURL}") + + def showInfo(self): + """Show human readable description of our node""" + print(f"Preferences: {stripnl(MessageToJson(self.radioConfig.preferences))}\n") + self.showChannels() + def requestConfig(self): """ @@ -254,14 +263,13 @@ class Node: return self._sendAdmin(p) - @property - def channelURL(self): + def getURL(self, includeAll: bool = True): """The sharable URL that describes the current channel """ # Only keep the primary/secondary channels, assume primary is first channelSet = apponly_pb2.ChannelSet() for c in self.channels: - if c.role != channel_pb2.Channel.Role.DISABLED: + if c.role == channel_pb2.Channel.Role.PRIMARY or (includeAll and c.role == channel_pb2.Channel.Role.SECONDARY): channelSet.settings.append(c.settings) bytes = channelSet.SerializeToString() s = base64.urlsafe_b64encode(bytes).decode('ascii') @@ -439,10 +447,10 @@ class MeshInterface: def showInfo(self): """Show human readable summary about this object""" - print(self.myInfo) - print("Nodes in mesh:") + print(f"My info: {stripnl(MessageToJson(self.myInfo))}") + print("\nNodes in mesh:") for n in self.nodes.values(): - print(stripnl(n)) + print(" " + stripnl(n)) def getNode(self, nodeId): """Return a node object which contains device settings and channel info""" diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 2b192e2..332da4f 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -318,14 +318,14 @@ def onConnected(interface): print(f"Deleting channel {channelIndex}") ch = getNode().deleteChannel(channelIndex) - if args.setchan or args.setch_longslow or args.setch_shortfast: + if args.ch_set or args.ch_longslow or args.ch_shortfast: closeNow = True ch = getNode().channels[channelIndex] enable = args.ch_enable # should we enable this channel? - if args.setch_longslow or args.setch_shortfast: + if args.ch_longslow or args.ch_shortfast: if channelIndex != 0: raise Exception("standard channel settings can only be applied to the PRIMARY channel") @@ -342,16 +342,16 @@ def onConnected(interface): ch.settings.CopyFrom(chs) # handle the simple channel set commands - if args.setch_longslow: + if args.ch_longslow: setSimpleChannel( channel_pb2.ChannelSettings.ModemConfig.Bw125Cr48Sf4096) - if args.setch_shortfast: + if args.ch_shortfast: setSimpleChannel( channel_pb2.ChannelSettings.ModemConfig.Bw500Cr45Sf128) # Handle the channel settings - for pref in (args.setchan or []): + for pref in (args.ch_set or []): if pref[0] == "psk": ch.settings.psk =fromPSK(pref[1]) else: @@ -367,11 +367,14 @@ def onConnected(interface): getNode().writeChannel(channelIndex) if args.info: + print("") if not args.dest: # If we aren't trying to talk to our local node, don't show it interface.showInfo() + print("") getNode().showInfo() closeNow = True # FIXME, for now we leave the link up while talking to remote nodes + print("") if args.nodes: closeNow = True @@ -380,9 +383,10 @@ def onConnected(interface): if args.qr: closeNow = True - print(f"Channel URL {interface.channelURL}") - url = pyqrcode.create(interface.channelURL) - print(url.terminal()) + url = interface.localNode.getURL(includeAll = False) + print(f"Primary channel URL {url}") + qr = pyqrcode.create(url) + print(qr.terminal()) if have_tunnel and args.tunnel: from . import tunnel @@ -434,15 +438,17 @@ def common(): if not args.seriallog: if args.info or args.nodes or args.set or args.seturl or args.set_owner or args.setlat or args.setlon or \ - args.setch_longslow or args.setch_shortfast or args.setchan or args.sendtext or \ + args.ch_longslow or args.ch_shortfast or args.ch_set or args.sendtext or \ args.qr or args.ch_add or args.ch_del or args.set_ham: args.seriallog = "none" # assume no debug output in this case else: args.seriallog = "stdout" # default to stdout - if args.router != None: + if args.deprecated != None: logging.error( - '--set-router has been deprecated. Use "--set is_router true" or "--set is_router false" instead') + 'This option has been deprecated, see help below for the correct replacement...') + parser.print_help(sys.stderr) + sys.exit(1) elif args.test: test.testAll() else: @@ -500,15 +506,6 @@ def initParser(): parser.add_argument( "--set", help="Set a preferences field", nargs=2, action='append') - parser.add_argument( - "--setchan", help="Set a channel parameter", nargs=2, action='append') - - parser.add_argument( - "--setch-longslow", help="Change to the standard long-range (but slow) channel", action='store_true') - - parser.add_argument( - "--setch-shortfast", help="Change to the standard fast (but short range) channel", action='store_true') - parser.add_argument( "--seturl", help="Set a channel URL", action="store") @@ -527,6 +524,15 @@ def initParser(): parser.add_argument( "--ch-disable", help="Disable the specified channel", action="store_false", dest="ch_enable") + parser.add_argument( + "--ch-set", help="Set a channel parameter", nargs=2, action='append') + + parser.add_argument( + "--ch-longslow", help="Change to the standard long-range (but slow) channel", action='store_true') + + parser.add_argument( + "--ch-shortfast", help="Change to the standard fast (but short range) channel", action='store_true') + parser.add_argument( "--set-owner", help="Set device owner name", action="store") @@ -582,10 +588,12 @@ def initParser(): parser.add_argument("--noproto", help="Don't start the API, just function as a dumb serial terminal.", action="store_true") - parser.add_argument('--set-router', dest='router', - action='store_true', help="Deprecated, use --set is_router true instead") - parser.add_argument('--unset-router', dest='router', - action='store_false', help="Deprecated, use --set is_router false instead") + parser.add_argument('--setchan', dest='deprecated', + action='store_true', help='Deprecated, use "--ch-set param value" instead') + parser.add_argument('--set-router', dest='deprecated', + action='store_true', help='Deprecated, use "--set is_router true" instead') + parser.add_argument('--unset-router', dest='deprecated', + action='store_false', help='Deprecated, use "--set is_router false" instead') if have_tunnel: parser.add_argument('--tunnel', @@ -593,7 +601,7 @@ def initParser(): parser.add_argument( "--subnet", dest='tunnel_net', help="Read from a GPIO mask", default=None) - parser.set_defaults(router=None) + parser.set_defaults(deprecated=None) parser.add_argument('--version', action='version', version=f"{pkg_resources.require('meshtastic')[0].version}") diff --git a/meshtastic/util.py b/meshtastic/util.py index 6375238..fe971b1 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -10,8 +10,9 @@ blacklistVids = dict.fromkeys([0x1366]) def stripnl(s): - """remove newlines from a string""" - return str(s).replace("\n", " ") + """remove newlines from a string (and remove extra whitespace)""" + s = str(s).replace("\n", " ") + return ' '.join(s.split()) def fixme(message):