Add support for legacy relays API
This commit is contained in:
@ -132,12 +132,14 @@ impl ConsumptionHistoryFile {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::app_config::ConsumptionHistoryType;
|
||||
use crate::energy::consumption::EnergyConsumption;
|
||||
use crate::energy::consumption_history_file::{ConsumptionHistoryFile, TIME_INTERVAL};
|
||||
|
||||
#[test]
|
||||
fn test_consumption_history() {
|
||||
let mut history = ConsumptionHistoryFile::new_memory(0);
|
||||
let mut history =
|
||||
ConsumptionHistoryFile::new_memory(0, ConsumptionHistoryType::GridConsumption);
|
||||
|
||||
for i in 0..50 {
|
||||
assert_eq!(
|
||||
|
@ -399,8 +399,8 @@ impl Handler<GetDevicesState> for EnergyActor {
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct ResRelayState {
|
||||
pub id: DeviceRelayID,
|
||||
on: bool,
|
||||
r#for: usize,
|
||||
pub on: bool,
|
||||
pub r#for: usize,
|
||||
}
|
||||
|
||||
/// Get the state of all relays
|
||||
|
@ -8,7 +8,7 @@ use crate::devices::device::{Device, DeviceId, DeviceRelay, DeviceRelayID};
|
||||
use crate::energy::consumption::EnergyConsumption;
|
||||
use crate::energy::relay_state_history;
|
||||
use crate::energy::relay_state_history::RelayStateHistory;
|
||||
use crate::utils::time_utils::{curr_hour, time_secs, time_start_of_day};
|
||||
use crate::utils::time_utils::{curr_hour, time_secs};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DeviceState {
|
||||
@ -283,12 +283,7 @@ impl EnergyEngine {
|
||||
continue;
|
||||
}
|
||||
|
||||
let time_start_day = time_start_of_day().unwrap_or(1726696800);
|
||||
let start_time = time_start_day + constraints.reset_time as u64;
|
||||
let end_time = time_start_day + 3600 * 24 + constraints.reset_time as u64;
|
||||
let total_runtime =
|
||||
relay_state_history::relay_total_runtime(r.id, start_time, end_time)
|
||||
.unwrap_or(3600 * 24);
|
||||
let total_runtime = relay_state_history::relay_total_runtime_adjusted(r);
|
||||
|
||||
if total_runtime > constraints.min_runtime {
|
||||
continue;
|
||||
@ -454,7 +449,7 @@ mod test {
|
||||
fn run_test(name: &str, conf: &str) {
|
||||
let (devices, mut energy_engine, consumption, states) = parse_test_config(conf);
|
||||
|
||||
energy_engine.refresh(consumption, &devices);
|
||||
energy_engine.refresh(consumption, &devices.iter().collect::<Vec<_>>());
|
||||
|
||||
for (device_s, device) in states.iter().zip(&devices) {
|
||||
for (relay_s, relay) in device_s.relays.iter().zip(&device.relays) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::devices::device::DeviceRelayID;
|
||||
use crate::devices::device::{DeviceRelay, DeviceRelayID};
|
||||
use crate::utils::files_utils;
|
||||
use crate::utils::time_utils::day_number;
|
||||
use crate::utils::time_utils::{day_number, time_start_of_day};
|
||||
|
||||
const TIME_INTERVAL: usize = 30;
|
||||
|
||||
@ -119,6 +119,20 @@ pub fn relay_total_runtime(device_id: DeviceRelayID, from: u64, to: u64) -> anyh
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
/// Get the total runtime of a relay taking account of daily reset time
|
||||
pub fn relay_total_runtime_adjusted(relay: &DeviceRelay) -> usize {
|
||||
let reset_time = relay
|
||||
.daily_runtime
|
||||
.as_ref()
|
||||
.map(|r| r.reset_time)
|
||||
.unwrap_or(0);
|
||||
|
||||
let time_start_day = time_start_of_day().unwrap_or(1726696800);
|
||||
let start_time = time_start_day + reset_time as u64;
|
||||
let end_time = time_start_day + 3600 * 24 + reset_time as u64;
|
||||
relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::devices::device::DeviceRelayID;
|
||||
|
Reference in New Issue
Block a user