mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
Add event filtering to support ACK tracking
This commit is contained in:
parent
478bcd92c1
commit
6dc87bafbb
9 changed files with 325 additions and 80 deletions
|
|
@ -1,23 +1,67 @@
|
|||
#!/usr/bin/python
|
||||
"""
|
||||
Example of sending a message and waiting for its specific acknowledgment
|
||||
using event attribute filtering.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from meshcore import MeshCore
|
||||
import argparse
|
||||
from meshcore import MeshCore, EventType
|
||||
|
||||
PORT = "/dev/tty.usbserial-583A0069501"
|
||||
BAUDRATE = 115200
|
||||
DEST = "🦄"
|
||||
MSG = "hello from serial"
|
||||
async def main():
|
||||
# Parse command line arguments
|
||||
parser = argparse.ArgumentParser(description="Send a message and wait for ACK")
|
||||
parser.add_argument("-p", "--port", required=True, help="Serial port")
|
||||
parser.add_argument("-b", "--baudrate", type=int, default=115200, help="Baud rate")
|
||||
parser.add_argument("-d", "--dest", default="🦄", help="Destination contact name")
|
||||
parser.add_argument("-m", "--message", default="hello from serial", help="Message to send")
|
||||
parser.add_argument("--timeout", type=float, default=30.0, help="ACK timeout in seconds")
|
||||
args = parser.parse_args()
|
||||
|
||||
async def main () :
|
||||
mc = await MeshCore.create_serial(PORT, BAUDRATE)
|
||||
# Connect to the device
|
||||
mc = await MeshCore.create_serial(args.port, args.baudrate, debug=True)
|
||||
|
||||
await mc.ensure_contacts()
|
||||
contact = mc.get_contact_by_name(DEST)
|
||||
if not contact:
|
||||
print(f"Contact {DEST} not found")
|
||||
return
|
||||
await mc.commands.send_msg(bytes.fromhex(contact["public_key"])[0:6], MSG)
|
||||
print ("Message sent ... awaiting")
|
||||
try:
|
||||
# Make sure we have contacts loaded
|
||||
await mc.ensure_contacts()
|
||||
|
||||
# Find the contact by name
|
||||
contact = mc.get_contact_by_name(args.dest)
|
||||
if not contact:
|
||||
print(f"Contact '{args.dest}' not found. Available contacts:")
|
||||
for name, c in mc.contacts.items():
|
||||
print(f"- {c.get('adv_name', 'Unknown')}")
|
||||
return
|
||||
|
||||
print(f"Found contact: {contact.get('adv_name')} ({contact['public_key'][:12]}...)")
|
||||
|
||||
# Send the message and get the MSG_SENT event
|
||||
print(f"Sending message: '{args.message}'")
|
||||
send_result = await mc.commands.send_msg(
|
||||
bytes.fromhex(contact["public_key"])[0:6],
|
||||
args.message
|
||||
)
|
||||
|
||||
# Extract the expected ACK code
|
||||
expected_ack = send_result["expected_ack"].hex()
|
||||
print(f"Message sent, waiting for ACK with code: {expected_ack}")
|
||||
|
||||
# Wait for the specific ACK that matches our message
|
||||
ack_event = await mc.wait_for_event(
|
||||
EventType.ACK,
|
||||
attribute_filters={"code": expected_ack},
|
||||
timeout=args.timeout
|
||||
)
|
||||
|
||||
if ack_event:
|
||||
print(f"✅ Message confirmed delivered! (ACK received)")
|
||||
else:
|
||||
print(f"⚠️ Timed out waiting for ACK after {args.timeout} seconds")
|
||||
|
||||
finally:
|
||||
# Always disconnect
|
||||
await mc.disconnect()
|
||||
|
||||
asyncio.run(main())
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue