make requests to tasmota outlets!

This commit is contained in:
sigil-03 2025-04-03 21:58:20 -06:00
parent c6a6ed2ca1
commit 3c4f5b1c4e
7 changed files with 144 additions and 16 deletions

View file

@ -2,6 +2,7 @@ use clap::{Parser, Subcommand};
use monitor::Monitor;
mod monitor;
mod tasmota;
#[derive(Subcommand)]
pub enum Commands {
@ -9,14 +10,17 @@ pub enum Commands {
}
impl Commands {
pub fn execute(self, config_file: &str) {
match self {
pub async fn execute(self, config_file: &str) {
let handle = match self {
Self::Monitor => {
let _m = Monitor::new_from_file(config_file).unwrap();
println!("[TODO] Power: ----W")
let m = Monitor::new_from_file(config_file).unwrap();
tokio::spawn(async move {
m.get_power().await.unwrap();
})
// println!("[TODO] Power: ----W")
}
}
};
handle.await.unwrap();
}
}
@ -29,12 +33,13 @@ pub struct Cli {
}
impl Cli {
pub fn execute(self) {
self.command.execute(&self.config_file);
pub async fn execute(self) {
self.command.execute(&self.config_file).await;
}
}
fn main() {
#[tokio::main]
async fn main() {
let cli = Cli::parse();
cli.execute();
cli.execute().await;
}

View file

@ -1,3 +1,5 @@
use crate::tasmota::{PowerStatusData, StatusResponse};
use reqwest::Client;
use serde::Deserialize;
use std::fs;
use thiserror::Error;
@ -8,6 +10,10 @@ pub enum 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),
}
#[derive(Deserialize)]
@ -22,13 +28,35 @@ impl MonitorConfig {
}
}
pub struct Monitor {}
pub struct Monitor {
config: MonitorConfig,
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)?;
config.print();
Ok(Self {})
// config.print();
Ok(Self {
config,
client: Client::new(),
})
}
pub async fn get_power(&self) -> Result<(), Error> {
let ip = &self.config.target[0];
let res = self
.client
.get(format!("http://{ip}/cm?cmnd=Status%208"))
.send()
.await?
.text()
.await?;
// println!("body = {res:?}");
let data: StatusResponse = serde_json::from_str(&res)?;
println!("POWER: {}W", data.status.energy.power);
Ok(())
}
}

21
src/tasmota.rs Normal file
View file

@ -0,0 +1,21 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct EnergyData {
#[serde(rename = "Power")]
pub power: isize,
}
#[derive(Deserialize)]
pub struct PowerStatusData {
// #[serde(rename = "Time")]
// pub time: String,
#[serde(rename = "ENERGY")]
pub energy: EnergyData,
}
#[derive(Deserialize)]
pub struct StatusResponse {
#[serde(rename = "StatusSNS")]
pub status: PowerStatusData,
}