Start to implement devices enrollment
This commit is contained in:
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user