feat: conditionally initialize Meshtastic handler based on configuration

This commit is contained in:
sh4un
2026-01-31 18:15:00 -05:00
parent 61c8c26fda
commit b6993ae952
2 changed files with 30 additions and 20 deletions

View File

@@ -69,13 +69,18 @@ class Bridge:
self.handlers: list[object] = []
try:
self.meshtastic_handler = MeshtasticHandler(
config=config,
to_external_queue=self.to_external_queue,
from_external_queue=self.to_meshtastic_queue,
shutdown_event=self.shutdown_event,
)
self.handlers.append(self.meshtastic_handler)
# Only initialize Meshtastic handler if MESHTASTIC_SERIAL_PORT is set
if config.meshtastic_port and config.meshtastic_port.strip():
self.meshtastic_handler = MeshtasticHandler(
config=config,
to_external_queue=self.to_external_queue,
from_external_queue=self.to_meshtastic_queue,
shutdown_event=self.shutdown_event,
)
self.handlers.append(self.meshtastic_handler)
else:
self.meshtastic_handler = None
self.logger.info("MESHTASTIC_SERIAL_PORT not set; skipping Meshtastic handler initialization.")
if config.external_transport == "serial":
self.logger.info("Selected external transport: Serial")
@@ -117,7 +122,7 @@ class Bridge:
def run(self):
self.logger.info("Starting AMMB run sequence...")
if not self.meshtastic_handler or not self.external_handler:
if not self.external_handler:
self.logger.error(
"One or more handlers failed to initialize. Bridge cannot run."
)
@@ -125,13 +130,14 @@ class Bridge:
return
self.logger.info("Attempting initial network connections...")
if not self.meshtastic_handler.connect():
self.logger.critical(
"Failed to connect to Meshtastic device on startup. "
"Bridge cannot start."
)
self.stop()
return
if self.meshtastic_handler:
if not self.meshtastic_handler.connect():
self.logger.critical(
"Failed to connect to Meshtastic device on startup. "
"Bridge cannot start."
)
self.stop()
return
if not self.external_handler.connect():
handler_type = type(self.external_handler).__name__
@@ -142,7 +148,8 @@ class Bridge:
)
self.logger.info("Starting handler background tasks/threads...")
try:
self.meshtastic_handler.start_sender()
if self.meshtastic_handler:
self.meshtastic_handler.start_sender()
if isinstance(self.external_handler, MeshcoreHandler):
self.external_handler.start_threads()
elif isinstance(self.external_handler, MQTTHandler):

View File

@@ -115,10 +115,13 @@ def load_config(config_path: str = CONFIG_FILE) -> Optional[BridgeConfig]:
logger.warning("Using only defaults.")
cfg_section = config["DEFAULT"] if "DEFAULT" in config else DEFAULT_CONFIG
meshtastic_port = cfg_section.get(
"MESHTASTIC_SERIAL_PORT",
fallback=DEFAULT_CONFIG["MESHTASTIC_SERIAL_PORT"],
)
# Only set meshtastic_port if explicitly present and not commented out
if "MESHTASTIC_SERIAL_PORT" in cfg_section:
meshtastic_port = cfg_section.get("MESHTASTIC_SERIAL_PORT", "").strip()
if not meshtastic_port or meshtastic_port.startswith("#"):
meshtastic_port = None
else:
meshtastic_port = None
external_network_id = cfg_section.get(
"EXTERNAL_NETWORK_ID",
fallback=DEFAULT_CONFIG["EXTERNAL_NETWORK_ID"],