From 05c99decab74c9ba61fa65af4fdfa65364e82a62 Mon Sep 17 00:00:00 2001 From: Joao Mario Lago Date: Mon, 30 Sep 2024 10:08:42 -0300 Subject: [PATCH] core: autopilot_manager: Add fallback router init * Make autopilot manager fallback to first avbailable router when unable to start current preferred router --- .../ardupilot_manager/autopilot_manager.py | 18 ++++++++++++++---- .../mavlink_proxy/AbstractRouter.py | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/services/ardupilot_manager/autopilot_manager.py b/core/services/ardupilot_manager/autopilot_manager.py index dcd9845c9..fa2929c3a 100644 --- a/core/services/ardupilot_manager/autopilot_manager.py +++ b/core/services/ardupilot_manager/autopilot_manager.py @@ -54,10 +54,20 @@ def __init__(self) -> None: async def setup(self) -> None: # This is the logical continuation of __init__(), extracted due to its async nature self.configuration = deepcopy(self.settings.content) - self.mavlink_manager = MavlinkManager(self.load_preferred_router()) - if not self.load_preferred_router(): - await self.set_preferred_router(self.mavlink_manager.available_interfaces()[0].name()) - logger.info(f"Setting {self.mavlink_manager.available_interfaces()[0].name()} as preferred router.") + + self.mavlink_manager = MavlinkManager() + preferred_router = self.load_preferred_router() + try: + self.mavlink_manager = MavlinkManager(preferred_router) + except ValueError as error: + logger.warning( + f"Failed to start MavlinkManager[{preferred_router}]. Falling back to the first available router. Error details: {error}" + ) + preferred_router = None + + if not preferred_router: + await self.set_preferred_router(self.mavlink_manager.tool.name()) + logger.info(f"Setting {self.mavlink_manager.tool.name()} as preferred router.") self.mavlink_manager.set_logdir(self.settings.log_path) self._load_endpoints() diff --git a/core/services/ardupilot_manager/mavlink_proxy/AbstractRouter.py b/core/services/ardupilot_manager/mavlink_proxy/AbstractRouter.py index 2c54278d7..be08dba10 100644 --- a/core/services/ardupilot_manager/mavlink_proxy/AbstractRouter.py +++ b/core/services/ardupilot_manager/mavlink_proxy/AbstractRouter.py @@ -81,7 +81,7 @@ def get_interface(name: str) -> Type["AbstractRouter"]: for interface in AbstractRouter.__subclasses__(): if interface.is_ok() and interface.name() == name: return interface - raise RuntimeError("Interface is not ok or does not exist.") + raise ValueError("Interface is not ok or does not exist.") def binary(self) -> Optional[str]: return self._binary