mirror of
https://github.com/pdxlocations/contact.git
synced 2026-05-09 14:54:27 +02:00
add simple argparse args for connection
This commit is contained in:
+51
-4
@@ -7,7 +7,7 @@ V 0.1.7
|
||||
'''
|
||||
|
||||
import curses
|
||||
import meshtastic.serial_interface, meshtastic.tcp_interface
|
||||
import meshtastic.serial_interface, meshtastic.tcp_interface, meshtastic.ble_interface
|
||||
from pubsub import pub
|
||||
import textwrap # Import the textwrap module
|
||||
|
||||
@@ -19,9 +19,55 @@ 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='192.168.xx.xx')
|
||||
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()
|
||||
|
||||
@@ -447,4 +493,5 @@ def main(stdscr):
|
||||
pub.subscribe(on_receive, 'meshtastic.receive')
|
||||
|
||||
if __name__ == "__main__":
|
||||
curses.wrapper(main)
|
||||
curses.wrapper(main)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user