diff --git a/src/action.rs b/src/action.rs index eb91b98..394b4e1 100644 --- a/src/action.rs +++ b/src/action.rs @@ -2,8 +2,9 @@ use core::slice::Iter; use std::{fmt::Display, str::FromStr}; use anyhow::{bail, Result}; +use serde::Serialize; -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)] pub enum ActionType { GreaterThan, LessThan, @@ -43,8 +44,9 @@ impl FromStr for ActionType { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct ActionID(pub(crate) i64); -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)] pub struct Action { + #[serde(skip)] pub(crate) id: Option, pub device_id: String, @@ -68,7 +70,7 @@ impl Action { } } -#[derive(Debug, Default, Clone, PartialEq, Eq)] +#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)] pub struct ActionSet { actions: Vec, } diff --git a/src/main.rs b/src/main.rs index 37aa32f..aac6988 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,8 @@ async fn run_web_server( .service(plug_data_range) .service(push_temperature) .service(push_humidity) + .service(update_push_action) + .service(actions) }) .bind((IP, PORT)) .map_err(|err| anyhow::Error::msg(format!("failed binding to address: {err:#?}")))? diff --git a/src/web_server.rs b/src/web_server.rs index e3a741b..2993f6e 100644 --- a/src/web_server.rs +++ b/src/web_server.rs @@ -308,6 +308,26 @@ async fn update_push_action( Ok("Ok") } +#[get("/actions/{device}")] +async fn actions( + param: Path, + db: Data>>, +) -> Result { + let device_name = param.into_inner(); + let db_lock = db.lock().unwrap(); + + let action_sets: Vec = db_lock + .action_sets(&device_name) + .map_err(|err| MyError::from(err))? + .into_iter() + .filter(|action_set| action_set.begins_with_device(&device_name)) + .collect(); + + Ok(Json( + to_string(&action_sets).map_err(|err| MyError::from(anyhow::Error::from(err)))?, + )) +} + fn collapse_data(data: Vec<(u64, f32)>, f: F) -> Vec<(u64, f32)> where F: Fn(NaiveDateTime) -> NaiveDateTime,