Change contract for commands to return full event

This commit is contained in:
Alex Wolden 2025-04-14 11:10:59 -07:00
parent 39ea3cb3f3
commit 6fbf15885d
17 changed files with 231 additions and 104 deletions

View file

@ -69,8 +69,12 @@ async def main () :
else :
if line.startswith("send") :
line = line[5:]
ret = await mc.commands.send_msg(contact , line)
exp_ack = ret["expected_ack"].hex()
result = await mc.commands.send_msg(contact, line)
if result.type == EventType.ERROR:
print(f"⚠️ Failed to send message: {result.payload}")
continue
exp_ack = result.payload["expected_ack"].hex()
print(" Sent ... ", end="", flush=True)
res = await mc.wait_for_event(EventType.ACK, attribute_filters={"code": exp_ack}, timeout=5)
if res is None :

View file

@ -45,7 +45,11 @@ async def main():
print("Connected to MeshCore device")
# Get contacts
contacts = await meshcore.commands.get_contacts()
result = await meshcore.commands.get_contacts()
if result.type == EventType.ERROR:
print(f"Error fetching contacts: {result.payload}")
return
contacts = result.payload
if contacts:
print(f"\nFound {len(contacts)} contacts:")
for name, contact in contacts.items():

View file

@ -17,7 +17,9 @@ async def handle_message(event):
data = event.payload
contact = mc.get_contact_by_key_prefix(data['pubkey_prefix'])
if contact is None:
print(f"Unknown contact with pubkey prefix: {data['pubkey_prefix']}")
return
print(f"{contact['adv_name']}: {data['text']}")
async def main () :
@ -54,7 +56,7 @@ async def main () :
if line.startswith("to ") :
dest = line[3:]
nc = mc.get_contact_by_name(dest)
if mc is None:
if nc is None:
print(f"Contact '{DEST}' not found in contacts.")
return
else :
@ -72,8 +74,12 @@ async def main () :
else :
if line.startswith("send") :
line = line[5:]
ret = await mc.commands.send_msg(contact , line)
exp_ack = ret["expected_ack"].hex()
result = await mc.commands.send_msg(contact, line)
if result.type == EventType.ERROR:
print(f"⚠️ Failed to send message: {result.payload}")
continue
exp_ack = result.payload["expected_ack"].hex()
print(" Sent ... ", end="", flush=True)
res = await mc.wait_for_event(EventType.ACK, attribute_filters={"code": exp_ack}, timeout=5)
if res is None :
@ -82,7 +88,7 @@ async def main () :
print ("Ack")
except KeyboardInterrupt:
meshcore.stop()
mc.stop()
print("\nExiting...")
except asyncio.CancelledError:
# Handle task cancellation from KeyboardInterrupt in asyncio.run()

View file

@ -5,6 +5,7 @@ import json
from meshcore import MeshCore
from meshcore import SerialConnection
from meshcore import EventType
PORT = "/dev/ttyUSB0"
BAUDRATE = 115200
@ -16,6 +17,10 @@ async def main () :
mc = MeshCore(con)
await mc.connect()
print(json.dumps(await mc.commands.get_contacts(),indent=4))
result = await mc.commands.get_contacts()
if result.type == EventType.ERROR:
print(f"Error getting contacts: {result.payload}")
else:
print(json.dumps(result.payload, indent=4))
asyncio.run(main())

View file

