HomeServer/src/web_server.rs

174 lines
4.1 KiB
Rust
Raw Normal View History

2023-09-21 08:08:23 +00:00
use actix_files::NamedFile;
2023-09-21 05:30:39 +00:00
use actix_web::{
get, post,
web::{Data, Json, Path},
2023-09-21 08:08:23 +00:00
Responder, ResponseError,
2023-09-21 05:30:39 +00:00
};
use serde::Serialize;
use serde_json::to_string;
2023-09-21 08:08:23 +00:00
use crate::{db::DataBase, tasmota::Tasmota};
2023-09-21 05:30:39 +00:00
2023-09-21 08:08:23 +00:00
use std::{
fmt::{Display, Formatter, Result as FmtResult},
sync::{Arc, Mutex},
};
2023-09-21 05:30:39 +00:00
#[derive(Serialize)]
struct DeviceState {
name: String,
power: bool,
led: bool,
power_draw: f32,
}
#[derive(Debug)]
struct MyError {
msg: String,
}
impl Display for MyError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "Error: {}", self.msg)
}
}
impl ResponseError for MyError {}
impl From<anyhow::Error> for MyError {
fn from(value: anyhow::Error) -> Self {
MyError {
msg: value.to_string(),
}
}
}
#[get("/")]
2023-09-21 08:08:23 +00:00
async fn index() -> Result<NamedFile, impl ResponseError> {
NamedFile::open("resources/static/index.html")
2023-09-21 05:30:39 +00:00
}
#[get("/devices")]
2023-09-21 08:08:23 +00:00
async fn device_query(
db: Data<Arc<Mutex<DataBase>>>,
) -> Result<impl Responder, impl ResponseError> {
db.lock()
.unwrap()
2023-09-21 08:46:23 +00:00
.devices()
.map_err(|err| {
println!("{err:?}");
MyError::from(err)
})?
2023-09-21 05:30:39 +00:00
.to_json()
.map(|json| Json(json))
2023-09-21 08:46:23 +00:00
.map_err(|err| {
println!("{err:?}");
MyError::from(err)
})
2023-09-21 05:30:39 +00:00
}
2023-09-21 08:08:23 +00:00
#[post("/device_name/{device}/{name}")]
async fn change_device_name(
device: Path<String>,
name: Path<String>,
db: Data<Arc<Mutex<DataBase>>>,
) -> Result<impl Responder, MyError> {
db.lock()
.unwrap()
.change_device_name(&device.into_inner(), &name.into_inner())
.map_err(|err| MyError::from(err))?;
return Ok("Ok");
}
2023-09-21 05:30:39 +00:00
async fn tasmota_info(tasmota: &Tasmota) -> anyhow::Result<String> {
let led = tasmota.led_state().await?;
let power = tasmota.power_state().await?;
let power_draw = tasmota.read_power_usage().await?;
Ok(to_string(&DeviceState {
name: tasmota.name().to_string(),
power,
led,
power_draw,
})?)
}
#[get("/plug_state/{plug}")]
async fn plug_state(
plug: Path<String>,
plugs: Data<Vec<Tasmota>>,
) -> Result<impl Responder, impl ResponseError> {
let plug_name = plug.into_inner();
if let Some(tasmota) = plugs.iter().find(|tasmota| tasmota.name() == plug_name) {
return Ok(tasmota_info(tasmota)
.await
.map(|s| Json(s))
.map_err(|err| MyError::from(err))?);
}
Err(MyError {
msg: format!("plug ({plug_name}) not found"),
})
}
#[post("/plug/{plug}/{action}")]
async fn change_plug_state(
plug: Path<String>,
action: Path<String>,
plugs: Data<Vec<Tasmota>>,
) -> Result<impl Responder, impl ResponseError> {
let plug_name = plug.into_inner();
if let Some(tasmota) = plugs.iter().find(|tasmota| tasmota.name() == plug_name) {
match action.into_inner().as_str() {
"led_on" => tasmota
.turn_on_led()
.await
.map_err(|err| MyError::from(err))?,
"led_off" => tasmota
.turn_off_led()
.await
.map_err(|err| MyError::from(err))?,
"power_on" => tasmota
.switch_on()
.await
.map_err(|err| MyError::from(err))?,
"power_off" => tasmota
.switch_off()
.await
.map_err(|err| MyError::from(err))?,
_ => (),
}
return Ok("Ok");
}
Err(MyError {
msg: format!("plug ({plug_name}) not found"),
})
}
2023-09-21 08:08:23 +00:00
#[cfg(test)]
mod test {
use actix_web::{http::header::ContentType, test, App};
use super::*;
#[actix_web::test]
async fn test_index_get() {
let app = test::init_service(App::new().service(index)).await;
let req = test::TestRequest::default()
.insert_header(ContentType::plaintext())
.to_request();
let resp = test::call_service(&app, req).await;
let status = resp.status();
let body = resp.into_body();
assert!(status.is_success(), "{:?}", body);
}
}