add sensor values

This commit is contained in:
Romulus21
2024-12-06 23:58:13 +01:00
parent ba67b0becd
commit f12ab66d55
10 changed files with 199 additions and 5 deletions

View File

@@ -42,11 +42,12 @@ pub const OPTIONS: &[Option] = &[
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Value {
id: u16,
id: i32,
service: String,
capteur: String,
type_donnee: String,
donnee: String,
pub donnee: String,
pub date_donnee: String,
}
impl Value {
@@ -71,6 +72,46 @@ impl Value {
.execute(crate::database::get_db())
.await
}
#[cfg(feature = "ssr")]
pub async fn get_one(topic: String) -> Result<Self, sqlx::Error> {
let split: Vec<&str> = topic.split("/").collect();
let unknow_value = Value {
id: 0,
service: "".to_string(),
capteur: "".to_string(),
type_donnee: "".to_string(),
donnee: "--".to_string(),
date_donnee: "--:--".to_string(),
};
if split.len() == 3 {
let res = sqlx::query!(
"SELECT * FROM donnees WHERE service = ? AND capteur = ? AND type = ?",
split[0],
split[1],
split[2],
//chrono::Local::now().naive_local(),
)
.map(|x| Self {
id: x.id,
service: x.service,
capteur: x.capteur,
type_donnee: x.r#type,
donnee: x.donnee,
date_donnee: x.date_donnee.format("%H:%M").to_string(),
})
.fetch_one(crate::database::get_db())
.await;
match res {
Ok(res) => Ok(res),
Err(_) => Ok(unknow_value)
}
} else {
Ok(unknow_value)
}
}
}
fn find_option(value: &str) -> Result<&Option, &str> {