Can set device desired version
This commit is contained in:
		@@ -189,11 +189,13 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
 | 
			
		||||
                "/web_api/ota/{platform}/{version}",
 | 
			
		||||
                web::post().to(ota_controller::upload_firmware),
 | 
			
		||||
            )
 | 
			
		||||
            // TODO : upload a new software update
 | 
			
		||||
            // TODO : list ota software update per platform
 | 
			
		||||
            // TODO : download a OTA file
 | 
			
		||||
            // TODO : delete an OTA file
 | 
			
		||||
            // TODO : deploy an update to a device
 | 
			
		||||
            .route(
 | 
			
		||||
                "/web_api/ota/set_desired_version",
 | 
			
		||||
                web::post().to(ota_controller::set_desired_version),
 | 
			
		||||
            )
 | 
			
		||||
            // Logging controller API
 | 
			
		||||
            .route(
 | 
			
		||||
                "/web_api/logging/logs",
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,10 @@
 | 
			
		||||
use crate::constants;
 | 
			
		||||
use crate::devices::device::DeviceId;
 | 
			
		||||
use crate::energy::energy_actor;
 | 
			
		||||
use crate::ota::ota_manager;
 | 
			
		||||
use crate::ota::ota_update::OTAPlatform;
 | 
			
		||||
use crate::server::custom_error::HttpResult;
 | 
			
		||||
use crate::server::WebEnergyActor;
 | 
			
		||||
use actix_multipart::form::tempfile::TempFile;
 | 
			
		||||
use actix_multipart::form::MultipartForm;
 | 
			
		||||
use actix_web::{web, HttpResponse};
 | 
			
		||||
@@ -49,3 +52,48 @@ pub async fn upload_firmware(
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Accepted().body("OTA update successfully saved."))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Deserialize)]
 | 
			
		||||
pub struct SetDesiredDeviceVersion {
 | 
			
		||||
    devices: Option<Vec<DeviceId>>,
 | 
			
		||||
    platform: Option<OTAPlatform>,
 | 
			
		||||
    version: semver::Version,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn set_desired_version(
 | 
			
		||||
    actor: WebEnergyActor,
 | 
			
		||||
    body: web::Json<SetDesiredDeviceVersion>,
 | 
			
		||||
) -> HttpResult {
 | 
			
		||||
    if body.devices.is_none() && body.platform.is_none() {
 | 
			
		||||
        return Ok(
 | 
			
		||||
            HttpResponse::BadRequest().json("Must specify one filter to select target devices!")
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let devices = actor.send(energy_actor::GetDeviceLists).await?;
 | 
			
		||||
 | 
			
		||||
    for d in devices {
 | 
			
		||||
        // Filter per platform
 | 
			
		||||
        if let Some(p) = body.platform {
 | 
			
		||||
            if d.info.reference != p.to_string() {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Filter per device
 | 
			
		||||
        if let Some(ids) = &body.devices {
 | 
			
		||||
            if !ids.contains(&d.id) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        actor
 | 
			
		||||
            .send(energy_actor::SetDesiredVersion(
 | 
			
		||||
                d.id,
 | 
			
		||||
                Some(body.version.clone()),
 | 
			
		||||
            ))
 | 
			
		||||
            .await??;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok().finish())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user