Add new engine tests for forced state
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -25,6 +25,33 @@ impl DeviceState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum SetRelayForcedStateReq {
|
||||
#[default]
|
||||
None,
|
||||
Off {
|
||||
for_secs: u64,
|
||||
},
|
||||
On {
|
||||
for_secs: u64,
|
||||
},
|
||||
}
|
||||
|
||||
impl SetRelayForcedStateReq {
|
||||
pub fn to_forced_state(&self) -> RelayForcedState {
|
||||
match &self {
|
||||
SetRelayForcedStateReq::None => RelayForcedState::None,
|
||||
SetRelayForcedStateReq::Off { for_secs } => RelayForcedState::Off {
|
||||
until: time_secs() + for_secs,
|
||||
},
|
||||
SetRelayForcedStateReq::On { for_secs } => RelayForcedState::On {
|
||||
until: time_secs() + for_secs,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum RelayForcedState {
|
||||
@@ -440,7 +467,7 @@ impl EnergyEngine {
|
||||
mod test {
|
||||
use crate::devices::device::{Device, DeviceId, DeviceRelayID};
|
||||
use crate::energy::consumption::EnergyConsumption;
|
||||
use crate::energy::engine::EnergyEngine;
|
||||
use crate::energy::engine::{EnergyEngine, SetRelayForcedStateReq};
|
||||
use crate::utils::time_utils::time_secs;
|
||||
use rust_embed::Embed;
|
||||
|
||||
@@ -449,6 +476,8 @@ mod test {
|
||||
id: DeviceRelayID,
|
||||
on: bool,
|
||||
r#for: usize,
|
||||
#[serde(default)]
|
||||
forced_state: SetRelayForcedStateReq,
|
||||
should_be_on: bool,
|
||||
}
|
||||
|
||||
@@ -496,6 +525,7 @@ mod test {
|
||||
let s = engine.relay_state(r.id);
|
||||
s.on = r.on;
|
||||
s.since = time_secs() as usize - r.r#for;
|
||||
s.forced_state = r.forced_state.to_forced_state()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::devices::device::{DeviceId, DeviceRelay, DeviceRelayID};
|
||||
use crate::energy::energy_actor;
|
||||
use crate::energy::engine::RelayForcedState;
|
||||
use crate::energy::engine::SetRelayForcedStateReq;
|
||||
use crate::server::WebEnergyActor;
|
||||
use crate::server::custom_error::HttpResult;
|
||||
use crate::utils::time_utils::time_secs;
|
||||
use actix_web::{HttpResponse, web};
|
||||
|
||||
/// Get the full list of relays
|
||||
@@ -87,19 +86,6 @@ pub async fn update(
|
||||
Ok(HttpResponse::Accepted().finish())
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum SetRelayForcedStateReq {
|
||||
#[default]
|
||||
None,
|
||||
Off {
|
||||
for_secs: u64,
|
||||
},
|
||||
On {
|
||||
for_secs: u64,
|
||||
},
|
||||
}
|
||||
|
||||
/// Set relay forced status
|
||||
pub async fn set_forced_state(
|
||||
actor: WebEnergyActor,
|
||||
@@ -116,15 +102,7 @@ pub async fn set_forced_state(
|
||||
actor
|
||||
.send(energy_actor::SetRelayForcedState(
|
||||
path.id,
|
||||
match &req.0 {
|
||||
SetRelayForcedStateReq::None => RelayForcedState::None,
|
||||
SetRelayForcedStateReq::Off { for_secs } => RelayForcedState::Off {
|
||||
until: time_secs() + for_secs,
|
||||
},
|
||||
SetRelayForcedStateReq::On { for_secs } => RelayForcedState::On {
|
||||
until: time_secs() + for_secs,
|
||||
},
|
||||
},
|
||||
req.to_forced_state(),
|
||||
))
|
||||
.await??;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user