Implement energy engine test logic
This commit is contained in:
parent
719b0a0c5c
commit
b7635935ba
26
central_backend/Cargo.lock
generated
26
central_backend/Cargo.lock
generated
@ -634,6 +634,7 @@ dependencies = [
|
|||||||
"semver",
|
"semver",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serde_yml",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio_schedule",
|
"tokio_schedule",
|
||||||
@ -1499,6 +1500,16 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libyml"
|
||||||
|
version = "0.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.14"
|
version = "0.4.14"
|
||||||
@ -2197,6 +2208,21 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_yml"
|
||||||
|
version = "0.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap",
|
||||||
|
"itoa",
|
||||||
|
"libyml",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha1"
|
name = "sha1"
|
||||||
version = "0.10.6"
|
version = "0.10.6"
|
||||||
|
@ -37,3 +37,4 @@ rust-embed = "8.5.0"
|
|||||||
jsonwebtoken = { version = "9.3.0", features = ["use_pem"] }
|
jsonwebtoken = { version = "9.3.0", features = ["use_pem"] }
|
||||||
prettytable-rs = "0.10.0"
|
prettytable-rs = "0.10.0"
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
|
serde_yml = "0.0.12"
|
30
central_backend/engine_test/test_basic_conf.yaml
Normal file
30
central_backend/engine_test/test_basic_conf.yaml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
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
|
||||||
|
should_be_on: true
|
||||||
|
|
||||||
|
online: true
|
||||||
|
|
||||||
|
curr_consumption: -1000
|
@ -371,3 +371,93 @@ impl EnergyEngine {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::devices::device::{Device, DeviceId, DeviceRelayID};
|
||||||
|
use crate::energy::consumption::EnergyConsumption;
|
||||||
|
use crate::energy::engine::EnergyEngine;
|
||||||
|
use crate::utils::time_utils::time_secs;
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct TestRelayState {
|
||||||
|
id: DeviceRelayID,
|
||||||
|
on: bool,
|
||||||
|
r#for: usize,
|
||||||
|
should_be_on: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct TestDeviceState {
|
||||||
|
id: DeviceId,
|
||||||
|
online: bool,
|
||||||
|
relays: Vec<TestRelayState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct TestConfig {
|
||||||
|
curr_consumption: EnergyConsumption,
|
||||||
|
devices: Vec<Device>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct TestConfigState {
|
||||||
|
devices: Vec<TestDeviceState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_test_config(
|
||||||
|
conf: &str,
|
||||||
|
) -> (
|
||||||
|
Vec<Device>,
|
||||||
|
EnergyEngine,
|
||||||
|
EnergyConsumption,
|
||||||
|
Vec<TestDeviceState>,
|
||||||
|
) {
|
||||||
|
let config: TestConfig = serde_yml::from_str(conf).unwrap();
|
||||||
|
let test_config: TestConfigState = serde_yml::from_str(conf).unwrap();
|
||||||
|
|
||||||
|
let mut engine = EnergyEngine {
|
||||||
|
devices_state: Default::default(),
|
||||||
|
relays_state: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
for d in &test_config.devices {
|
||||||
|
engine.device_state(&d.id).last_ping = match d.online {
|
||||||
|
true => time_secs() - 1,
|
||||||
|
false => 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
for r in &d.relays {
|
||||||
|
let s = engine.relay_state(r.id);
|
||||||
|
s.on = r.on;
|
||||||
|
s.since = time_secs() as usize - r.r#for;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
config.devices,
|
||||||
|
engine,
|
||||||
|
config.curr_consumption,
|
||||||
|
test_config.devices,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_test(conf: &str) {
|
||||||
|
let (devices, mut energy_engine, consumption, states) = parse_test_config(conf);
|
||||||
|
|
||||||
|
energy_engine.refresh(consumption, &devices);
|
||||||
|
|
||||||
|
for d in states {
|
||||||
|
for r in d.relays {
|
||||||
|
assert_eq!(energy_engine.relay_state(r.id).on, r.should_be_on);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic_conf() {
|
||||||
|
run_test(include_str!("../../engine_test/test_basic_conf.yaml"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : test more scenarios
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user