Start to implement devices enrollment
This commit is contained in:
		
							
								
								
									
										52
									
								
								central_backend/src/devices/device.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								central_backend/src/devices/device.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct DeviceInfo {
 | 
			
		||||
    reference: String,
 | 
			
		||||
    version: semver::Version,
 | 
			
		||||
    max_relays: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)]
 | 
			
		||||
pub struct DeviceId(pub String);
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct Device {
 | 
			
		||||
    /// The device ID
 | 
			
		||||
    id: DeviceId,
 | 
			
		||||
    /// Information about the device
 | 
			
		||||
    device: DeviceInfo,
 | 
			
		||||
    /// Name given to the device on the Web UI
 | 
			
		||||
    name: String,
 | 
			
		||||
    /// Description given to the device on the Web UI
 | 
			
		||||
    description: String,
 | 
			
		||||
    /// Specify whether the device is enabled or not
 | 
			
		||||
    enabled: bool,
 | 
			
		||||
    /// Specify whether the device has been validated or not
 | 
			
		||||
    validated: bool,
 | 
			
		||||
    /// Information about the relays handled by the device
 | 
			
		||||
    relays: Vec<DeviceRelay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Structure that contains information about the minimal expected execution
 | 
			
		||||
/// time of a device
 | 
			
		||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct DailyMinRuntime {
 | 
			
		||||
    min_runtime: usize,
 | 
			
		||||
    reset_time: usize,
 | 
			
		||||
    catch_up_hours: Vec<usize>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
 | 
			
		||||
pub struct DeviceRelayID(uuid::Uuid);
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct DeviceRelay {
 | 
			
		||||
    id: DeviceRelayID,
 | 
			
		||||
    name: String,
 | 
			
		||||
    enabled: bool,
 | 
			
		||||
    priority: usize,
 | 
			
		||||
    consumption: usize,
 | 
			
		||||
    minimal_uptime: usize,
 | 
			
		||||
    minimal_downtime: usize,
 | 
			
		||||
    daily_runtime: Option<DailyMinRuntime>,
 | 
			
		||||
    depends_on: Vec<DeviceRelay>,
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								central_backend/src/devices/devices_list.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								central_backend/src/devices/devices_list.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
use crate::app_config::AppConfig;
 | 
			
		||||
use crate::devices::device::{Device, DeviceId};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
pub struct DevicesList(HashMap<DeviceId, Device>);
 | 
			
		||||
 | 
			
		||||
impl DevicesList {
 | 
			
		||||
    /// Load the list of devices. This method should be called only once during the whole execution
 | 
			
		||||
    /// of the program
 | 
			
		||||
    pub fn load() -> anyhow::Result<Self> {
 | 
			
		||||
        let mut list = Self(HashMap::new());
 | 
			
		||||
 | 
			
		||||
        for f in std::fs::read_dir(AppConfig::get().devices_config_path())? {
 | 
			
		||||
            let f = f?.file_name();
 | 
			
		||||
            let f = f.to_string_lossy();
 | 
			
		||||
 | 
			
		||||
            let dev_id = match f.strip_suffix(".conf") {
 | 
			
		||||
                Some(s) => DeviceId(s.to_string()),
 | 
			
		||||
 | 
			
		||||
                // This is not a device configuration file
 | 
			
		||||
                None => continue,
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            let device_conf = std::fs::read(AppConfig::get().device_config_path(&dev_id))?;
 | 
			
		||||
 | 
			
		||||
            list.0.insert(dev_id, serde_json::from_slice(&device_conf)?);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(list)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Check if a device with a given id exists or not
 | 
			
		||||
    pub fn exists(&self, id: &DeviceId) -> bool {
 | 
			
		||||
        self.0.contains_key(id)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								central_backend/src/devices/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								central_backend/src/devices/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
pub mod device;
 | 
			
		||||
pub mod devices_list;
 | 
			
		||||
		Reference in New Issue
	
	Block a user