Merge pull request #10 from ianmcorvidae/argparse

add simple argparse args for connection
This commit is contained in:
pdxlocations
2024-10-07 22:16:54 -07:00
committed by GitHub

View File

@@ -19,11 +19,57 @@ except ImportError:
from settings import settings
import argparse
parser = argparse.ArgumentParser(
add_help=False,
epilog="If no connection arguments are specified, we attempt a serial connection and then a TCP connection to localhost.")
connOuter = parser.add_argument_group('Connection', 'Optional arguments to specify a device to connect to and how.')
conn = connOuter.add_mutually_exclusive_group()
conn.add_argument(
"--port",
"--serial",
"-s",
help="The port to connect to via serial, e.g. `/dev/ttyUSB0`.",
nargs="?",
default=None,
const=None,
)
conn.add_argument(
"--host",
"--tcp",
"-t",
help="The hostname or IP address to connect to using TCP.",
nargs="?",
default=None,
const="localhost",
)
conn.add_argument(
"--ble",
"-b",
help="The BLE device MAC address or name to connect to.",
nargs="?",
default=None,
const="any"
)
args = parser.parse_args()
# Initialize Meshtastic interface
interface = meshtastic.serial_interface.SerialInterface()
# interface = meshtastic.tcp_interface.TCPInterface(hostname='meshtastic.local')
# discover ble with 'meshtsastic --ble-scan' and replace the MAC address below
# interface = meshtastic.ble_interface.BLEInterface('AA:BB:CC:DD:EE:FF')
if args.ble:
interface = meshtastic.ble_interface.BLEInterface(args.ble if args.ble != "any" else None)
elif args.host:
interface = meshtastic.tcp_interface.TCPInterface(args.host)
else:
try:
interface = meshtastic.serial_interface.SerialInterface(args.port)
except PermissionError as ex:
print("You probably need to add yourself to the `dialout` group to use a serial connection.")
if interface.devPath is None:
interface = meshtastic.tcp_interface.TCPInterface("localhost")
myinfo = interface.getMyNodeInfo()
@@ -449,4 +495,5 @@ def main(stdscr):
pub.subscribe(on_receive, 'meshtastic.receive')
if __name__ == "__main__":
curses.wrapper(main)
curses.wrapper(main)