Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --set-time command that set's the node time using a provided timestamp or the host system clock #676

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions meshtastic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
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)

Check warning on line 289 in meshtastic/__main__.py

View check run for this annotation

Codecov / codecov/patch

meshtastic/__main__.py#L289

Added line #L289 was not covered by tests

if args.remove_position:
closeNow = True
waitForAckNak = True
Expand Down Expand Up @@ -1597,6 +1600,16 @@
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"
)
Expand Down
15 changes: 15 additions & 0 deletions meshtastic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,21 @@
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}")

Check warning on line 732 in meshtastic/node.py

View check run for this annotation

Codecov / codecov/patch

meshtastic/node.py#L727-L732

Added lines #L727 - L732 were not covered by tests

if self == self.iface.localNode:
onResponse = None

Check warning on line 735 in meshtastic/node.py

View check run for this annotation

Codecov / codecov/patch

meshtastic/node.py#L734-L735

Added lines #L734 - L735 were not covered by tests
else:
onResponse = self.onAckNak
return self._sendAdmin(p, onResponse=onResponse)

Check warning on line 738 in meshtastic/node.py

View check run for this annotation

Codecov / codecov/patch

meshtastic/node.py#L737-L738

Added lines #L737 - L738 were not covered by tests

def _fixupChannels(self):
"""Fixup indexes and add disabled channels as needed"""

Expand Down
Loading