Virtually turn off all relays that can be stopped

This commit is contained in:
Pierre HUBERT 2024-09-13 22:11:40 +02:00
parent 1d11c3a968
commit f0081eb4bf
2 changed files with 53 additions and 8 deletions

View File

@ -98,13 +98,13 @@ pub struct DeviceRelay {
/// Relay priority when selecting relays to turn on. 0 = lowest priority /// Relay priority when selecting relays to turn on. 0 = lowest priority
pub priority: usize, pub priority: usize,
/// Estimated consumption of the electrical equipment triggered by the relay /// 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 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 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 /// Optional minimal runtime requirements for this relay
daily_runtime: Option<DailyMinRuntime>, pub daily_runtime: Option<DailyMinRuntime>,
/// Specify relay that must be turned on before this relay can be started /// Specify relay that must be turned on before this relay can be started
pub depends_on: Vec<DeviceRelayID>, pub depends_on: Vec<DeviceRelayID>,
/// Specify relays that must be turned off before this relay can be started /// Specify relays that must be turned off before this relay can be started

View File

@ -1,5 +1,5 @@
use crate::constants; 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::energy::consumption::EnergyConsumption;
use crate::utils::time_utils::time_secs; use crate::utils::time_utils::time_secs;
use prettytable::{row, Table}; use prettytable::{row, Table};
@ -26,10 +26,26 @@ pub struct RelayState {
since: usize, since: usize,
} }
type RelaysState = HashMap<DeviceRelayID, RelayState>;
#[derive(Default)] #[derive(Default)]
pub struct EnergyEngine { pub struct EnergyEngine {
devices_state: HashMap<DeviceId, DeviceState>, devices_state: HashMap<DeviceId, DeviceState>,
relays_state: HashMap<DeviceRelayID, RelayState>, 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 { 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 // TODO Turn on relays with running constraints