From dbbf14c4bf8a39648e50100cf9bfbce9842dedbe Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 3 May 2020 20:14:11 -0700 Subject: [PATCH] add commandline option for setting radio configs --- TODO.md | 2 +- meshtastic/__init__.py | 9 +++++++++ meshtastic/__main__.py | 34 ++++++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index a9f864b..329e3b2 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,6 @@ ## Before beta -- make command line options for displaying/changing config - update nodedb as nodes change - radioConfig - getter/setter syntax: https://www.python-course.eu/python3_properties.php - let user change radio params via commandline options @@ -35,3 +34,4 @@ - DONE keep everything in dicts - DONE have device send a special packet at boot so the serial client can detect if it rebooted - DONE add fromId and toId to received messages dictionaries +- make command line options for displaying/changing config diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 394e08f..b14ba54 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -121,6 +121,15 @@ class MeshInterface: toRadio.packet.CopyFrom(meshPacket) self._sendToRadio(toRadio) + def writeConfig(self): + """Write the current (edited) radioConfig to the device""" + if self.radioConfig == None: + raise Exception("No RadioConfig has been read") + + t = mesh_pb2.ToRadio() + t.set_radio.CopyFrom(self.radioConfig) + self._sendToRadio(t) + def _disconnected(self): """Called by subclasses to tell clients this interface has disconnected""" self.isConnected = False diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 3be7798..511b9a0 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -20,14 +20,27 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): """Callback invoked when we connect/disconnect from a radio""" print(f"Connection changed: {topic.getName()}") global args - if topic.getName() == "meshtastic.connection.established" and args.info: - print(interface.myInfo) - print(interface.radioConfig) - interface.close() - print("Nodes in mesh:") - for n in interface.nodes.values(): - asDict = google.protobuf.json_format.MessageToJson(n) - print(asDict) + if topic.getName() == "meshtastic.connection.established": + try: + if args.setpref: + name = args.setpref[0] + val = int(args.setpref[1]) + setattr(interface.radioConfig.preferences, name, val) + print("Writing modified preferences to device...") + interface.writeConfig() + + if args.info: + print(interface.myInfo) + print(interface.radioConfig) + print("Nodes in mesh:") + for n in interface.nodes.values(): + asDict = google.protobuf.json_format.MessageToJson(n) + print(asDict) + except Exception as ex: + print(ex) + + if args.info or args.setpref: + interface.close() # after running command then exit def onNode(node): @@ -59,6 +72,8 @@ def main(): parser.add_argument("--info", help="Read and display the radio config information", action="store_true") + parser.add_argument("--setpref", help="Set a preferences field", nargs=2) + parser.add_argument("--debug", help="Show API library debug log messages", action="store_true") @@ -69,6 +84,9 @@ def main(): args = parser.parse_args() logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) + if args.info or args.setpref: + args.seriallog = "none" # assume no debug output in this case + if args.test: test.testAll() else: