make SET stub + move elements into system wrapper

This commit is contained in:
sigil-03 2025-04-06 14:34:15 -06:00
parent fd120be85e
commit 987150c9b6
6 changed files with 108 additions and 65 deletions

View file

@ -1,68 +1,9 @@
use crate::tasmota::{PowerStatusData, StatusResponse, TasmotaInterface, TasmotaInterfaceConfig};
use crate::types::Error;
use reqwest::Client;
use serde::Deserialize;
use std::fs;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("io error")]
IoError(#[from] std::io::Error),
#[error("toml parsing error")]
ParseError(#[from] toml::de::Error),
#[error("request error")]
RequestError(#[from] reqwest::Error),
#[error("JSON Parse error")]
JsonParseError(#[from] serde_json::Error),
}
pub trait Monitoring {
async fn get_power(&self) -> Result<isize, Error>;
}
#[derive(Deserialize)]
pub struct MonitorConfig {
targets: Vec<TasmotaInterfaceConfig>,
}
impl MonitorConfig {
fn print(&self) {
for t in &self.targets {
t.print();
}
}
}
pub struct Monitor {
targets: Vec<TasmotaInterface>,
client: Client,
}
impl Monitor {
pub fn new_from_file(config_file: &str) -> Result<Self, Error> {
let config_str = fs::read_to_string(config_file)?;
let config: MonitorConfig = toml::from_str(&config_str)?;
Ok(Self {
targets: Monitor::load_targets(&config.targets),
client: Client::new(),
})
}
pub fn load_targets(targets: &Vec<TasmotaInterfaceConfig>) -> Vec<TasmotaInterface> {
let mut v = Vec::new();
for target in targets {
v.push(TasmotaInterface::new(target.clone()));
}
v
}
pub async fn get_power(&self) -> Result<(), Error> {
for target in &self.targets {
if let Ok(res) = target.get_power().await {
target.print();
println!("* POWER: {}W", res);
println!("------------------")
}
}
Ok(())
}
}