Notify devices of available updates through sync endpoint
This commit is contained in:
		@@ -11,7 +11,7 @@ pub struct DeviceInfo {
 | 
			
		||||
    /// Device reference
 | 
			
		||||
    pub reference: String,
 | 
			
		||||
    /// Device firmware / software version
 | 
			
		||||
    version: semver::Version,
 | 
			
		||||
    pub version: semver::Version,
 | 
			
		||||
    /// Maximum number of relay that the device can support
 | 
			
		||||
    pub max_relays: usize,
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
use std::fmt::{Display, Formatter};
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
 | 
			
		||||
pub enum OTAPlatform {
 | 
			
		||||
@@ -13,6 +14,14 @@ impl Display for OTAPlatform {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromStr for OTAPlatform {
 | 
			
		||||
    type Err = anyhow::Error;
 | 
			
		||||
 | 
			
		||||
    fn from_str(s: &str) -> Result<Self, Self::Err> {
 | 
			
		||||
        Ok(serde_json::from_str::<Self>(&format!("\"{s}\""))?)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Single OTA update information
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Eq, PartialEq)]
 | 
			
		||||
pub struct OTAUpdate {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,12 +2,15 @@ use crate::app_config::AppConfig;
 | 
			
		||||
use crate::devices::device::{DeviceId, DeviceInfo};
 | 
			
		||||
use crate::energy::energy_actor;
 | 
			
		||||
use crate::energy::energy_actor::RelaySyncStatus;
 | 
			
		||||
use crate::ota::ota_manager;
 | 
			
		||||
use crate::ota::ota_update::OTAPlatform;
 | 
			
		||||
use crate::server::custom_error::HttpResult;
 | 
			
		||||
use crate::server::devices_api::jwt_parser::JWTRequest;
 | 
			
		||||
use crate::server::WebEnergyActor;
 | 
			
		||||
use actix_web::{web, HttpResponse};
 | 
			
		||||
use openssl::nid::Nid;
 | 
			
		||||
use openssl::x509::X509Req;
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, serde::Deserialize)]
 | 
			
		||||
pub struct EnrollRequest {
 | 
			
		||||
@@ -135,6 +138,7 @@ struct Claims {
 | 
			
		||||
#[derive(serde::Serialize)]
 | 
			
		||||
struct SyncResult {
 | 
			
		||||
    relays: Vec<RelaySyncStatus>,
 | 
			
		||||
    available_update: Option<semver::Version>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Synchronize device
 | 
			
		||||
@@ -142,8 +146,25 @@ pub async fn sync_device(body: web::Json<JWTRequest>, actor: WebEnergyActor) ->
 | 
			
		||||
    let (device, claims) = body.0.parse_jwt::<Claims>(actor.clone()).await?;
 | 
			
		||||
 | 
			
		||||
    let relays = actor
 | 
			
		||||
        .send(energy_actor::SynchronizeDevice(device.id, claims.info))
 | 
			
		||||
        .send(energy_actor::SynchronizeDevice(
 | 
			
		||||
            device.id,
 | 
			
		||||
            claims.info.clone(),
 | 
			
		||||
        ))
 | 
			
		||||
        .await??;
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok().json(SyncResult { relays }))
 | 
			
		||||
    let mut available_update = None;
 | 
			
		||||
 | 
			
		||||
    // Check if the version is available
 | 
			
		||||
    if let Some(desired) = device.desired_version {
 | 
			
		||||
        if claims.info.version < desired
 | 
			
		||||
            && ota_manager::update_exists(OTAPlatform::from_str(&claims.info.reference)?, &desired)?
 | 
			
		||||
        {
 | 
			
		||||
            available_update = Some(desired);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok().json(SyncResult {
 | 
			
		||||
        relays,
 | 
			
		||||
        available_update,
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user