Can create a relay

This commit is contained in:
2024-08-27 22:32:22 +02:00
parent f46a7dbc94
commit 87fb3360fb
6 changed files with 89 additions and 11 deletions

View File

@ -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)
}
}