Can update device general information

This commit is contained in:
2024-07-22 22:19:48 +02:00
parent baf341d505
commit 4d5ba939d1
18 changed files with 546 additions and 147 deletions

@@ -1,5 +1,7 @@
//! # Devices entities definition
use crate::constants::StaticConstraints;
/// Device information provided directly by the device during syncrhonisation.
///
/// It should not be editable fro the Web UI
@@ -100,3 +102,29 @@ pub struct DeviceRelay {
/// Specify relays that must be turned off before this relay can be started
conflicts_with: Vec<DeviceRelayID>,
}
/// Device general information
///
/// This structure is used to update device information
#[derive(serde::Deserialize, Debug, Clone)]
pub struct DeviceGeneralInfo {
pub name: String,
pub description: String,
pub enabled: bool,
}
impl DeviceGeneralInfo {
/// Check for errors in the structure
pub fn error(&self) -> Option<&'static str> {
let constraints = StaticConstraints::default();
if !constraints.dev_name_len.validate(&self.name) {
return Some("Invalid device name length!");
}
if !constraints.dev_description_len.validate(&self.description) {
return Some("Invalid device description length!");
}
None
}
}

@@ -1,6 +1,6 @@
use crate::app_config::AppConfig;
use crate::crypto::pki;
use crate::devices::device::{Device, DeviceId, DeviceInfo};
use crate::devices::device::{Device, DeviceGeneralInfo, DeviceId, DeviceInfo};
use crate::utils::time_utils::time_secs;
use openssl::x509::{X509Req, X509};
use std::collections::HashMap;
@@ -15,6 +15,8 @@ pub enum DevicesListError {
ValidateDeviceFailedDeviceNotFound,
#[error("Validated device failed: the device is already validated!")]
ValidateDeviceFailedDeviceAlreadyValidated,
#[error("Update device failed: the device does not exists!")]
UpdateDeviceFailedDeviceNotFound,
#[error("Requested device was not found")]
DeviceNotFound,
#[error("Requested device is not validated")]
@@ -133,6 +135,27 @@ impl DevicesList {
Ok(())
}
/// Update a device general information
pub fn update_general_info(
&mut self,
id: &DeviceId,
general_info: DeviceGeneralInfo,
) -> anyhow::Result<()> {
let dev = self
.0
.get_mut(id)
.ok_or(DevicesListError::UpdateDeviceFailedDeviceNotFound)?;
dev.name = general_info.name;
dev.description = general_info.description;
dev.enabled = general_info.enabled;
dev.time_update = time_secs();
self.persist_dev_config(id)?;
Ok(())
}
/// Get single certificate information
fn get_cert(&self, id: &DeviceId) -> anyhow::Result<X509> {
let dev = self