Refactor energy management

This commit is contained in:
2024-09-10 19:40:06 +02:00
parent 36ba4efd9f
commit c74ed0cfbb
5 changed files with 241 additions and 36 deletions

View File

@ -5,33 +5,15 @@ use crate::devices::device::{
use crate::devices::devices_list::DevicesList;
use crate::energy::consumption;
use crate::energy::consumption::EnergyConsumption;
use crate::energy::engine::EnergyEngine;
use crate::utils::time_utils::time_secs;
use actix::prelude::*;
use openssl::x509::X509Req;
use std::collections::HashMap;
#[derive(Default)]
struct DeviceState {
last_ping: u64,
}
impl DeviceState {
fn is_online(&self) -> bool {
(time_secs() - self.last_ping) < constants::DEVICE_MAX_PING_TIME
}
}
#[derive(Default)]
struct RelayState {
enabled: bool,
since: usize,
}
pub struct EnergyActor {
curr_consumption: EnergyConsumption,
devices: DevicesList,
devices_state: HashMap<DeviceId, DeviceState>,
relays_state: HashMap<DeviceRelayID, RelayState>,
engine: EnergyEngine,
}
impl EnergyActor {
@ -39,20 +21,10 @@ impl EnergyActor {
Ok(Self {
curr_consumption: consumption::get_curr_consumption().await?,
devices: DevicesList::load()?,
devices_state: Default::default(),
relays_state: Default::default(),
engine: EnergyEngine::default(),
})
}
fn device_state(&mut self, dev_id: &DeviceId) -> &mut DeviceState {
if !self.devices_state.contains_key(dev_id) {
self.devices_state
.insert(dev_id.clone(), Default::default());
}
self.devices_state.get_mut(dev_id).unwrap()
}
async fn refresh(&mut self) -> anyhow::Result<()> {
// Refresh energy
self.curr_consumption = consumption::get_curr_consumption()
@ -64,6 +36,9 @@ impl EnergyActor {
constants::FALLBACK_PRODUCTION_VALUE
});
self.engine
.refresh(self.curr_consumption, &self.devices.full_list());
Ok(())
}
}
@ -285,8 +260,7 @@ impl Handler<SynchronizeDevice> for EnergyActor {
type Result = anyhow::Result<Vec<RelaySyncStatus>>;
fn handle(&mut self, msg: SynchronizeDevice, _ctx: &mut Context<Self>) -> Self::Result {
let s = self.device_state(&msg.0);
s.last_ping = time_secs();
self.engine.device_state(&msg.0).record_ping();
// TODO : implement real code
let mut v = vec![];
@ -319,7 +293,7 @@ impl Handler<GetDevicesState> for EnergyActor {
.full_list()
.into_iter()
.map(|d| {
let s = self.device_state(&d.id);
let s = self.engine.device_state(&d.id);
ResDevState {
id: d.id,
last_ping: time_secs() - s.last_ping,