TC2-BBS-mesh/config_init.py
TC² 4f385cffcd
Update config_init.py
Fix issue where server.py wouldn't run if the [sync] portion of the config.ini was commented out.
2024-06-25 18:15:46 -04:00

45 lines
1.8 KiB
Python

import configparser
import time
import meshtastic.serial_interface
import meshtastic.tcp_interface
import serial.tools.list_ports
def initialize_config():
config = configparser.ConfigParser()
config.read('config.ini')
interface_type = config['interface']['type']
hostname = config['interface'].get('hostname', None)
port = config['interface'].get('port', None)
bbs_nodes = config.get('sync', 'bbs_nodes', fallback='').split(',')
if bbs_nodes == ['']:
bbs_nodes = []
return config, interface_type, hostname, port, bbs_nodes
def get_interface(interface_type, hostname=None, port=None):
while True:
try:
if interface_type == 'serial':
if port:
return meshtastic.serial_interface.SerialInterface(port)
else:
ports = list(serial.tools.list_ports.comports())
if len(ports) == 1:
return meshtastic.serial_interface.SerialInterface(ports[0].device)
elif len(ports) > 1:
port_list = ', '.join([p.device for p in ports])
raise ValueError(f"Multiple serial ports detected: {port_list}. Specify one with the 'port' argument.")
else:
raise ValueError("No serial ports detected.")
elif interface_type == 'tcp':
if not hostname:
raise ValueError("Hostname must be specified for TCP interface")
return meshtastic.tcp_interface.TCPInterface(hostname=hostname)
else:
raise ValueError("Invalid interface type specified in config file")
except PermissionError as e:
print(f"PermissionError: {e}. Retrying in 5 seconds...")
time.sleep(5)