Skip to content

Commit a852bc6

Browse files
wip
potato wip update board detection wip tests pass
1 parent 5e8ab1f commit a852bc6

File tree

15 files changed

+319
-932
lines changed

15 files changed

+319
-932
lines changed

core/services/ardupilot_manager/api/v1/routers/index.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def wrapper(*args: Tuple[Any], **kwargs: dict[str, Any]) -> Any:
6060
# all the CRUD operations, we gonna keep ones that have less than 2 endpoints in the index router.
6161

6262

63-
async def target_board(board_name: Optional[str]) -> FlightController:
63+
async def target_board(board_name: Optional[str], board_id: Optional[int] = None) -> FlightController:
6464
"""Returns the board that should be used to perform operations on.
6565
6666
Most of the API routes that have operations related to board management will give the option to perform those
@@ -72,6 +72,10 @@ async def target_board(board_name: Optional[str]) -> FlightController:
7272
"""
7373
if board_name is not None:
7474
try:
75+
if board_id is not None:
76+
return next(
77+
board for board in await autopilot.available_boards(True) if board.ardupilot_board_id == board_id
78+
)
7579
return next(board for board in await autopilot.available_boards(True) if board.name == board_name)
7680
except StopIteration as error:
7781
raise ValueError("Chosen board not available.") from error
@@ -151,8 +155,13 @@ async def get_firmware_vehicle_type() -> Any:
151155
summary="Retrieve dictionary of available firmwares versions with their respective URL.",
152156
)
153157
@index_to_http_exception
154-
async def get_available_firmwares(vehicle: Vehicle, board_name: Optional[str] = None) -> Any:
155-
return autopilot.get_available_firmwares(vehicle, (await target_board(board_name)))
158+
async def get_available_firmwares(
159+
vehicle: Vehicle,
160+
board_name: Optional[str] = None,
161+
board_id: Optional[int] = None,
162+
firmware: Optional[str] = "Ardupilot",
163+
) -> Any:
164+
return autopilot.get_available_firmwares(vehicle, await target_board(board_name, board_id), firmware)
156165

157166

158167
@index_router_v1.post("/install_firmware_from_url", summary="Install firmware for given URL.")

core/services/ardupilot_manager/autopilot_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,10 @@ async def update_endpoints(self, endpoints_to_update: Set[Endpoint]) -> None:
656656
self._save_current_endpoints()
657657
await self.mavlink_manager.restart()
658658

659-
def get_available_firmwares(self, vehicle: Vehicle, board: FlightController) -> List[Firmware]:
660-
return self.firmware_manager.get_available_firmwares(vehicle, board)
659+
def get_available_firmwares(
660+
self, vehicle: Vehicle, board: FlightController, firmware_name: Optional[str] = "Ardupilot"
661+
) -> List[Firmware]:
662+
return self.firmware_manager.get_available_firmwares(vehicle, board, firmware_name)
661663

662664
async def install_firmware_from_file(
663665
self, firmware_path: pathlib.Path, board: FlightController, default_parameters: Optional[Parameters] = None

core/services/ardupilot_manager/firmware/FirmwareDownload.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
NoCandidate,
2222
NoVersionAvailable,
2323
)
24-
from typedefs import FirmwareFormat, FlightController, Platform, PlatformType, Vehicle
24+
from typedefs import Firmware, FirmwareFormat, FlightController, Platform, PlatformType, Vehicle
2525

2626
# TODO: This should be not necessary
2727
# Disable SSL verification
@@ -105,7 +105,7 @@ def download_manifest(self) -> bool:
105105

106106
return True
107107

108-
def _find_version_item(self, **args: str) -> List[Dict[str, Any]]:
108+
def _find_version_item(self, **args: str) -> List[Firmware]:
109109
"""Find version objects in the manifest that match the specific case of **args
110110
111111
The arguments should follow the same name described in the dictionary inside the manifest
@@ -117,6 +117,7 @@ def _find_version_item(self, **args: str) -> List[Dict[str, Any]]:
117117
Returns:
118118
List[Dict[str, Any]]: A list of firmware items that match the arguments.
119119
"""
120+
logger.info(f"Searching for version item with args: {args}")
120121
if not self._manifest and not self.download_manifest():
121122
raise ManifestUnavailable("Manifest file is not available. Cannot use it to find firmware candidates.")
122123

@@ -125,39 +126,45 @@ def _find_version_item(self, **args: str) -> List[Dict[str, Any]]:
125126
# Make sure that the item matches all args value
126127
for item in self._manifest["firmware"]:
127128
for key, value in args.items():
128-
real_key = key.replace("_", "-").lower()
129-
if real_key not in item or item[real_key].lower() != value.lower():
129+
real_key = key.replace("_", "-").lower() if key != "board_id" else "board_id"
130+
if real_key not in item or str(item[real_key]).lower() != str(value).lower():
130131
break
131132
else:
132133
found_version_item.append(item)
133134

