Virtually turn off all relays that can be stopped

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

View File

@ -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<DeviceRelayID, RelayState>;
#[derive(Default)]
pub struct EnergyEngine {
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 {
@ -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