diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index c1c6a51..cf011e1 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -8,6 +8,7 @@ import google.protobuf.json_format import pyqrcode import traceback import pkg_resources +from easy_table import EasyTable """We only import the tunnel code if we are on a platform that can run it""" have_tunnel = platform.system() == 'Linux' @@ -132,6 +133,26 @@ def setRouter(interface, on): prefs.gps_attempt_time = 0 prefs.gps_update_interval = 0 +def printNodes(nodes): + #Create the table and define the structure + table = EasyTable("Nodes") + table.setCorners("/", "\\", "\\", "/") + table.setOuterStructure("|", "-") + table.setInnerStructure("|", "-", "+") + + tableData = [] + for node in nodes: + #aux var to get not defined keys + lat=node['position'].get("latitude", "N/A") + lon=node['position'].get("longitude", "N/A") + alt=node['position'].get("altitude", "N/A") + batt=node['position'].get("batteryLevel", "N/A") + snr=node.get("snr", "N/A") + tableData.append({"User":node['user']['longName'], + "Position":"Lat:"+lat+",Lon:"+lon+",Alt:"+alt, + "Battery":batt, "SNR":snr}) + table.setData(tableData) + table.displayTable() def onConnected(interface): """Callback invoked when we connect to a radio""" @@ -273,6 +294,10 @@ def onConnected(interface): for n in interface.nodes.values(): print(n) + if args.nodes: + closeNow = True + printNodes(interface.nodes.values()) + if args.qr: closeNow = True print(f"Channel URL {interface.channelURL}") @@ -283,7 +308,7 @@ def onConnected(interface): from . import tunnel closeNow = False # Even if others said we could close, stay open if the user asked for a tunnel tunnel.Tunnel(interface, subnet=args.tunnel_net) - + except Exception as ex: print(ex) @@ -291,7 +316,6 @@ def onConnected(interface): if (not args.seriallog) and closeNow: interface.close() # after running command then exit - def onNode(node): """Callback invoked when the node DB changes""" print(f"Node changed: {node}") @@ -316,7 +340,7 @@ def common(): args.destOrAll = "^all" if not args.seriallog: - if args.info or args.set or args.seturl or args.setowner or args.setlat or args.setlon or \ + if args.info or args.nodes or args.set or args.seturl or args.setowner or args.setlat or args.setlon or \ args.settime or \ args.setch_longslow or args.setch_shortfast or args.setstr or args.setchan or args.sendtext or \ args.router != None or args.qr: @@ -367,6 +391,9 @@ def initParser(): parser.add_argument("--info", help="Read and display the radio config information", action="store_true") + parser.add_argument("--nodes", help="Print Node List in a pretty formatted table", + action="store_true") + parser.add_argument("--qr", help="Display the QR code that corresponds to the current channel", action="store_true") diff --git a/setup.py b/setup.py index 7f01ed4..ac6167d 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ setup( include_package_data=True, install_requires=["pyserial>=3.4", "protobuf>=3.13.0", "pypubsub>=4.0.3", "dotmap>=1.3.14", "pexpect>=4.6.0", "pyqrcode>=1.2.1", - "pygatt>=4.0.5"], + "pygatt>=4.0.5", "easy-table>=0.0.4"], extras_require={ 'tunnel': ["pytap2>=2.0.0"] },