|
23 | 23 | from meshcore import MeshCore, EventType, logger |
24 | 24 |
|
25 | 25 | # Version |
26 | | -VERSION = "v1.1.34" |
| 26 | +VERSION = "v1.1.35" |
27 | 27 |
|
28 | 28 | # default ble address is stored in a config file |
29 | 29 | MCCLI_CONFIG_DIR = str(Path.home()) + "/.config/meshcore/" |
@@ -563,7 +563,8 @@ async def interactive_loop(mc, to=None) : |
563 | 563 | contact = to |
564 | 564 | prev_contact = None |
565 | 565 |
|
566 | | - await mc.commands.get_contacts(anim=True) |
| 566 | +# await get_contacts(mc, anim=True) |
| 567 | + await get_contacts(mc, anim=True) |
567 | 568 | await get_channels(mc, anim=True) |
568 | 569 | await subscribe_to_msgs(mc, above=True) |
569 | 570 |
|
@@ -978,6 +979,60 @@ async def get_channel_by_name (mc, name): |
978 | 979 |
|
979 | 980 | return None |
980 | 981 |
|
| 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 | + |
981 | 1036 | async def get_channels (mc, anim=False) : |
982 | 1037 | if hasattr(mc, 'channels') : |
983 | 1038 | return mc.channels |
@@ -1021,7 +1076,7 @@ async def next_cmd(mc, cmds, json_output=False): |
1021 | 1076 | elif json_output : |
1022 | 1077 | print(json.dumps(res.payload, indent=4)) |
1023 | 1078 | else : |
1024 | | - print("Devince info :") |
| 1079 | + print("Device info :") |
1025 | 1080 | if res.payload["fw ver"] >= 3: |
1026 | 1081 | print(f" Model: {res.payload['model']}") |
1027 | 1082 | print(f" Version: {res.payload['ver']}") |
|
0 commit comments