From f61ccf2b57aedec0e0535f469a64a38257d93165 Mon Sep 17 00:00:00 2001 From: Florent de Lamotte Date: Tue, 4 Mar 2025 21:54:07 +0100 Subject: [PATCH] wait_login and wait_status --- mccli.py | 79 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/mccli.py b/mccli.py index 0b423f0..60a0bfa 100755 --- a/mccli.py +++ b/mccli.py @@ -30,8 +30,8 @@ MCCLI_ADDRESS = MCCLI_CONFIG_DIR + "default_address" # if None or "" then a scan is performed ADDRESS = "" -def printerr (str) : - sys.stderr.write(str) +def printerr (s) : + sys.stderr.write(str(s)) sys.stderr.write("\n") sys.stderr.flush() @@ -248,6 +248,7 @@ class MeshCore: self.rx_sem = asyncio.Semaphore(0) self.ack_ev = asyncio.Event() self.login_resp = asyncio.Future() + self.status_resp = asyncio.Future() self.cx = cx cx.set_mc(self) @@ -375,7 +376,8 @@ class MeshCore: res["last_snr"] = int.from_bytes(data[50:52], byteorder='little', signed=True) / 4 res["direct_dups"] = int.from_bytes(data[52:54], byteorder='little') res["flood_dups"] = int.from_bytes(data[54:56], byteorder='little') - print(res) + self.status_resp.set_result(res) + #printerr(res) # unhandled case _: printerr (f"Unhandled data received {data}") @@ -478,9 +480,17 @@ class MeshCore: return False async def send_statusreq(self, dst): + self.status_resp = asyncio.Future() data = b"\x1b" + dst return await self.send(data) + async def wait_status(self, timeout = 5): + try : + return await asyncio.wait_for(self.status_resp, timeout) + except TimeoutError : + printerr ("Timeout...") + return False + async def send_cmd(self, dst, cmd): """ Send a cmd to a node """ timestamp = (await self.get_time()).to_bytes(4, 'little') @@ -551,10 +561,14 @@ async def next_cmd(mc, cmds): await mc.ensure_contacts() print(await mc.send_login(bytes.fromhex(mc.contacts[cmds[1]]["public_key"]), cmds[2])) + case "wait_login": + print(await mc.wait_login()) case "req_status" : argnum = 1 await mc.ensure_contacts() print(await mc.send_statusreq(bytes.fromhex(mc.contacts[cmds[1]]["public_key"]))) + case "wait_status" : + print(await mc.wait_status()) case "contacts" : print(json.dumps(await mc.get_contacts(),indent=4)) case "change_path": @@ -588,8 +602,6 @@ async def next_cmd(mc, cmds): print (res) case "wait_ack" : await mc.wait_ack() - case "wait_login": - print(await mc.wait_login()) case "infos" : print(json.dumps(mc.self_info,indent=4)) case "advert" : @@ -620,34 +632,35 @@ def usage () : -b : specify baudrate Available Commands (can be chained) : - infos : print informations about the node - reboot - send : sends msg to the node with pubkey starting by key - sendto : sends msg to the node with given name - msg : same as sendto - wait_ack : wait an ack for last sent msg - recv : reads next msg - sync_msgs : gets all unread msgs from the node - wait_msg : wait for a message - advert : sends advert - contacts : gets contact list - share_contact - remove_contact - reset_path - change_path - sync_time : sync time with system - set_time : sets time to given epoch - get_time : gets current time - set_name : sets node name - get_bat : gets battery level - login : log into a node (repeater) with given pwd - wait_login : wait for login (timeouts after 5sec) - cmd : sends a command to a repeater - req_status : requests status from a node - sleep : sleeps for a given amount of secs""") - -async def main(argv): - """ Do the job """ + infos : print informations about the node + reboot : reboots node + send : sends msg to the node with pubkey starting by key + sendto : sends msg to the node with given name + msg : same as sendto + wait_ack : wait an ack for last sent msg + recv : reads next msg + sync_msgs : gets all unread msgs from the node + wait_msg : wait for a message and read it + advert : sends advert + contacts : gets contact list + share_contact : share a contact with others + remove_contact : removes a contact from this node + reset_path : resets path to a contact to flood + change_path : change the path to a contact + get_time : gets current time + set_time : sets time to given epoch + sync_time : sync time with system + set_name : sets node name + get_bat : gets battery level + login : log into a node (repeater) with given pwd + wait_login : wait for login (timeouts after 5sec) + cmd : sends a command to a repeater (no ack) + req_status : requests status from a node + wait_status : wait and print reply + sleep : sleeps for a given amount of secs""") + +async def main(argv): + """ Do the job """ address = ADDRESS port = 5000 hostname = None