From 40019a9712f7d87a0d58dca9ea4856a961293f57 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Sun, 22 Sep 2024 09:32:04 -0700 Subject: [PATCH] Add a --set-time command that set's the node time using a provided timestamp or the host system clock. --- meshtastic/__main__.py | 13 +++++++++++++ meshtastic/node.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 686d207..ccac5b2 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -285,6 +285,9 @@ def onConnected(interface): if not args.export_config: print("Connected to radio") + if args.set_time is not None: + interface.getNode(args.dest, False, **getNode_kwargs).setTime(args.set_time) + if args.remove_position: closeNow = True waitForAckNak = True @@ -1597,6 +1600,16 @@ def initParser(): action="store_true", ) + group.add_argument( + "--set-time", + help="Set the time to the provided unix epoch timestamp, or the system's current time if omitted or 0.", + action="store", + type=int, + nargs="?", + default=None, + const=0, + ) + group.add_argument( "--reply", help="Reply to received messages", action="store_true" ) diff --git a/meshtastic/node.py b/meshtastic/node.py index bef632e..91cc3a9 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -722,6 +722,21 @@ class Node: onResponse = self.onAckNak return self._sendAdmin(p, onResponse=onResponse) + def setTime(self, timeSec: int = 0): + """Tell the node to set its time to the provided timestamp, or the system's current time if not provided or 0.""" + self.ensureSessionKey() + if timeSec == 0: + timeSec = int(time.time()) + p = admin_pb2.AdminMessage() + p.set_time_only = timeSec + logging.info(f"Setting node time to {timeSec}") + + if self == self.iface.localNode: + onResponse = None + else: + onResponse = self.onAckNak + return self._sendAdmin(p, onResponse=onResponse) + def _fixupChannels(self): """Fixup indexes and add disabled channels as needed"""