mirror of
https://github.com/meshcore-dev/meshcore-cli.git
synced 2026-04-20 22:13:48 +00:00
Implement wait_msg and wait_ack
This commit is contained in:
parent
978f3416e7
commit
8bb88af542
1 changed files with 25 additions and 1 deletions
26
mccli.py
26
mccli.py
|
|
@ -44,6 +44,8 @@ class MeshCore:
|
||||||
self.time = 0
|
self.time = 0
|
||||||
self.result = asyncio.Future()
|
self.result = asyncio.Future()
|
||||||
self.contact_nb = 0
|
self.contact_nb = 0
|
||||||
|
self.rx_sem = asyncio.Semaphore(0)
|
||||||
|
self.ack_ev = asyncio.Event()
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -158,8 +160,10 @@ class MeshCore:
|
||||||
case 0x81:
|
case 0x81:
|
||||||
print("Code path update")
|
print("Code path update")
|
||||||
case 0x82:
|
case 0x82:
|
||||||
|
self.ack_ev.set()
|
||||||
print("Received ACK")
|
print("Received ACK")
|
||||||
case 0x83:
|
case 0x83:
|
||||||
|
self.rx_sem.release()
|
||||||
print("Msgs are waiting")
|
print("Msgs are waiting")
|
||||||
# unhandled
|
# unhandled
|
||||||
case _:
|
case _:
|
||||||
|
|
@ -213,11 +217,23 @@ class MeshCore:
|
||||||
""" Send a message to a node """
|
""" Send a message to a node """
|
||||||
timestamp = (await self.get_time()).to_bytes(4, 'little')
|
timestamp = (await self.get_time()).to_bytes(4, 'little')
|
||||||
data = b"\x02\x00\x00" + timestamp + dst + msg.encode("ascii")
|
data = b"\x02\x00\x00" + timestamp + dst + msg.encode("ascii")
|
||||||
|
self.ack_ev.clear()
|
||||||
return await self.send(data)
|
return await self.send(data)
|
||||||
|
|
||||||
async def get_msg(self):
|
async def get_msg(self):
|
||||||
""" Get message from the node (stored in queue) """
|
""" Get message from the node (stored in queue) """
|
||||||
return await self.send(b"\x0A", 1)
|
res = await self.send(b"\x0A", 1)
|
||||||
|
if res is False :
|
||||||
|
self.rx_sem=asyncio.Semaphore(0) # reset semaphore as there are no msgs in queue
|
||||||
|
return res
|
||||||
|
|
||||||
|
async def wait_msg(self):
|
||||||
|
""" Wait for a message """
|
||||||
|
await self.rx_sem.acquire()
|
||||||
|
|
||||||
|
async def wait_ack(self):
|
||||||
|
""" Wait ack """
|
||||||
|
await self.ack_ev.wait()
|
||||||
|
|
||||||
async def next_cmd(mc, cmds):
|
async def next_cmd(mc, cmds):
|
||||||
""" process next command """
|
""" process next command """
|
||||||
|
|
@ -250,6 +266,12 @@ async def next_cmd(mc, cmds):
|
||||||
while res:
|
while res:
|
||||||
res = await mc.get_msg()
|
res = await mc.get_msg()
|
||||||
print (res)
|
print (res)
|
||||||
|
case "wait_msg" :
|
||||||
|
await mc.wait_msg()
|
||||||
|
res = await mc.get_msg()
|
||||||
|
print (res)
|
||||||
|
case "wait_ack" :
|
||||||
|
print (await mc.wait_ack())
|
||||||
case "infos" :
|
case "infos" :
|
||||||
print(mc.self_info)
|
print(mc.self_info)
|
||||||
case "advert" :
|
case "advert" :
|
||||||
|
|
@ -279,8 +301,10 @@ def usage () :
|
||||||
infos : print informations about the node
|
infos : print informations about the node
|
||||||
send <key> <msg> : sends msg to the node with pubkey starting by key
|
send <key> <msg> : sends msg to the node with pubkey starting by key
|
||||||
sendto <name> <msg> : sends msg to the node with given name
|
sendto <name> <msg> : sends msg to the node with given name
|
||||||
|
wait_ack : wait an ack for last sent msg
|
||||||
recv : reads next msg
|
recv : reads next msg
|
||||||
sync_msgs : gets all unread msgs from the node
|
sync_msgs : gets all unread msgs from the node
|
||||||
|
wait_msg : wait for a message
|
||||||
advert : sends advert
|
advert : sends advert
|
||||||
contacts : gets contact list
|
contacts : gets contact list
|
||||||
sync_time : sync time with system
|
sync_time : sync time with system
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue