From f0081eb4bf9a608796b990b3759b531d0893f104 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 13 Sep 2024 22:11:40 +0200 Subject: [PATCH] Virtually turn off all relays that can be stopped --- central_backend/src/devices/device.rs | 8 ++-- central_backend/src/energy/engine.rs | 53 +++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/central_backend/src/devices/device.rs b/central_backend/src/devices/device.rs index cbae2ea..4cd4be5 100644 --- a/central_backend/src/devices/device.rs +++ b/central_backend/src/devices/device.rs @@ -98,13 +98,13 @@ pub struct DeviceRelay { /// Relay priority when selecting relays to turn on. 0 = lowest priority pub priority: usize, /// Estimated consumption of the electrical equipment triggered by the relay - consumption: usize, + pub consumption: usize, /// Minimal time this relay shall be left on before it can be turned off (in seconds) - minimal_uptime: usize, + pub minimal_uptime: usize, /// Minimal time this relay shall be left off before it can be turned on again (in seconds) - minimal_downtime: usize, + pub minimal_downtime: usize, /// Optional minimal runtime requirements for this relay - daily_runtime: Option, + pub daily_runtime: Option, /// Specify relay that must be turned on before this relay can be started pub depends_on: Vec, /// Specify relays that must be turned off before this relay can be started diff --git a/central_backend/src/energy/engine.rs b/central_backend/src/energy/engine.rs index 6e3e3f8..85ad257 100644 --- a/central_backend/src/energy/engine.rs +++ b/central_backend/src/energy/engine.rs @@ -1,5 +1,5 @@ use crate::constants; -use crate::devices::device::{Device, DeviceId, DeviceRelayID}; +use crate::devices::device::{Device, DeviceId, DeviceRelay, DeviceRelayID}; use crate::energy::consumption::EnergyConsumption; use crate::utils::time_utils::time_secs; use prettytable::{row, Table}; @@ -26,10 +26,26 @@ pub struct RelayState { since: usize, } +type RelaysState = HashMap; + #[derive(Default)] pub struct EnergyEngine { devices_state: HashMap, - relays_state: HashMap, + relays_state: RelaysState, +} + +impl DeviceRelay { + fn relay_has_running_dependencies(&self, s: &RelaysState, devices: &[Device]) -> bool { + for d in devices { + for r in &d.relays { + if r.depends_on.contains(&self.id) && s.get(&r.id).unwrap().on { + return true; + } + } + } + + false + } } impl EnergyEngine { @@ -105,9 +121,38 @@ impl EnergyEngine { } } - // TODO Virtually turn off all relays that can be stopped + // Virtually turn off all relays that can be stopped + loop { + let mut changed = false; - // TODO Turn on relays based on priority / dependencies + for d in devices { + for r in &d.relays { + let state = new_relays_state.get(&r.id).unwrap(); + if !state.on { + continue; + } + + // Check if minimal runtime has not been reached + if (state.since + r.minimal_uptime) as i64 > time_secs() as i64 { + continue; + } + + // Check that no relay that depends on this relay are turned on + if r.relay_has_running_dependencies(&new_relays_state, devices) { + continue; + } + + new_relays_state.get_mut(&r.id).unwrap().on = false; + changed = true; + } + } + + if !changed { + break; + } + } + + // TODO Turn on relays based on priority / dependencies / enabled / min downtime // TODO Turn on relays with running constraints