From d38040cb98be1f10139043c882511be56aa77382 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 5 Oct 2024 19:35:23 +0200 Subject: [PATCH] Get the list of OTA updates for a given platform --- central_backend/src/app_config.rs | 9 +++++--- central_backend/src/ota/ota_manager.rs | 23 ++++++++++++++++++- central_backend/src/ota/ota_update.rs | 6 ++--- central_backend/src/server/servers.rs | 6 ++++- .../src/server/web_api/ota_controller.rs | 12 ++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/central_backend/src/app_config.rs b/central_backend/src/app_config.rs index 50b421a..364bcbd 100644 --- a/central_backend/src/app_config.rs +++ b/central_backend/src/app_config.rs @@ -300,11 +300,14 @@ impl AppConfig { self.storage_path().join("ota") } + /// Get the directory that will store OTA updates for a given platform + pub fn ota_platform_dir(&self, platform: OTAPlatform) -> PathBuf { + self.ota_dir().join(platform.to_string()) + } + /// Get the path to the file that will contain an OTA update pub fn path_ota_update(&self, platform: OTAPlatform, version: &semver::Version) -> PathBuf { - self.ota_dir() - .join(platform.to_string()) - .join(version.to_string()) + self.ota_platform_dir(platform).join(version.to_string()) } } diff --git a/central_backend/src/ota/ota_manager.rs b/central_backend/src/ota/ota_manager.rs index f954631..b1d56b0 100644 --- a/central_backend/src/ota/ota_manager.rs +++ b/central_backend/src/ota/ota_manager.rs @@ -1,6 +1,8 @@ use crate::app_config::AppConfig; -use crate::ota::ota_update::OTAPlatform; +use crate::ota::ota_update::{OTAPlatform, OTAUpdate}; use crate::utils::files_utils; +use std::os::unix::fs::MetadataExt; +use std::str::FromStr; /// Check out whether a given update exists or not pub fn update_exists(platform: OTAPlatform, version: &semver::Version) -> anyhow::Result { @@ -22,3 +24,22 @@ pub fn save_update( Ok(()) } + +/// Get the list of OTA software updates for a platform +pub fn get_ota_updates_for_platform(platform: OTAPlatform) -> anyhow::Result> { + let ota_path = AppConfig::get().ota_platform_dir(platform); + + let mut out = Vec::new(); + + for e in std::fs::read_dir(ota_path)? { + let e = e?; + + out.push(OTAUpdate { + platform, + version: semver::Version::from_str(e.file_name().to_str().unwrap_or("bad"))?, + file_size: e.metadata()?.size(), + }); + } + + Ok(out) +} diff --git a/central_backend/src/ota/ota_update.rs b/central_backend/src/ota/ota_update.rs index 23f3254..4227720 100644 --- a/central_backend/src/ota/ota_update.rs +++ b/central_backend/src/ota/ota_update.rs @@ -16,7 +16,7 @@ impl Display for OTAPlatform { /// Single OTA update information #[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Eq, PartialEq)] pub struct OTAUpdate { - platform: OTAPlatform, - version: semver::Version, - file_size: usize, + pub platform: OTAPlatform, + pub version: semver::Version, + pub file_size: u64, } diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index bc8dffe..9c76ad7 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -189,7 +189,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> "/web_api/ota/{platform}/{version}", web::post().to(ota_controller::upload_firmware), ) - // TODO : list ota software update per platform + // TODO : list all ota software updates + .route( + "/web_api/ota/{platform}", + web::get().to(ota_controller::list_updates_platform), + ) // TODO : download a OTA file // TODO : delete an OTA file .route( diff --git a/central_backend/src/server/web_api/ota_controller.rs b/central_backend/src/server/web_api/ota_controller.rs index e5f6f4d..c709449 100644 --- a/central_backend/src/server/web_api/ota_controller.rs +++ b/central_backend/src/server/web_api/ota_controller.rs @@ -53,6 +53,18 @@ pub async fn upload_firmware( Ok(HttpResponse::Accepted().body("OTA update successfully saved.")) } +#[derive(serde::Deserialize)] +pub struct ListOTAPath { + platform: OTAPlatform, +} + +/// List OTA software updates for a given platform +pub async fn list_updates_platform(path: web::Path) -> HttpResult { + let list = ota_manager::get_ota_updates_for_platform(path.platform)?; + + Ok(HttpResponse::Ok().json(list)) +} + #[derive(serde::Deserialize)] pub struct SetDesiredDeviceVersion { devices: Option>,