Added TpLink Tapo power usage data GET route
This commit is contained in:
parent
05915aae30
commit
fb9f39ce21
7 changed files with 1365 additions and 44 deletions
1307
Cargo.lock
generated
1307
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -26,6 +26,7 @@ tokio = { version = "1.50.0", features = [
|
|||
"rt-multi-thread",
|
||||
"process",
|
||||
] }
|
||||
tapo = "0.9.0"
|
||||
zbus = "5.14.0"
|
||||
webauthn-rs = "0.5"
|
||||
url = "2"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
/// (label, ip) pairs for Tapo P115 plugs. Credentials are read from
|
||||
/// TAPO_USERNAME and TAPO_PASSWORD environment variables at runtime.
|
||||
pub const TAPO_DEVICES: &[(&str, &str)] = &[
|
||||
("server", "192.168.1.64"),
|
||||
("desktop", "192.168.1.85"),
|
||||
];
|
||||
|
||||
pub const ALLOWED_SERVICES: &[&str] = &[
|
||||
"syncthing",
|
||||
"caddy",
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ async fn main() {
|
|||
|
||||
let protected = Router::new()
|
||||
.route("/stats", get(routes::stats::get_stats))
|
||||
.route("/power", get(routes::power::get_power))
|
||||
.route(
|
||||
"/services/{service}/restart",
|
||||
post(routes::services::restart_service),
|
||||
|
|
|
|||
|
|
@ -92,3 +92,23 @@ pub struct LoadAvgStats {
|
|||
pub five: f64,
|
||||
pub fifteen: f64,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TapoDeviceData {
|
||||
pub name: String,
|
||||
pub ip: String,
|
||||
pub alias: String,
|
||||
pub model: String,
|
||||
pub on: bool,
|
||||
pub current_power_w: f64,
|
||||
pub today_energy_wh: u64,
|
||||
pub month_energy_wh: u64,
|
||||
pub today_runtime_min: u64,
|
||||
pub month_runtime_min: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TapoPowerResponse {
|
||||
pub timestamp: String,
|
||||
pub devices: Vec<TapoDeviceData>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod power;
|
||||
pub mod services;
|
||||
pub mod stats;
|
||||
pub mod system;
|
||||
|
|
|
|||
72
src/routes/power.rs
Normal file
72
src/routes/power.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Json};
|
||||
use tapo::ApiClient;
|
||||
|
||||
use crate::models;
|
||||
|
||||
async fn query_device(
|
||||
username: &str,
|
||||
password: &str,
|
||||
name: &str,
|
||||
ip: &str,
|
||||
) -> Result<models::TapoDeviceData, String> {
|
||||
let device = ApiClient::new(username, password)
|
||||
.p110(ip)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let info = device.get_device_info().await.map_err(|e| e.to_string())?;
|
||||
let energy = device.get_energy_usage().await.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(models::TapoDeviceData {
|
||||
name: name.to_string(),
|
||||
ip: ip.to_string(),
|
||||
alias: info.nickname,
|
||||
model: info.model,
|
||||
on: info.device_on,
|
||||
current_power_w: energy.current_power.unwrap_or(0) as f64 / 1000.0,
|
||||
today_energy_wh: energy.today_energy,
|
||||
month_energy_wh: energy.month_energy,
|
||||
today_runtime_min: energy.today_runtime,
|
||||
month_runtime_min: energy.month_runtime,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_power() -> impl IntoResponse {
|
||||
let username = std::env::var("TAPO_USERNAME").unwrap_or_default();
|
||||
let password = std::env::var("TAPO_PASSWORD").unwrap_or_default();
|
||||
|
||||
if username.is_empty() || password.is_empty() {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"TAPO_USERNAME and TAPO_PASSWORD must be set",
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let tasks: Vec<_> = crate::config::TAPO_DEVICES
|
||||
.iter()
|
||||
.map(|(name, ip)| {
|
||||
let username = username.clone();
|
||||
let password = password.clone();
|
||||
let name = name.to_string();
|
||||
let ip = ip.to_string();
|
||||
tokio::spawn(async move { query_device(&username, &password, &name, &ip).await })
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut devices = Vec::new();
|
||||
for task in tasks {
|
||||
match task.await {
|
||||
Ok(Ok(data)) => devices.push(data),
|
||||
Ok(Err(e)) => eprintln!("Tapo query error: {e}"),
|
||||
Err(e) => eprintln!("Tapo task panic: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
Json(models::TapoPowerResponse {
|
||||
timestamp: chrono::Utc::now().to_rfc3339(),
|
||||
devices,
|
||||
})
|
||||
.into_response()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue