Add devices definitions

This commit is contained in:
Pierre HUBERT 2024-07-17 18:57:23 +02:00
parent 37406faa32
commit 370084b3bb

View File

@ -1,11 +1,20 @@
//! # Devices entities definition
/// Device information provided directly by the device during syncrhonisation.
///
/// It should not be editable fro the Web UI
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct DeviceInfo { pub struct DeviceInfo {
/// Device reference
reference: String, reference: String,
/// Device firmware / software version
version: semver::Version, version: semver::Version,
/// Maximum number of relay that the device can support
max_relays: usize, max_relays: usize,
} }
impl DeviceInfo { impl DeviceInfo {
/// Identify errors in device information definition
pub fn error(&self) -> Option<&str> { pub fn error(&self) -> Option<&str> {
if self.reference.trim().is_empty() { if self.reference.trim().is_empty() {
return Some("Given device reference is empty or blank!"); return Some("Given device reference is empty or blank!");
@ -19,14 +28,19 @@ impl DeviceInfo {
} }
} }
/// Device identifier
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)]
pub struct DeviceId(pub String); pub struct DeviceId(pub String);
/// Single device information
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Device { pub struct Device {
/// The device ID /// The device ID
pub id: DeviceId, pub id: DeviceId,
/// Information about the device /// Information about the device
///
/// These information shall not be editable from the webui. They are automatically updated during
/// device synchronization
pub info: DeviceInfo, pub info: DeviceInfo,
/// Time at which device was initially enrolled /// Time at which device was initially enrolled
pub time_create: u64, pub time_create: u64,
@ -42,6 +56,8 @@ pub struct Device {
/// Specify whether the device is enabled or not /// Specify whether the device is enabled or not
pub enabled: bool, pub enabled: bool,
/// Information about the relays handled by the device /// Information about the relays handled by the device
///
/// There cannot be more than [info.max_relays] relays
pub relays: Vec<DeviceRelay>, pub relays: Vec<DeviceRelay>,
} }
@ -49,24 +65,38 @@ pub struct Device {
/// time of a device /// time of a device
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct DailyMinRuntime { pub struct DailyMinRuntime {
/// Minimum time, in seconds, that this relay should run
pub min_runtime: usize, pub min_runtime: usize,
/// The seconds in the days (from 00:00) where the counter is reset
pub reset_time: usize, pub reset_time: usize,
/// The hours during which the relay should be turned on to reach expected runtime
pub catch_up_hours: Vec<usize>, pub catch_up_hours: Vec<usize>,
} }
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)] #[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
pub struct DeviceRelayID(uuid::Uuid); pub struct DeviceRelayID(uuid::Uuid);
/// Single device relay information
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct DeviceRelay { pub struct DeviceRelay {
/// Device relay id. Should be unique across the whole application
id: DeviceRelayID, id: DeviceRelayID,
/// Human-readable name for the relay
name: String, name: String,
/// Whether this relay can be turned on or not
enabled: bool, enabled: bool,
/// Relay priority when selecting relays to turn of / on. 0 = lowest priority
priority: usize, priority: usize,
/// Estimated consumption of the electrical equipment triggered by the relay
consumption: usize, consumption: usize,
/// Minimal time this relay shall be left on before it can be turned off
minimal_uptime: usize, minimal_uptime: usize,
/// Minimal time this relay shall be left off before it can be turned on again
minimal_downtime: usize, minimal_downtime: usize,
/// Optional minimal runtime requirements for this relay
daily_runtime: Option<DailyMinRuntime>, daily_runtime: Option<DailyMinRuntime>,
depends_on: Vec<DeviceRelay>, /// Specify relay that must be turned on before this relay can be started
conflicts_with: Vec<DeviceRelay>, depends_on: Vec<DeviceRelayID>,
/// Specify relays that must be turned off before this relay can be started
conflicts_with: Vec<DeviceRelayID>,
} }