Get the list of OTA updates for a given platform
This commit is contained in:
		@@ -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())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<bool> {
 | 
			
		||||
@@ -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<Vec<OTAUpdate>> {
 | 
			
		||||
    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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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(
 | 
			
		||||
 
 | 
			
		||||
@@ -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<ListOTAPath>) -> 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<Vec<DeviceId>>,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user