raspberry_web/
models.rs

1use super::schema::{allowed_states, gpio_state};
2use std::collections::HashMap;
3
4#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
5#[table_name = "gpio_state"]
6pub struct Gpio {
7    pub gpio_id: i32,                // 0..16 + 21..31
8    pub in_use: i32,                 // 0 or 1
9    pub gpio_mode: Option<String>,   // INPUT or OUTPUT
10    pub gpio_level: Option<String>,  // HIGH or LOW
11    pub last_change: Option<String>, // Timestamp
12}
13
14#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
15#[table_name = "allowed_states"]
16pub struct AllowedStates {
17    pub state_id: i32,
18    pub state_type: String, // MODE or LEVEL
19    // i32 for true / false below
20    pub input: i32,
21    pub output: i32,
22    pub high: i32,
23    pub low: i32,
24}
25
26impl AllowedStates {
27    pub fn to_hashmap(&self) -> HashMap<&'static str, bool> {
28        let mut hashed: HashMap<&'static str, bool> = HashMap::new();
29        hashed.insert("input", self.input == 1);
30        hashed.insert("output", self.output == 1);
31        hashed.insert("high", self.high == 1);
32        hashed.insert("low", self.low == 1);
33        hashed
34    }
35}