134-
return found_version_item
135+
return [
136+
Firmware(
137+
board_id=item["board_id"] if "board_id" in item else None,
138+
platform=item["platform"],
139+
name=item["mav-firmware-version-type"],
140+
url=item["url"],
141+
)
142+
for item in found_version_item
143+
]
135144

136145
@temporary_cache(timeout_seconds=3600)
137-
def get_available_versions(self, vehicle: Vehicle, board: FlightController) -> List[str]:
146+
def get_available_versions(
147+
self, vehicle: Vehicle, board: FlightController, firmware_name: Optional[str] = "Ardupilot"
148+
) -> List[Firmware]:
138149
"""Get available firmware versions for the specific plataform and vehicle
139150
140151
Args:
141152
vehicle (Vehicle): Desired vehicle.
142153
board (FlightController): Desired Flight Controller.
154+
firmware (Optional[str]): Desired firmware ("Ardupilot" or "PX4").
143155
144156
Returns:
145157
List[str]: List of available versions that match the specific desired configuration.
146158
"""
147-
available_versions: List[str] = []
148-
159+
logger.info(f"Getting available versions for {vehicle=}, {board=}, {firmware_name=}")
160+
logger.error("implement px4 stuff!", firmware_name)
149161
if not self._manifest_is_valid():
150162
raise InvalidManifest("Manifest file is invalid. Cannot use it to find available versions.")
151-
152-
platform = board.platform.value if board.platform != Platform.GenericSerial else board.name
153-
platform_type = board.platform.type if board.platform != Platform.GenericSerial else PlatformType.Serial
154-
items = self._find_version_item(vehicletype=vehicle.value, platform=platform)
155-
156-
for item in items:
157-
if item["format"] == FirmwareDownloader._supported_firmware_formats[platform_type]:
158-
available_versions.append(item["mav-firmware-version-type"])
159-
160-
return available_versions
163+
if board.ardupilot_board_id is None:
164+
if board.platform != Platform.GenericSerial:
165+
return self._find_version_item(vehicletype=vehicle.value, platform=board.platform.value)
166+
return self._find_version_item(vehicletype=vehicle.value, platform=board.name)
167+
return self._find_version_item(vehicletype=vehicle.value, board_id=str(board.ardupilot_board_id))
161168

162169
@temporary_cache(timeout_seconds=3600)
163170
def get_download_url(self, vehicle: Vehicle, board: FlightController, version: str = "") -> str:
@@ -178,21 +185,21 @@ def get_download_url(self, vehicle: Vehicle, board: FlightController, version: s
178185
if not versions:
179186
raise NoVersionAvailable(f"Could not find available firmware versions for {board}/{vehicle}.")
180187

181-
if version and version not in versions:
188+
if version and not any(version == found_version.name for found_version in versions):
182189
raise NoVersionAvailable(f"Version {version} was not found for {board}/{vehicle}.")
183190

184-
firmware_format = FirmwareDownloader._supported_firmware_formats[board.platform.type]
191+
firmware_format = FirmwareDownloader._supported_firmware_formats[board.platform.type].value
185192

186193
# Autodetect the latest supported version.
187194
# For .apj firmwares (e.g. Pixhawk), we use the latest STABLE version while for the others (e.g. SITL and
188195
# Navigator) we use latest BETA. Specially on this development phase of the BlueOS/navigator, using
189196
# the BETA release allow us to track and fix introduced bugs faster.
190197
if not version:
191198
if firmware_format == FirmwareFormat.APJ:
192-
supported_versions = [version for version in versions if "STABLE" in version]
199+
supported_versions = [version for version in versions if "STABLE" in version.name]
193200
newest_version: Optional[str] = None
194201
for supported_version in supported_versions:
195-
semver_version = supported_version.split("-")[1]
202+
semver_version = supported_version.name.split("-")[1]
196203
if not newest_version or Version(newest_version) < Version(semver_version):
197204
newest_version = semver_version
198205
if not newest_version:
@@ -219,7 +226,7 @@ def get_download_url(self, vehicle: Vehicle, board: FlightController, version: s
219226

220227
item = items[0]
221228
logger.debug(f"Downloading following firmware: {item}")
222-
return str(item["url"])
229+
return item.url
223230

224231
def download(self, vehicle: Vehicle, board: FlightController, version: str = "") -> pathlib.Path:
225232
"""Download a specific firmware that matches the arguments.

0 commit comments

Comments
 (0)