Skip to content

Commit 3f7fe47

Browse files
committed
manage anims in mccli
1 parent 302e437 commit 3f7fe47

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "meshcore-cli"
7-
version = "1.1.34"
7+
version = "1.1.35"
88
authors = [
99
{ name="Florent de Lamotte", email="[email protected]" },
1010
]
@@ -17,7 +17,7 @@ classifiers = [
1717
]
1818
license = "MIT"
1919
license-files = ["LICEN[CS]E*"]
20-
dependencies = [ "meshcore >= 2.1.15", "prompt_toolkit >= 3.0.50", "requests >= 2.28.0" ]
20+
dependencies = [ "meshcore >= 2.1.16", "prompt_toolkit >= 3.0.50", "requests >= 2.28.0" ]
2121

2222
[project.urls]
2323
Homepage = "https://github.com/fdlamotte/meshcore-cli"

src/meshcore_cli/meshcore_cli.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from meshcore import MeshCore, EventType, logger
2424

2525
# Version
26-
VERSION = "v1.1.34"
26+
VERSION = "v1.1.35"
2727

2828
# default ble address is stored in a config file
2929
MCCLI_CONFIG_DIR = str(Path.home()) + "/.config/meshcore/"
@@ -563,7 +563,8 @@ async def interactive_loop(mc, to=None) :
563563
contact = to
564564
prev_contact = None
565565

566-
await mc.commands.get_contacts(anim=True)
566+
# await get_contacts(mc, anim=True)
567+
await get_contacts(mc, anim=True)
567568
await get_channels(mc, anim=True)
568569
await subscribe_to_msgs(mc, above=True)
569570

@@ -978,6 +979,60 @@ async def get_channel_by_name (mc, name):
978979

979980
return None
980981

982+
async def get_contacts (mc, anim=False, lastomod=0, timeout=5) :
983+
if anim:
984+
print("Fetching contacts ", end="", flush=True)
985+
986+
await mc.commands.get_contacts_async()
987+
988+
futures = []
989+
contact_nb = 0
990+
for event_type in [EventType.ERROR, EventType.NEXT_CONTACT, EventType.CONTACTS] :
991+
future = asyncio.create_task(
992+
mc.wait_for_event(event_type, {}, timeout=timeout)
993+
)
994+
futures.append(future)
995+
996+
while True:
997+
# Wait for the first event to complete or all to timeout
998+
done, pending = await asyncio.wait(
999+
futures, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
1000+
)
1001+
1002+
# Check if any future completed successfully
1003+
if len(done) == 0:
1004+
logger.debug("Timeout while getting contacts")
1005+
for future in pending: # cancel all futures
1006+
future.cancel()
1007+
return None
1008+
1009+
for future in done:
1010+
event = await future
1011+
1012+
if event:
1013+
if event.type == EventType.NEXT_CONTACT:
1014+
if anim:
1015+
contact_nb = contact_nb+1
1016+
print(".", end="", flush=True)
1017+
else: # Done or Error ... cancel pending and return
1018+
if anim:
1019+
if event.type == EventType.CONTACTS:
1020+
print ((len(event.payload)-contact_nb)*"." + " Done")
1021+
else :
1022+
print(" Error")
1023+
for future in pending:
1024+
future.cancel()
1025+
return event
1026+
1027+
futures = []
1028+
for future in pending: # put back pending
1029+
futures.append(future)
1030+
1031+
future = asyncio.create_task( # and recreate NEXT_CONTACT
1032+
mc.wait_for_event(EventType.NEXT_CONTACT, {}, timeout)
1033+
)
1034+
futures.append(future)
1035+
9811036
async def get_channels (mc, anim=False) :
9821037
if hasattr(mc, 'channels') :
9831038
return mc.channels
@@ -1021,7 +1076,7 @@ async def next_cmd(mc, cmds, json_output=False):
10211076
elif json_output :
10221077
print(json.dumps(res.payload, indent=4))
10231078
else :
1024-
print("Devince info :")
1079+
print("Device info :")
10251080
if res.payload["fw ver"] >= 3:
10261081
print(f" Model: {res.payload['model']}")
10271082
print(f" Version: {res.payload['ver']}")

0 commit comments

Comments
 (0)