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:
33
central_backend/engine_test/test_turn_forced_off.yaml
Normal file
33
central_backend/engine_test/test_turn_forced_off.yaml
Normal file
@@ -0,0 +1,33 @@
|
||||
devices:
|
||||
- id: dev1
|
||||
info:
|
||||
reference: A
|
||||
version: 0.0.1
|
||||
max_relays: 1
|
||||
time_create: 1
|
||||
time_update: 1
|
||||
name: Dev1
|
||||
description: Day1
|
||||
validated: true
|
||||
enabled: true
|
||||
relays:
|
||||
- id: dcb3fd91-bf9b-4de3-99e5-92c1c7dd72e9
|
||||
name: R1
|
||||
enabled: true
|
||||
priority: 1
|
||||
consumption: 100
|
||||
minimal_uptime: 10
|
||||
minimal_downtime: 10
|
||||
depends_on: []
|
||||
conflicts_with: []
|
||||
|
||||
on: false
|
||||
for: 5000
|
||||
forced_state:
|
||||
type: Off
|
||||
for_secs: 500
|
||||
should_be_on: false
|
||||
|
||||
online: true
|
||||
|
||||
curr_consumption: -10000
|
||||
49
central_backend/engine_test/test_turn_forced_on.yaml
Normal file
49
central_backend/engine_test/test_turn_forced_on.yaml
Normal file
@@ -0,0 +1,49 @@
|
||||
devices:
|
||||
- id: dev1
|
||||
info:
|
||||
reference: A
|
||||
version: 0.0.1
|
||||
max_relays: 1
|
||||
time_create: 1
|
||||
time_update: 1
|
||||
name: Dev1
|
||||
description: Day1
|
||||
validated: true
|
||||
enabled: true
|
||||
relays:
|
||||
- id: dcb3fd91-bf9b-4de3-99e5-92c1c7dd72e9
|
||||
name: R1
|
||||
enabled: true
|
||||
priority: 1
|
||||
consumption: 100
|
||||
minimal_uptime: 10
|
||||
minimal_downtime: 10
|
||||
depends_on: []
|
||||
conflicts_with: []
|
||||
|
||||
on: false
|
||||
for: 500
|
||||
forced_state:
|
||||
type: On
|
||||
for_secs: 500
|
||||
should_be_on: true
|
||||
|
||||
- id: dcb3fd91-bf9b-4de3-99e5-92c1c7dd72f0
|
||||
name: R2
|
||||
enabled: true
|
||||
priority: 1
|
||||
consumption: 100
|
||||
minimal_uptime: 10
|
||||
minimal_downtime: 10
|
||||
depends_on: [ ]
|
||||
conflicts_with: [ ]
|
||||
|
||||
on: false
|
||||
for: 500
|
||||
forced_state:
|
||||
type: None
|
||||
should_be_on: false
|
||||
|
||||
online: true
|
||||
|
||||
curr_consumption: 10000
|
||||
@@ -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