Store relay consumption values
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::app_config::{AppConfig, ConsumptionHistoryType};
|
||||
use crate::energy::consumption::EnergyConsumption;
|
||||
use crate::utils::time_utils::day_number;
|
||||
|
||||
@ -16,13 +16,14 @@ pub enum ConsumptionHistoryError {
|
||||
pub struct ConsumptionHistoryFile {
|
||||
day: u64,
|
||||
buff: Vec<EnergyConsumption>,
|
||||
r#type: ConsumptionHistoryType,
|
||||
}
|
||||
|
||||
impl ConsumptionHistoryFile {
|
||||
/// Open consumption history file, if it exists, or create an empty one
|
||||
pub fn open(time: u64) -> anyhow::Result<Self> {
|
||||
pub fn open(time: u64, r#type: ConsumptionHistoryType) -> anyhow::Result<Self> {
|
||||
let day = day_number(time);
|
||||
let path = AppConfig::get().energy_consumption_history_day(day);
|
||||
let path = AppConfig::get().energy_consumption_history_day(day, r#type);
|
||||
|
||||
if path.exists() {
|
||||
Ok(Self {
|
||||
@ -32,20 +33,22 @@ impl ConsumptionHistoryFile {
|
||||
bincode::config::standard(),
|
||||
)?
|
||||
.0,
|
||||
r#type,
|
||||
})
|
||||
} else {
|
||||
log::debug!(
|
||||
"Energy consumption stats for day {day} does not exists yet, creating memory buffer"
|
||||
);
|
||||
Ok(Self::new_memory(day))
|
||||
Ok(Self::new_memory(day, r#type))
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new in memory consumption history
|
||||
fn new_memory(day: u64) -> Self {
|
||||
fn new_memory(day: u64, r#type: ConsumptionHistoryType) -> Self {
|
||||
Self {
|
||||
day,
|
||||
buff: vec![0; (3600 * 24 / TIME_INTERVAL) + 1],
|
||||
r#type,
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,7 +90,7 @@ impl ConsumptionHistoryFile {
|
||||
|
||||
/// Persist device relay state history
|
||||
pub fn save(&self) -> anyhow::Result<()> {
|
||||
let path = AppConfig::get().energy_consumption_history_day(self.day);
|
||||
let path = AppConfig::get().energy_consumption_history_day(self.day, self.r#type);
|
||||
std::fs::write(
|
||||
path,
|
||||
bincode::encode_to_vec(&self.buff, bincode::config::standard())?,
|
||||
|
Reference in New Issue
Block a user