Skip to content

Commit 83b119f

Browse files
committed
display path updates and adverts
1 parent 75c39f2 commit 83b119f

File tree

1 file changed

+85
-14
lines changed

1 file changed

+85
-14
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,54 @@ async def process_event_message(mc, ev, json_output, end="\n", above=False):
176176
process_event_message.color=True
177177
process_event_message.last_node=None
178178

179+
async def handle_advert(event):
180+
if not handle_advert.print_adverts:
181+
return
182+
183+
if handle_advert.json_output:
184+
msg = json.dumps({"event": "advert", "public_key" : event.payload["public_key"]})
185+
else:
186+
key = event.payload["public_key"]
187+
contact = handle_advert.mc.get_contact_by_key_prefix(key)
188+
name = "<Unknown Contact>"
189+
190+
if not contact is None :
191+
name = contact["adv_name"]
192+
193+
msg = f"Got advert from {name} [{key}]"
194+
195+
if handle_message.above:
196+
print_above(msg)
197+
else :
198+
print(msg)
199+
handle_advert.print_adverts=False
200+
handle_advert.mc=None
201+
handle_advert.json_output=False
202+
203+
async def handle_path_update(event):
204+
if not handle_path_update.print_path_updates:
205+
return
206+
207+
if handle_path_update.json_output:
208+
msg = json.dumps({"event": "path_update", "public_key" : event.payload["public_key"]})
209+
else:
210+
key = event.payload["public_key"]
211+
contact = handle_path_update.mc.get_contact_by_key_prefix(key)
212+
name = "<Unknown Contact>"
213+
214+
if not contact is None :
215+
name = contact["adv_name"]
216+
217+
msg = f"Got path update for {name} [{key}]"
218+
219+
if handle_message.above:
220+
print_above(msg)
221+
else :
222+
print(msg)
223+
handle_path_update.print_path_updates=False
224+
handle_path_update.mc=None
225+
handle_path_update.json_output=False
226+
179227
async def handle_message(event):
180228
""" Process incoming message events """
181229
await process_event_message(handle_message.mc, event,
@@ -269,6 +317,8 @@ def make_completion_dict(contacts, to=None):
269317
"json_msgs" : {"on":None, "off": None},
270318
"color" : {"on":None, "off":None},
271319
"print_name" : {"on":None, "off":None},
320+
"print_adverts" : {"on":None, "off":None},
321+
"print_path_updates" : {"on":None,"off":None},
272322
"classic_prompt" : {"on" : None, "off":None},
273323
"manual_add_contacts" : {"on" : None, "off":None},
274324
"telemetry_mode_base" : {"always" : None, "device":None, "never":None},
@@ -288,6 +338,8 @@ def make_completion_dict(contacts, to=None):
288338
"json_msgs":None,
289339
"color":None,
290340
"print_name":None,
341+
"print_adverts":None,
342+
"print_path_updates":None,
291343
"classic_prompt":None,
292344
"manual_add_contacts":None,
293345
"telemetry_mode_base":None,
@@ -748,6 +800,14 @@ async def next_cmd(mc, cmds, json_output=False):
748800
process_event_message.print_snr = (cmds[2] == "on")
749801
if json_output :
750802
print(json.dumps({"cmd" : cmds[1], "param" : cmds[2]}))
803+
case "print_adverts" :
804+
handle_advert.print_adverts = (cmds[2] == "on")
805+
if json_output :
806+
print(json.dumps({"cmd" : cmds[1], "param" : cmds[2]}))
807+
case "print_path_updates" :
808+
handle_path_update.print_path_updates = (cmds[2] == "on")
809+
if json_output :
810+
print(json.dumps({"cmd" : cmds[1], "param" : cmds[2]}))
751811
case "json_msgs" :
752812
handle_message.json_output = (cmds[2] == "on")
753813
if json_output :
@@ -1260,7 +1320,7 @@ async def next_cmd(mc, cmds, json_output=False):
12601320

12611321
case "path":
12621322
argnum = 1
1263-
res = await mc.ensure_contacts()
1323+
res = await mc.ensure_contacts(follow=True)
12641324
contact = mc.get_contact_by_name(cmds[1])
12651325
if contact is None:
12661326
if json_output :
@@ -1284,7 +1344,7 @@ async def next_cmd(mc, cmds, json_output=False):
12841344

12851345
case "contact_info" | "ci":
12861346
argnum = 1
1287-
res = await mc.ensure_contacts()
1347+
res = await mc.ensure_contacts(follow=True)
12881348
contact = mc.get_contact_by_name(cmds[1])
12891349
if contact is None:
12901350
if json_output :
@@ -1310,7 +1370,6 @@ async def next_cmd(mc, cmds, json_output=False):
13101370
print(f"Error setting path: {res}")
13111371
elif json_output :
13121372
print(json.dumps(res.payload, indent=4))
1313-
await mc.commands.get_contacts()
13141373

13151374
case "change_flags" | "cf":
13161375
argnum = 2
@@ -1328,7 +1387,6 @@ async def next_cmd(mc, cmds, json_output=False):
13281387
print(f"Error setting path: {res}")
13291388
elif json_output :
13301389
print(json.dumps(res.payload, indent=4))
1331-
await mc.commands.get_contacts()
13321390

13331391
case "reset_path" | "rp" :
13341392
argnum = 1
@@ -1344,9 +1402,11 @@ async def next_cmd(mc, cmds, json_output=False):
13441402
logger.debug(res)
13451403
if res.type == EventType.ERROR:
13461404
print(f"Error resetting path: {res}")
1347-
elif json_output :
1348-
print(json.dumps(res.payload, indent=4))
1349-
await mc.commands.get_contacts()
1405+
else:
1406+
if json_output :
1407+
print(json.dumps(res.payload, indent=4))
1408+
contact["out_path"] = ""
1409+
contact["out_path_len"] = -1
13501410

13511411
case "share_contact" | "sc":
13521412
argnum = 1
@@ -1379,10 +1439,11 @@ async def next_cmd(mc, cmds, json_output=False):
13791439
logger.debug(res)
13801440
if res.type == EventType.ERROR:
13811441
print(f"Error exporting contact: {res}")
1382-
elif json_output :
1383-
print(json.dumps(res.payload))
1384-
else :
1385-
print(res.payload['uri'])
1442+
else:
1443+
if json_output :
1444+
print(json.dumps(res.payload))
1445+
else :
1446+
print(res.payload['uri'])
13861447

13871448
case "import_contact"|"ic":
13881449
argnum = 1
@@ -1392,7 +1453,8 @@ async def next_cmd(mc, cmds, json_output=False):
13921453
if res.type == EventType.ERROR:
13931454
print(f"Error while importing contact: {res}")
13941455
else:
1395-
logger.info("Contact successfully added, refresh with lc")
1456+
logger.info("Contact successfully added")
1457+
await mc.commands.get_contacts()
13961458

13971459
case "upload_contact" | "uc" :
13981460
argnum = 1
@@ -1453,8 +1515,10 @@ async def next_cmd(mc, cmds, json_output=False):
14531515
logger.debug(res)
14541516
if res.type == EventType.ERROR:
14551517
print(f"Error removing contact: {res}")
1456-
elif json_output :
1457-
print(json.dumps(res.payload, indent=4))
1518+
else:
1519+
if json_output :
1520+
print(json.dumps(res.payload, indent=4))
1521+
await mc.commands.get_contacts()
14581522

14591523
case "recv" | "r" :
14601524
res = await mc.commands.get_msg()
@@ -1738,6 +1802,8 @@ async def main(argv):
17381802
case "-j" :
17391803
json_output=True
17401804
handle_message.json_output=True
1805+
handle_advert.json_output=True
1806+
handle_path_update.json_output=True
17411807
case "-D" :
17421808
debug=True
17431809
case "-h" :
@@ -1812,6 +1878,11 @@ async def main(argv):
18121878
f.write(address)
18131879

18141880
handle_message.mc = mc # connect meshcore to handle_message
1881+
handle_advert.mc = mc
1882+
handle_path_update.mc = mc
1883+
1884+
mc.subscribe(EventType.ADVERTISEMENT, handle_advert)
1885+
mc.subscribe(EventType.PATH_UPDATE, handle_path_update)
18151886

18161887
res = await mc.commands.send_device_query()
18171888
if res.type == EventType.ERROR :

0 commit comments

Comments
 (0)