Skip to content

Commit 7cc18e9

Browse files
committed
fix(waypoint): Missing methods
Add missing methods callbacks regarding waypoints.
1 parent 9284a84 commit 7cc18e9

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

examples/waypoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Program to create and delete waypoint
22
To run:
33
python3 examples/waypoint.py --port /dev/ttyUSB0 create 45 test the_desc_2 '2024-12-18T23:05:23' 48.74 7.35
4-
python examples/waypoint.py delete 45
4+
python3 examples/waypoint.py delete 45
55
"""
66

77
import argparse

meshtastic/mesh_interface.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,19 @@ def onResponseTelemetry(self, p: dict):
702702
"No response from node. At least firmware 2.1.22 is required on the destination node."
703703
)
704704

705+
def onResponseWaypoint(self, p: dict):
706+
"""on response for waypoint"""
707+
if p["decoded"]["portnum"] == "WAYPOINT_APP":
708+
self._acknowledgment.receivedWaypoint = True
709+
w = mesh_pb2.Waypoint()
710+
w.ParseFromString(p["decoded"]["payload"])
711+
print(f"Waypoint received: {w}")
712+
elif p["decoded"]["portnum"] == "ROUTING_APP":
713+
if p["decoded"]["routing"]["errorReason"] == "NO_RESPONSE":
714+
our_exit(
715+
"No response from node. At least firmware 2.1.22 is required on the destination node."
716+
)
717+
705718
def sendWaypoint(
706719
self,
707720
name,
@@ -726,6 +739,8 @@ def sendWaypoint(
726739
w.description = description
727740
w.expire = expire
728741
if id is None:
742+
# Generate a waypoint's id, NOT a packet ID.
743+
# same algorithm as https://github.com/meshtastic/js/blob/715e35d2374276a43ffa93c628e3710875d43907/src/meshDevice.ts#L791
729744
seed = secrets.randbits(32)
730745
w.id = math.floor(seed * math.pow(2, -32) * 1e9)
731746
logging.debug(f"w.id:{w.id}")
@@ -918,6 +933,12 @@ def waitForPosition(self):
918933
if not success:
919934
raise MeshInterface.MeshInterfaceError("Timed out waiting for position")
920935

936+
def waitForWaypoint(self):
937+
"""Wait for waypoint"""
938+
success = self._timeout.waitForWaypoint(self._acknowledgment)
939+
if not success:
940+
raise MeshInterface.MeshInterfaceError("Timed out waiting for waypoint")
941+
921942
def getMyNodeInfo(self) -> Optional[Dict]:
922943
"""Get info about my node."""
923944
if self.myInfo is None or self.nodesByNum is None:

meshtastic/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ def waitForPosition(self, acknowledgment) -> bool:
254254
time.sleep(self.sleepInterval)
255255
return False
256256

257+
def waitForWaypoint(self, acknowledgment) -> bool:
258+
"""Block until waypoint response is received. Returns True if waypoint response has been received."""
259+
self.reset()
260+
while time.time() < self.expireTime:
261+
if getattr(acknowledgment, "receivedWaypoint", None):
262+
acknowledgment.reset()
263+
return True
264+
time.sleep(self.sleepInterval)
265+
return False
266+
257267
class Acknowledgment:
258268
"A class that records which type of acknowledgment was just received, if any."
259269

@@ -265,6 +275,7 @@ def __init__(self) -> None:
265275
self.receivedTraceRoute = False
266276
self.receivedTelemetry = False
267277
self.receivedPosition = False
278+
self.receivedWaypoint = False
268279

269280
def reset(self) -> None:
270281
"""reset"""
@@ -274,6 +285,7 @@ def reset(self) -> None:
274285
self.receivedTraceRoute = False
275286
self.receivedTelemetry = False
276287
self.receivedPosition = False
288+
self.receivedWaypoint = False
277289

278290

279291
class DeferredExecution:

0 commit comments

Comments
 (0)