WIP energy engine update

This commit is contained in:
Pierre HUBERT 2024-09-12 21:45:58 +02:00
parent c1c01058d8
commit 1d11c3a968

View File

@ -58,13 +58,52 @@ impl EnergyEngine {
} }
pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) { pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) {
// TODO : force creation of missing relays state // Force creation of missing relays state
for d in devices {
for r in &d.relays {
// Requesting relay state is enough to trigger relay creation
self.relay_state(r.id);
}
}
let new_relays_state = self.relays_state.clone(); let mut new_relays_state = self.relays_state.clone();
// TODO Forcefully turn off relays that belongs to offline devices // Forcefully turn off relays that belongs to offline devices
for d in devices {
if !self.device_state(&d.id).is_online() {
for r in &d.relays {
new_relays_state.get_mut(&r.id).unwrap().on = false;
}
}
}
// TODO Forcefully turn off relays with dependency conflicts // Forcefully turn off relays with missing dependency
loop {
let mut changed = false;
for d in devices {
if !self.device_state(&d.id).is_online() {
for r in &d.relays {
if !new_relays_state.get(&r.id).unwrap().on {
continue;
}
// Check if any dependency of relay is off
if r.depends_on
.iter()
.any(|d| !new_relays_state.get(d).unwrap().on)
{
new_relays_state.get_mut(&r.id).unwrap().on = false;
changed = true;
}
}
}
}
if !changed {
break;
}
}
// TODO Virtually turn off all relays that can be stopped // TODO Virtually turn off all relays that can be stopped