@ -37,13 +37,17 @@ async def main():
# Send the message and get the MSG_SENT event
print(f"Sending message: '{args.message}'")
send_result = await mc.commands.send_msg(
result = await mc.commands.send_msg(
contact,
args.message
)
if result.type == EventType.ERROR:
print(f"⚠️ Failed to send message: {result.payload}")
return
# Extract the expected ACK code
expected_ack = send_result["expected_ack"].hex()
expected_ack = result.payload["expected_ack"].hex()
print(f"Message sent, waiting for ACK with code: {expected_ack}")
# Wait for the specific ACK that matches our message

View file

@ -32,7 +32,7 @@ async def main():
print(f"Logging in to repeater '{args.repeater}'...")
login_event = await mc.commands.send_login(repeater, args.password)
if login_event and login_event.get("success") != False:
if login_event.type != EventType.ERROR:
print("Login successful")
# Send status request

View file

@ -34,10 +34,10 @@ async def main():
tag = random.randint(1, 0xFFFFFFFF)
result = await mc.commands.send_trace(path=args.path, tag=tag)
# Check if the result has a success indicator
if result.get("success") == False:
print(f"Failed to send trace packet: {result.get('reason', 'unknown error')}")
elif result:
# Check if the result is an error
if result.type == EventType.ERROR:
print(f"Failed to send trace packet: {result.payload.get('reason', 'unknown error')}")
elif result.type == EventType.MSG_SENT:
print(f"Trace packet sent successfully with tag={tag}")
print("Waiting for trace response matching our tag...")

View file

@ -17,7 +17,9 @@ async def handle_message(event):
data = event.payload
contact = mc.get_contact_by_key_prefix(data['pubkey_prefix'])
if contact is None:
print(f"Unknown contact with pubkey prefix: {data['pubkey_prefix']}")
return
print(f"{contact['adv_name']}: {data['text']}")
async def main () :
@ -54,7 +56,7 @@ async def main () :
if line.startswith("to ") :
dest = line[3:]
nc = mc.get_contact_by_name(dest)
if mc is None:
if nc is None:
print(f"Contact '{DEST}' not found in contacts.")
return
else :
@ -72,8 +74,12 @@ async def main () :
else :
if line.startswith("send") :
line = line[5:]
ret = await mc.commands.send_msg(contact , line)
exp_ack = ret["expected_ack"].hex()
result = await mc.commands.send_msg(contact, line)
if result.type == EventType.ERROR:
print(f"⚠️ Failed to send message: {result.payload}")
continue
exp_ack = result.payload["expected_ack"].hex()
print(" Sent ... ", end="", flush=True)
res = await mc.wait_for_event(EventType.ACK, attribute_filters={"code": exp_ack}, timeout=5)
if res is None :
@ -82,7 +88,7 @@ async def main () :
print ("Ack")
except KeyboardInterrupt:
meshcore.stop()
mc.stop()
print("\nExiting...")
except asyncio.CancelledError:
# Handle task cancellation from KeyboardInterrupt in asyncio.run()

View file

@ -4,6 +4,7 @@ import asyncio
import json
from meshcore import TCPConnection
from meshcore import MeshCore
from meshcore import EventType
HOSTNAME = "mchome"
PORT = 5000
@ -14,5 +15,9 @@ async def main () :
mc = MeshCore(con)
await mc.connect()
print(json.dumps(await mc.commands.get_contacts(),indent=4))
result = await mc.commands.get_contacts()
if result.type == EventType.ERROR:
print(f"Error getting contacts: {result.payload}")
else:
print(json.dumps(result.payload, indent=4))
asyncio.run(main())

View file

@ -22,9 +22,14 @@ async def main () :
if contact is None:
print(f"Contact '{DEST}' not found in contacts.")
return
ret = await mc.commands.send_msg(contact ,MSG)
print (ret)
exp_ack = ret["expected_ack"].hex()
result = await mc.commands.send_msg(contact, MSG)
print(result)
if result.type == EventType.ERROR:
print(f"⚠️ Failed to send message: {result.payload}")
return
exp_ack = result.payload["expected_ack"].hex()
print(await mc.wait_for_event(EventType.ACK, attribute_filters={"code": exp_ack}, timeout=5))
asyncio.run(main())

View file

@ -4,6 +4,7 @@ import asyncio
import json
from meshcore import TCPConnection
from meshcore import MeshCore
from meshcore import EventType
HOSTNAME = "mchome"
PORT = 5000
@ -19,9 +20,12 @@ async def main () :
res = True
while res:
result = await mc.commands.get_msg()
if result.get("success") == False:
if result.type == EventType.NO_MORE_MSGS:
res = False
print("No more messages")
print (result)
elif result.type == EventType.ERROR:
res = False
print(f"Error retrieving messages: {result.payload}")
print(result)
asyncio.run(main())