Can create a relay
This commit is contained in:
		@@ -13,7 +13,7 @@ pub struct DeviceInfo {
 | 
			
		||||
    /// Device firmware / software version
 | 
			
		||||
    version: semver::Version,
 | 
			
		||||
    /// Maximum number of relay that the device can support
 | 
			
		||||
    max_relays: usize,
 | 
			
		||||
    pub max_relays: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl DeviceInfo {
 | 
			
		||||
@@ -90,7 +90,7 @@ impl Default for DeviceRelayID {
 | 
			
		||||
pub struct DeviceRelay {
 | 
			
		||||
    /// Device relay id. Should be unique across the whole application
 | 
			
		||||
    #[serde(default)]
 | 
			
		||||
    id: DeviceRelayID,
 | 
			
		||||
    pub id: DeviceRelayID,
 | 
			
		||||
    /// Human-readable name for the relay
 | 
			
		||||
    name: String,
 | 
			
		||||
    /// Whether this relay can be turned on or not
 | 
			
		||||
@@ -165,7 +165,7 @@ impl DeviceRelay {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let mut relays_map = list.iter().map(|r| (r.id, r)).collect::<HashMap<_, _>>();
 | 
			
		||||
        let relays_map = list.iter().map(|r| (r.id, r)).collect::<HashMap<_, _>>();
 | 
			
		||||
 | 
			
		||||
        if self.depends_on.iter().any(|d| !relays_map.contains_key(d)) {
 | 
			
		||||
            return Some("A specified dependent relay does not exists!");
 | 
			
		||||
@@ -212,12 +212,12 @@ impl DeviceRelay {
 | 
			
		||||
        clone.insert(self.id);
 | 
			
		||||
 | 
			
		||||
        for d in &self.depends_on {
 | 
			
		||||
            if visited.contains(&d) {
 | 
			
		||||
            if visited.contains(d) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if list
 | 
			
		||||
                .get(&d)
 | 
			
		||||
                .get(d)
 | 
			
		||||
                .expect("Missing a relay!")
 | 
			
		||||
                .check_for_loop_in_dependencies(&clone, list)
 | 
			
		||||
            {
 | 
			
		||||
@@ -235,7 +235,7 @@ impl DeviceRelay {
 | 
			
		||||
        list: &HashMap<DeviceRelayID, &Self>,
 | 
			
		||||
    ) {
 | 
			
		||||
        for d in &self.depends_on {
 | 
			
		||||
            let dependency = list.get(&d).expect("Missing a relay!");
 | 
			
		||||
            let dependency = list.get(d).expect("Missing a relay!");
 | 
			
		||||
 | 
			
		||||
            deps_out.insert(dependency.id);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
use crate::app_config::AppConfig;
 | 
			
		||||
use crate::crypto::pki;
 | 
			
		||||
use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo, DeviceRelay};
 | 
			
		||||
use crate::devices::device::{
 | 
			
		||||
    Device, DeviceGeneralInfo, DeviceId, DeviceInfo, DeviceRelay, DeviceRelayID,
 | 
			
		||||
};
 | 
			
		||||
use crate::utils::time_utils::time_secs;
 | 
			
		||||
use openssl::x509::{X509Req, X509};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
@@ -194,10 +196,29 @@ impl DevicesList {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the full list of relays
 | 
			
		||||
    pub fn relays_list(&mut self) -> Vec<DeviceRelay> {
 | 
			
		||||
    pub fn relays_list(&self) -> Vec<DeviceRelay> {
 | 
			
		||||
        self.0
 | 
			
		||||
            .iter()
 | 
			
		||||
            .flat_map(|(_id, d)| d.relays.clone())
 | 
			
		||||
            .collect()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Create a new relay
 | 
			
		||||
    pub fn relay_create(&mut self, dev_id: &DeviceId, relay: DeviceRelay) -> anyhow::Result<()> {
 | 
			
		||||
        let dev = self
 | 
			
		||||
            .0
 | 
			
		||||
            .get_mut(dev_id)
 | 
			
		||||
            .ok_or(DevicesListError::DeviceNotFound)?;
 | 
			
		||||
 | 
			
		||||
        dev.relays.push(relay);
 | 
			
		||||
 | 
			
		||||
        self.persist_dev_config(dev_id)?;
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get a single relay
 | 
			
		||||
    pub fn relay_get_single(&self, relay_id: DeviceRelayID) -> Option<DeviceRelay> {
 | 
			
		||||
        self.relays_list().into_iter().find(|i| i.id == relay_id)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user