Take relays consumption in account
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::app_config::AppConfig;
|
||||
use prettytable::{row, Table};
|
||||
|
||||
use crate::constants;
|
||||
@ -87,6 +88,20 @@ impl DeviceRelay {
|
||||
}
|
||||
}
|
||||
|
||||
fn sum_relays_consumption(state: &RelaysState, devices: &[Device]) -> usize {
|
||||
let mut consumption = 0;
|
||||
|
||||
for d in devices {
|
||||
for r in &d.relays {
|
||||
if matches!(state.get(&r.id).map(|r| r.on), Some(true)) {
|
||||
consumption += r.consumption;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
consumption
|
||||
}
|
||||
|
||||
impl EnergyEngine {
|
||||
pub fn device_state(&mut self, dev_id: &DeviceId) -> &mut DeviceState {
|
||||
self.devices_state.entry(dev_id.clone()).or_default();
|
||||
@ -102,22 +117,60 @@ impl EnergyEngine {
|
||||
log::info!("Current consumption: {curr_consumption}");
|
||||
|
||||
let mut table = Table::new();
|
||||
table.add_row(row!["Device", "Relay", "On", "Since"]);
|
||||
table.add_row(row![
|
||||
"Device",
|
||||
"Relay",
|
||||
"Consumption",
|
||||
"Min downtime / uptime",
|
||||
"On",
|
||||
"Since",
|
||||
"Online",
|
||||
"Enabled device / relay"
|
||||
]);
|
||||
for d in devices {
|
||||
let dev_online = self.device_state(&d.id).is_online();
|
||||
for r in &d.relays {
|
||||
let status = self.relay_state(r.id);
|
||||
table.add_row(row![
|
||||
d.name,
|
||||
r.name,
|
||||
r.consumption,
|
||||
format!("{} / {}", r.minimal_downtime, r.minimal_uptime),
|
||||
status.is_on().to_string(),
|
||||
status.since
|
||||
status.since,
|
||||
match dev_online {
|
||||
true => "Online",
|
||||
false => "Offline",
|
||||
},
|
||||
format!(
|
||||
"{} / {}",
|
||||
match d.enabled {
|
||||
true => "Enabled",
|
||||
false => "Disabled",
|
||||
},
|
||||
match r.enabled {
|
||||
true => "Enabled",
|
||||
false => "Disabled",
|
||||
}
|
||||
)
|
||||
]);
|
||||
}
|
||||
}
|
||||
table.printstd();
|
||||
}
|
||||
|
||||
pub fn estimated_consumption_without_relays(
|
||||
&self,
|
||||
curr_consumption: EnergyConsumption,
|
||||
devices: &[Device],
|
||||
) -> EnergyConsumption {
|
||||
curr_consumption - sum_relays_consumption(&self.relays_state, devices) as i32
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) {
|
||||
let base_production = self.estimated_consumption_without_relays(curr_consumption, devices);
|
||||
log::info!("Estimated base production: {base_production}");
|
||||
|
||||
// Force creation of missing relays state
|
||||
for d in devices {
|
||||
for r in &d.relays {
|
||||
@ -238,7 +291,13 @@ impl EnergyEngine {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO : check consumption
|
||||
let new_consumption = base_production
|
||||
+ sum_relays_consumption(&new_relays_state, devices) as EnergyConsumption;
|
||||
|
||||
if new_consumption + relay.consumption as i32 > AppConfig::get().production_margin {
|
||||
continue;
|
||||
}
|
||||
|
||||
log::info!("Turn on relay {}", relay.name);
|
||||
new_relays_state.get_mut(&relay.id).unwrap().on = true;
|
||||
changed = true;
|
||||
|
Reference in New Issue
Block a user