Query actions from server
This commit is contained in:
parent
c71e5c63ee
commit
932958657d
3 changed files with 27 additions and 3 deletions
|
@ -2,8 +2,9 @@ use core::slice::Iter;
|
||||||
use std::{fmt::Display, str::FromStr};
|
use std::{fmt::Display, str::FromStr};
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
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 {
|
pub enum ActionType {
|
||||||
GreaterThan,
|
GreaterThan,
|
||||||
LessThan,
|
LessThan,
|
||||||
|
@ -43,8 +44,9 @@ impl FromStr for ActionType {
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct ActionID(pub(crate) i64);
|
pub struct ActionID(pub(crate) i64);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
|
||||||
pub struct Action {
|
pub struct Action {
|
||||||
|
#[serde(skip)]
|
||||||
pub(crate) id: Option<ActionID>,
|
pub(crate) id: Option<ActionID>,
|
||||||
|
|
||||||
pub device_id: String,
|
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 {
|
pub struct ActionSet {
|
||||||
actions: Vec<Action>,
|
actions: Vec<Action>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,6 +99,8 @@ async fn run_web_server(
|
||||||
.service(plug_data_range)
|
.service(plug_data_range)
|
||||||
.service(push_temperature)
|
.service(push_temperature)
|
||||||
.service(push_humidity)
|
.service(push_humidity)
|
||||||
|
.service(update_push_action)
|
||||||
|
.service(actions)
|
||||||
})
|
})
|
||||||
.bind((IP, PORT))
|
.bind((IP, PORT))
|
||||||
.map_err(|err| anyhow::Error::msg(format!("failed binding to address: {err:#?}")))?
|
.map_err(|err| anyhow::Error::msg(format!("failed binding to address: {err:#?}")))?
|
||||||
|
|
|
@ -308,6 +308,26 @@ async fn update_push_action(
|
||||||
Ok("Ok")
|
Ok("Ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/actions/{device}")]
|
||||||
|
async fn actions(
|
||||||
|
param: Path<String>,
|
||||||
|
db: Data<Arc<Mutex<DataBase>>>,
|
||||||
|
) -> Result<impl Responder, Error> {
|
||||||
|
let device_name = param.into_inner();
|
||||||
|
let db_lock = db.lock().unwrap();
|
||||||
|
|
||||||
|
let action_sets: Vec<ActionSet> = 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<F>(data: Vec<(u64, f32)>, f: F) -> Vec<(u64, f32)>
|
fn collapse_data<F>(data: Vec<(u64, f32)>, f: F) -> Vec<(u64, f32)>
|
||||||
where
|
where
|
||||||
F: Fn(NaiveDateTime) -> NaiveDateTime,
|
F: Fn(NaiveDateTime) -> NaiveDateTime,
|
||||||
|
|
Loading…
Reference in a new issue