Start to implement devices enrollment

This commit is contained in:
2024-07-01 21:10:45 +02:00
parent 378c296e71
commit 9ba4aa5194
21 changed files with 267 additions and 16 deletions

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