|
5 | 5 | import collections |
6 | 6 | import json |
7 | 7 | import logging |
| 8 | +import math |
8 | 9 | import random |
| 10 | +import secrets |
9 | 11 | import sys |
10 | 12 | import threading |
11 | 13 | import time |
@@ -700,6 +702,98 @@ def onResponseTelemetry(self, p: dict): |
700 | 702 | "No response from node. At least firmware 2.1.22 is required on the destination node." |
701 | 703 | ) |
702 | 704 |
|
| 705 | + def sendWaypoint( |
| 706 | + self, |
| 707 | + name, |
| 708 | + description, |
| 709 | + expire: int, |
| 710 | + id: Optional[int] = None, |
| 711 | + latitude: float = 0.0, |
| 712 | + longitude: float = 0.0, |
| 713 | + destinationId: Union[int, str] = BROADCAST_ADDR, |
| 714 | + wantAck: bool = True, |
| 715 | + wantResponse: bool = False, |
| 716 | + channelIndex: int = 0, |
| 717 | + ): |
| 718 | + """ |
| 719 | + Send a waypoint packet to some other node (normally a broadcast) |
| 720 | +
|
| 721 | + Returns the sent packet. The id field will be populated in this packet and |
| 722 | + can be used to track future message acks/naks. |
| 723 | + """ |
| 724 | + w = mesh_pb2.Waypoint() |
| 725 | + w.name = name |
| 726 | + w.description = description |
| 727 | + w.expire = expire |
| 728 | + if id is None: |
| 729 | + seed = secrets.randbits(32) |
| 730 | + w.id = math.floor(seed * math.pow(2, -32) * 1e9) |
| 731 | + logging.debug(f"w.id:{w.id}") |
| 732 | + else: |
| 733 | + w.id = id |
| 734 | + if latitude != 0.0: |
| 735 | + w.latitude_i = int(latitude * 1e7) |
| 736 | + logging.debug(f"w.latitude_i:{w.latitude_i}") |
| 737 | + if longitude != 0.0: |
| 738 | + w.longitude_i = int(longitude * 1e7) |
| 739 | + logging.debug(f"w.longitude_i:{w.longitude_i}") |
| 740 | + |
| 741 | + if wantResponse: |
| 742 | + onResponse = self.onResponseWaypoint |
| 743 | + else: |
| 744 | + onResponse = None |
| 745 | + |
| 746 | + d = self.sendData( |
| 747 | + w, |
| 748 | + destinationId, |
| 749 | + portNum=portnums_pb2.PortNum.WAYPOINT_APP, |
| 750 | + wantAck=wantAck, |
| 751 | + wantResponse=wantResponse, |
| 752 | + onResponse=onResponse, |
| 753 | + channelIndex=channelIndex, |
| 754 | + ) |
| 755 | + if wantResponse: |
| 756 | + self.waitForWaypoint() |
| 757 | + return d |
| 758 | + |
| 759 | + def deleteWaypoint( |
| 760 | + self, |
| 761 | + id: int, |
| 762 | + destinationId: Union[int, str] = BROADCAST_ADDR, |
| 763 | + wantAck: bool = True, |
| 764 | + wantResponse: bool = False, |
| 765 | + channelIndex: int = 0, |
| 766 | + ): |
| 767 | + """ |
| 768 | + Send a waypoint deletion packet to some other node (normally a broadcast) |
| 769 | +
|
| 770 | + NB: The id must be the waypoint's id and not the id of the packet creation. |
| 771 | + |
| 772 | + Returns the sent packet. The id field will be populated in this packet and |
| 773 | + can be used to track future message acks/naks. |
| 774 | + """ |
| 775 | + p = mesh_pb2.Waypoint() |
| 776 | + p.id = id |
| 777 | + p.expire = 0 |
| 778 | + |
| 779 | + if wantResponse: |
| 780 | + onResponse = self.onResponseWaypoint |
| 781 | + else: |
| 782 | + onResponse = None |
| 783 | + |
| 784 | + d = self.sendData( |
| 785 | + p, |
| 786 | + destinationId, |
| 787 | + portNum=portnums_pb2.PortNum.WAYPOINT_APP, |
| 788 | + wantAck=wantAck, |
| 789 | + wantResponse=wantResponse, |
| 790 | + onResponse=onResponse, |
| 791 | + channelIndex=channelIndex, |
| 792 | + ) |
| 793 | + if wantResponse: |
| 794 | + self.waitForWaypoint() |
| 795 | + return d |
| 796 | + |
703 | 797 | def _addResponseHandler( |
704 | 798 | self, |
705 | 799 | requestId: int, |
|
0 commit comments