Skip to content

Commit 601c19c

Browse files
committed
implement node_discover
1 parent 803e339 commit 601c19c

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from meshcore import MeshCore, EventType, logger
3434

3535
# Version
36-
VERSION = "v1.2.6"
36+
VERSION = "v1.2.7"
3737

3838
# default ble address is stored in a config file
3939
MCCLI_CONFIG_DIR = str(Path.home()) + "/.config/meshcore/"
@@ -451,6 +451,7 @@ def make_completion_dict(contacts, pending={}, to=None, channels=None):
451451
"share_contact" : contact_list,
452452
"path": contact_list,
453453
"disc_path" : contact_list,
454+
"node_discover": {"all":None, "sens":None, "rep":None, "comp":None, "room":None, "cli":None},
454455
"trace" : None,
455456
"reset_path" : contact_list,
456457
"change_path" : contact_list,
@@ -580,6 +581,7 @@ def make_completion_dict(contacts, pending={}, to=None, channels=None):
580581
"neighbors" : None,
581582
"req_acl":None,
582583
"setperm":contact_list,
584+
"region" : {"get":None, "allowf": None, "denyf": None, "put": None, "remove": None, "save": None, "home": None},
583585
"gps" : {"on":None,"off":None,"sync":None,"setloc":None,
584586
"advert" : {"none": None, "share": None, "prefs": None},
585587
},
@@ -2217,7 +2219,7 @@ async def next_cmd(mc, cmds, json_output=False):
22172219
argnum = 2
22182220
dest = None
22192221

2220-
if len(cmds[1]) == 12: # possibly an hex prefix
2222+
if len(cmds[1]) == 12: # possibly an hex prefix
22212223
try:
22222224
dest = bytes.fromhex(cmds[1])
22232225
except ValueError:
@@ -2415,6 +2417,52 @@ async def next_cmd(mc, cmds, json_output=False):
24152417
inp = inp if inp != "" else "direct"
24162418
print(f"Path for {contact['adv_name']}: out {outp}, in {inp}")
24172419

2420+
case "node_discover"|"nd" :
2421+
argnum = 1
2422+
try: # try to decode type as int
2423+
types = int(cmds[1])
2424+
except ValueError:
2425+
if "all" in cmds[1]:
2426+
types = 0xFF
2427+
else :
2428+
types = 0
2429+
if "rep" in cmds[1]:
2430+
types = types | 4
2431+
if "cli" in cmds[1] or "comp" in cmds[1]:
2432+
types = types | 2
2433+
if "room" in cmds[1]:
2434+
types = types | 8
2435+
if "sens" in cmds[1]:
2436+
types = types | 16
2437+
2438+
res = await mc.commands.send_node_discover_req(types)
2439+
if res is None or res.type == EventType.ERROR:
2440+
print("Error sending discover request")
2441+
else:
2442+
exp_tag = res.payload["tag"].to_bytes(4, "little").hex()
2443+
dn = []
2444+
while True:
2445+
r = await mc.wait_for_event(
2446+
EventType.DISCOVER_RESPONSE,
2447+
attribute_filters={"tag":exp_tag},
2448+
timeout = 5
2449+
)
2450+
if r is None or r.type == EventType.ERROR:
2451+
break
2452+
else:
2453+
dn.append(r.payload)
2454+
2455+
if json_output:
2456+
print(json.dumps(dn))
2457+
else:
2458+
await mc.ensure_contacts()
2459+
print(f"Discovered {len(dn)} nodes:")
2460+
for n in dn:
2461+
name = mc.get_contact_by_key_prefix(n["pubkey"])['adv_name']
2462+
if name is None:
2463+
name = n["pubkey"][0:12]
2464+
print(f" {name:12} type {n['node_type']} SNR: {n['SNR_in']:6,.2f}->{n['SNR']:6,.2f} RSSI: ->{n['RSSI']:4}")
2465+
24182466
case "req_btelemetry"|"rbt" :
24192467
argnum = 1
24202468
await mc.ensure_contacts()
@@ -2968,6 +3016,7 @@ def command_help():
29683016
time <epoch> : sets time to given epoch
29693017
clock : get current time
29703018
clock sync : sync device clock st
3019+
node_discover <filter> : discovers nodes based on their type nd
29713020
Contacts
29723021
contacts / list : gets contact list lc
29733022
reload_contacts : force reloading all contacts rc
@@ -3041,7 +3090,18 @@ def get_help_for (cmdname, context="line") :
30413090
at t=2,u>1d,d cn trace
30423091
# tries to do flood login to all repeaters
30433092
at t=2 rp login
3044-
""")
3093+
""")
3094+
3095+
if cmdname == "node_discover" or cmdname == "nd" :
3096+
print("""node_discover <filter> : discovers 0-hop nodes and displays signal info
3097+
3098+
filter can be "all" for all types or nodes or a comma separated list consisting of :
3099+
- cli or comp for companions
3100+
- rep for repeaters
3101+
- sens for sensors
3102+
- room for chat rooms
3103+
""")
3104+
30453105
else:
30463106
print(f"Sorry, no help yet for {cmdname}")
30473107

0 commit comments

Comments
 (0)