From 9b8cf19a0c313fcfa6dc230c0ac5c318aceea8a4 Mon Sep 17 00:00:00 2001 From: pdxlocations <117498748+pdxlocations@users.noreply.github.com> Date: Tue, 8 Apr 2025 21:05:32 -0700 Subject: [PATCH] Fix localhost fallback and allow tcp port (#164) * update interfaces.py * fix logging * update * update * fix devpath * fix returns * changes --- contact/utilities/interfaces.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/contact/utilities/interfaces.py b/contact/utilities/interfaces.py index d568de6..e51dcfc 100644 --- a/contact/utilities/interfaces.py +++ b/contact/utilities/interfaces.py @@ -4,20 +4,39 @@ import contact.globals as globals def initialize_interface(args): try: + if args.ble: return meshtastic.ble_interface.BLEInterface(args.ble if args.ble != "any" else None) + elif args.host: - return meshtastic.tcp_interface.TCPInterface(args.host) + try: + if ":" in args.host: + tcp_hostname, tcp_port = args.host.split(':') + else: + tcp_hostname = args.host + tcp_port = meshtastic.tcp_interface.DEFAULT_TCP_PORT + return meshtastic.tcp_interface.TCPInterface(tcp_hostname, portNumber=tcp_port) + except Exception as ex: + logging.error(f"Error connecting to {args.host}:{tcp_port}. {ex}") else: try: - return meshtastic.serial_interface.SerialInterface(args.port) + client = meshtastic.serial_interface.SerialInterface(args.port) + except FileNotFoundError as ex: + logging.error(f"The serial device at '{args.port}' was not found. {ex}") except PermissionError as ex: logging.error(f"You probably need to add yourself to the `dialout` group to use a serial connection. {ex}") except Exception as ex: logging.error(f"Unexpected error initializing interface: {ex}") - if globals.interface.devPath is None: - return meshtastic.tcp_interface.TCPInterface("localhost") - + except OSError as ex: + logging.error(f"The serial device couldn't be opened, it might be in use by another process. {ex}") + if client.devPath is None: + try: + client = meshtastic.tcp_interface.TCPInterface("localhost") + except Exception as ex: + logging.error(f"Error connecting to localhost:{ex}") + + return client + except Exception as ex: logging.critical(f"Fatal error initializing interface: {ex}") \ No newline at end of file