Add connected controllers hashmap for controller-ws

This commit is contained in:
Tobias Reisinger 2023-11-28 20:20:12 +01:00
parent 6459804e1f
commit 6536ff0792
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
17 changed files with 267 additions and 649 deletions
emgauwa-lib/src/models

View file

@ -1,6 +1,5 @@
use crate::db;
use crate::db::errors::DatabaseError;
use crate::db::{DbRelay, DbSchedule};
use crate::db::{DbController, DbRelay, DbSchedule};
use crate::types::ControllerUid;
use futures::executor;
use serde_derive::{Deserialize, Serialize};
@ -21,15 +20,15 @@ pub trait FromDbModel {
#[derive(Serialize, Deserialize, Debug)]
pub struct Schedule {
#[serde(flatten)]
pub schedule: DbSchedule,
pub s: DbSchedule,
pub tags: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Relay {
#[serde(flatten)]
pub relay: DbRelay,
pub controller: db::DbController,
pub r: DbRelay,
pub controller: DbController,
pub controller_id: ControllerUid,
pub tags: Vec<String>,
}
@ -37,7 +36,7 @@ pub struct Relay {
#[derive(Serialize, Deserialize, Debug)]
pub struct Controller {
#[serde(flatten)]
pub controller: db::DbController,
pub c: DbController,
pub relays: Vec<Relay>,
}
@ -51,7 +50,7 @@ impl FromDbModel for Schedule {
let schedule = db_model.clone();
let tags = executor::block_on(schedule.get_tags(conn))?;
Ok(Schedule { schedule, tags })
Ok(Schedule { s: schedule, tags })
}
}
@ -68,7 +67,7 @@ impl FromDbModel for Relay {
let tags = executor::block_on(relay.get_tags(conn))?;
Ok(Relay {
relay,
r: relay,
controller,
controller_id,
tags,
@ -76,6 +75,22 @@ impl FromDbModel for Relay {
}
}
impl FromDbModel for Controller {
type DbModel = DbController;
fn from_db_model(
conn: &mut PoolConnection<Sqlite>,
db_model: Self::DbModel,
) -> Result<Self, DatabaseError> {
let relays_db = executor::block_on(db_model.get_relays(conn))?;
let relays = convert_db_list(conn, relays_db)?;
Ok(Controller {
c: db_model,
relays,
})
}
}
pub fn convert_db_list<T: FromDbModel>(
conn: &mut PoolConnection<Sqlite>,
db_models: Vec<T::DbModel>,