Display relay consumption history

This commit is contained in:
2024-09-26 22:51:43 +02:00
parent 903f1fa8ce
commit 7895b9eca8
10 changed files with 101 additions and 12 deletions

View File

@@ -53,7 +53,7 @@ impl EnergyActor {
});
self.consumption_cache.add_value(latest_consumption);
let devices_list = self.devices.full_list();
let devices_list = self.devices.full_list_ref();
let mut history =
ConsumptionHistoryFile::open(time_secs(), ConsumptionHistoryType::GridConsumption)?;
@@ -119,7 +119,21 @@ impl Handler<GetCurrConsumption> for EnergyActor {
}
}
/// Get current consumption
/// Get relays consumption
#[derive(Message)]
#[rtype(result = "usize")]
pub struct RelaysConsumption;
impl Handler<RelaysConsumption> for EnergyActor {
type Result = usize;
fn handle(&mut self, _msg: RelaysConsumption, _ctx: &mut Context<Self>) -> Self::Result {
self.engine
.sum_relays_consumption(&self.devices.full_list_ref())
}
}
/// Check if device exists
#[derive(Message)]
#[rtype(result = "bool")]
pub struct CheckDeviceExists(pub DeviceId);

View File

@@ -55,7 +55,7 @@ pub struct EnergyEngine {
impl DeviceRelay {
// Note : this function is not recursive
fn has_running_dependencies(&self, s: &RelaysState, devices: &[Device]) -> bool {
fn 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().is_on() {
@@ -72,7 +72,7 @@ impl DeviceRelay {
self.depends_on.iter().any(|id| s.get(id).unwrap().is_off())
}
fn is_having_conflict(&self, s: &RelaysState, devices: &[Device]) -> bool {
fn is_having_conflict(&self, s: &RelaysState, devices: &[&Device]) -> bool {
if self
.conflicts_with
.iter()
@@ -94,7 +94,7 @@ impl DeviceRelay {
}
}
fn sum_relays_consumption(state: &RelaysState, devices: &[Device]) -> usize {
fn sum_relays_consumption(state: &RelaysState, devices: &[&Device]) -> usize {
let mut consumption = 0;
for d in devices {
@@ -119,11 +119,11 @@ impl EnergyEngine {
self.relays_state.get_mut(&relay_id).unwrap()
}
pub fn sum_relays_consumption(&self, devices: &[Device]) -> usize {
pub fn sum_relays_consumption(&self, devices: &[&Device]) -> usize {
sum_relays_consumption(&self.relays_state, devices)
}
fn print_summary(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) {
fn print_summary(&mut self, curr_consumption: EnergyConsumption, devices: &[&Device]) {
log::info!("Current consumption: {curr_consumption}");
let mut table = Table::new();
@@ -172,13 +172,13 @@ impl EnergyEngine {
pub fn estimated_consumption_without_relays(
&self,
curr_consumption: EnergyConsumption,
devices: &[Device],
devices: &[&Device],
) -> EnergyConsumption {
curr_consumption - self.sum_relays_consumption(devices) as i32
}
/// Refresh energy engine; this method shall never fail !
pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) {
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}");
@@ -366,7 +366,7 @@ impl EnergyEngine {
}
/// Save relays state to disk
pub fn persist_relays_state(&mut self, devices: &[Device]) -> anyhow::Result<()> {
pub fn persist_relays_state(&mut self, devices: &[&Device]) -> anyhow::Result<()> {
// Save all relays state
for d in devices {
for r in &d.relays {