working proto
This commit is contained in:
81
src/models/value.rs
Normal file
81
src/models/value.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use chrono::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Option {
|
||||
pub value: &'static str,
|
||||
pub name: &'static str,
|
||||
service: &'static str,
|
||||
device: &'static str,
|
||||
r_type: &'static str,
|
||||
}
|
||||
|
||||
pub const OPTIONS: &[Option] = &[
|
||||
Option {
|
||||
value: "citerne",
|
||||
name: "Citerne",
|
||||
service: "home",
|
||||
device: "citerne",
|
||||
r_type: "height",
|
||||
},
|
||||
Option {
|
||||
value: "eau",
|
||||
name: "Eau",
|
||||
service: "home",
|
||||
device: "eau",
|
||||
r_type: "volume",
|
||||
},
|
||||
Option {
|
||||
value: "elec-hight",
|
||||
name: "Electricité heure pleine",
|
||||
service: "home",
|
||||
device: "elec",
|
||||
r_type: "pleine",
|
||||
},
|
||||
Option {
|
||||
value: "elec-low",
|
||||
name: "Electricité heure creuse",
|
||||
service: "home",
|
||||
device: "elec",
|
||||
r_type: "creuse",
|
||||
},
|
||||
];
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Value {
|
||||
id: u16,
|
||||
service: String,
|
||||
capteur: String,
|
||||
type_donnee: String,
|
||||
donnee: String,
|
||||
}
|
||||
|
||||
impl Value {
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn insert(
|
||||
device: String,
|
||||
value: String,
|
||||
) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error> {
|
||||
let option = match find_option(&device) {
|
||||
Ok(option) => option,
|
||||
Err(err) => panic!("{}", err),
|
||||
};
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO data(service, device, type, data, date_data) VALUES (?, ?, ?, ?, NOW())",
|
||||
option.service,
|
||||
option.device,
|
||||
option.r_type,
|
||||
value,
|
||||
)
|
||||
.execute(crate::database::get_db())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
fn find_option(value: &str) -> Result<&Option, &str> {
|
||||
OPTIONS
|
||||
.iter()
|
||||
.find(|&option| option.value == value)
|
||||
.ok_or("No option found with the given value")
|
||||
}
|
||||
Reference in New Issue
Block a user