Refactor models names
This commit is contained in:
		
							parent
							
								
									76b14ce75b
								
							
						
					
					
						commit
						be7f31906c
					
				
					 24 changed files with 461 additions and 340 deletions
				
			
		| 
						 | 
				
			
			@ -13,3 +13,48 @@ file = "stdout"
 | 
			
		|||
driver = "gpio"
 | 
			
		||||
pin = 5
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 4
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 3
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 2
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 1
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 0
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 16
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "gpio"
 | 
			
		||||
pin = 15
 | 
			
		||||
inverted = 1
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "piface"
 | 
			
		||||
pin = 1
 | 
			
		||||
inverted = 0
 | 
			
		||||
 | 
			
		||||
[[relays]]
 | 
			
		||||
driver = "piface"
 | 
			
		||||
pin = 0
 | 
			
		||||
inverted = 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,17 +2,19 @@ use serde::{Deserialize, Deserializer};
 | 
			
		|||
 | 
			
		||||
#[derive(Debug, Clone, Copy)]
 | 
			
		||||
pub enum Driver {
 | 
			
		||||
    Gpio,
 | 
			
		||||
    Piface,
 | 
			
		||||
	Gpio,
 | 
			
		||||
	Piface,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'de> Deserialize<'de> for Driver {
 | 
			
		||||
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
 | 
			
		||||
        match String::deserialize(deserializer)?.as_str() {
 | 
			
		||||
            "gpio" => Ok(Driver::Gpio),
 | 
			
		||||
            "piface" => Ok(Driver::Piface),
 | 
			
		||||
            _ => Err(serde::de::Error::custom("invalid driver")),
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
 | 
			
		||||
	where
 | 
			
		||||
		D: Deserializer<'de>,
 | 
			
		||||
	{
 | 
			
		||||
		match String::deserialize(deserializer)?.as_str() {
 | 
			
		||||
			"gpio" => Ok(Driver::Gpio),
 | 
			
		||||
			"piface" => Ok(Driver::Piface),
 | 
			
		||||
			_ => Err(serde::de::Error::custom("invalid driver")),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,32 +1,51 @@
 | 
			
		|||
use std::str;
 | 
			
		||||
 | 
			
		||||
use futures::{future, pin_mut, SinkExt, StreamExt};
 | 
			
		||||
use crate::relay_loop::run_relay_loop;
 | 
			
		||||
use crate::settings::Settings;
 | 
			
		||||
use emgauwa_lib::db::errors::DatabaseError;
 | 
			
		||||
use emgauwa_lib::db::{DbController, DbRelay};
 | 
			
		||||
use emgauwa_lib::types::ControllerUid;
 | 
			
		||||
use emgauwa_lib::{db, models};
 | 
			
		||||
use futures::channel::mpsc;
 | 
			
		||||
use futures::{future, pin_mut, SinkExt, StreamExt};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use tokio::io::AsyncReadExt;
 | 
			
		||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
 | 
			
		||||
use tokio_tungstenite::tungstenite::Error;
 | 
			
		||||
use emgauwa_lib::db;
 | 
			
		||||
use emgauwa_lib::db::Controller;
 | 
			
		||||
use emgauwa_lib::db::types::ControllerUid;
 | 
			
		||||
use crate::relay_loop::run_relay_loop;
 | 
			
		||||
use crate::settings::Settings;
 | 
			
		||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
 | 
			
		||||
 | 
			
		||||
mod settings;
 | 
			
		||||
mod driver;
 | 
			
		||||
mod relay_loop;
 | 
			
		||||
mod settings;
 | 
			
		||||
 | 
			
		||||
fn create_this_controller(conn: &mut PoolConnection<Sqlite>, settings: &Settings) -> Controller {
 | 
			
		||||
	futures::executor::block_on(async {
 | 
			
		||||
		Controller::create(
 | 
			
		||||
			conn,
 | 
			
		||||
			&ControllerUid::new(),
 | 
			
		||||
			&settings.name,
 | 
			
		||||
			i64::try_from(settings.relays.len()).expect("Too many relays"),
 | 
			
		||||
			true
 | 
			
		||||
		).await.expect("Failed to create controller")
 | 
			
		||||
	})
 | 
			
		||||
async fn create_this_controller(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	settings: &Settings,
 | 
			
		||||
) -> DbController {
 | 
			
		||||
	DbController::create(
 | 
			
		||||
		conn,
 | 
			
		||||
		&ControllerUid::default(),
 | 
			
		||||
		&settings.name,
 | 
			
		||||
		i64::try_from(settings.relays.len()).expect("Too many relays"),
 | 
			
		||||
		true,
 | 
			
		||||
	)
 | 
			
		||||
	.await
 | 
			
		||||
	.expect("Failed to create controller")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn create_this_relay(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	this_controller: &DbController,
 | 
			
		||||
	settings_relay: &settings::Relay,
 | 
			
		||||
) -> DbRelay {
 | 
			
		||||
	DbRelay::create(
 | 
			
		||||
		conn,
 | 
			
		||||
		&settings_relay.name,
 | 
			
		||||
		settings_relay.number.unwrap(),
 | 
			
		||||
		this_controller,
 | 
			
		||||
	)
 | 
			
		||||
	.await
 | 
			
		||||
	.expect("Failed to create relay")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
| 
						 | 
				
			
			@ -37,22 +56,60 @@ async fn main() {
 | 
			
		|||
 | 
			
		||||
	let mut conn = pool.acquire().await.unwrap();
 | 
			
		||||
 | 
			
		||||
	let this = Controller::get_all(&mut conn)
 | 
			
		||||
	let db_controller = DbController::get_all(&mut conn)
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Failed to get controller from database")
 | 
			
		||||
		.pop()
 | 
			
		||||
		.unwrap_or_else(|| create_this_controller(&mut conn, &settings));
 | 
			
		||||
		.unwrap_or_else(|| {
 | 
			
		||||
			futures::executor::block_on(create_this_controller(&mut conn, &settings))
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
	let db_relays: Vec<DbRelay> = settings
 | 
			
		||||
		.relays
 | 
			
		||||
		.iter()
 | 
			
		||||
		.map(|relay| {
 | 
			
		||||
			futures::executor::block_on(async {
 | 
			
		||||
				match DbRelay::get_by_controller_and_num(
 | 
			
		||||
					&mut conn,
 | 
			
		||||
					&db_controller,
 | 
			
		||||
					relay.number.unwrap(),
 | 
			
		||||
				)
 | 
			
		||||
				.await
 | 
			
		||||
				{
 | 
			
		||||
					Ok(relay) => relay,
 | 
			
		||||
					Err(err) => match err {
 | 
			
		||||
						DatabaseError::NotFound => {
 | 
			
		||||
							create_this_relay(&mut conn, &db_controller, relay).await
 | 
			
		||||
						}
 | 
			
		||||
						_ => panic!("Failed to get relay from database"),
 | 
			
		||||
					},
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	let db_controller = db_controller
 | 
			
		||||
		.update(&mut conn, &db_controller.name, db_relays.len() as i64, true)
 | 
			
		||||
		.await
 | 
			
		||||
		.unwrap();
 | 
			
		||||
 | 
			
		||||
	let relays = db_relays
 | 
			
		||||
		.into_iter()
 | 
			
		||||
		.map(|relay| models::Relay::from_db_relay(relay, &mut conn))
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	let this = models::Controller {
 | 
			
		||||
		controller: db_controller,
 | 
			
		||||
		relays,
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	let this_json = serde_json::to_string(&this).unwrap();
 | 
			
		||||
 | 
			
		||||
	println!("{:?}", settings.relays);
 | 
			
		||||
	println!("{:?}", this);
 | 
			
		||||
	println!("{}", this_json);
 | 
			
		||||
 | 
			
		||||
	let url = format!(
 | 
			
		||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
			
		||||
		settings.core.host,
 | 
			
		||||
		settings.core.port
 | 
			
		||||
		settings.core.host, settings.core.port
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	let (stdin_tx, stdin_rx) = mpsc::unbounded();
 | 
			
		||||
| 
						 | 
				
			
			@ -83,13 +140,14 @@ async fn read_stdin(tx: mpsc::UnboundedSender<Message>) {
 | 
			
		|||
			Ok(n) => n,
 | 
			
		||||
		};
 | 
			
		||||
		buf.truncate(n);
 | 
			
		||||
		tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap())).unwrap();
 | 
			
		||||
		tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap()))
 | 
			
		||||
			.unwrap();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn handle_message(message_result: Result<Message, Error>) {
 | 
			
		||||
	match message_result {
 | 
			
		||||
		Ok(message) => println!("{}", message.into_text().unwrap()),
 | 
			
		||||
		Err(err) => println!("Error: {}", err)
 | 
			
		||||
		Err(err) => println!("Error: {}", err),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,9 +1,8 @@
 | 
			
		|||
use chrono::Local;
 | 
			
		||||
use crate::settings::Settings;
 | 
			
		||||
use chrono::Local;
 | 
			
		||||
 | 
			
		||||
#[allow(unused_variables)]
 | 
			
		||||
pub async fn run_relay_loop(settings: Settings) {
 | 
			
		||||
    let next_timestamp = Local::now().naive_local();
 | 
			
		||||
    loop {
 | 
			
		||||
    }
 | 
			
		||||
	let next_timestamp = Local::now().naive_local();
 | 
			
		||||
	loop {}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
use emgauwa_lib::{constants, utils};
 | 
			
		||||
use serde_derive::Deserialize;
 | 
			
		||||
 | 
			
		||||
use crate::driver::Driver;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, Deserialize)]
 | 
			
		||||
| 
						 | 
				
			
			@ -23,6 +24,8 @@ pub struct Logging {
 | 
			
		|||
#[allow(unused)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	pub driver: Driver,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
	pub number: Option<i64>,
 | 
			
		||||
	pub pin: u8,
 | 
			
		||||
	pub inverted: bool,
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -54,6 +57,8 @@ impl Default for Relay {
 | 
			
		|||
	fn default() -> Self {
 | 
			
		||||
		Relay {
 | 
			
		||||
			driver: Driver::Gpio,
 | 
			
		||||
			number: None,
 | 
			
		||||
			name: String::from("Relay"),
 | 
			
		||||
			pin: 0,
 | 
			
		||||
			inverted: false,
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -79,5 +84,13 @@ impl Default for Logging {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn init() -> Settings {
 | 
			
		||||
	utils::load_settings("controller", "CONTROLLER")
 | 
			
		||||
	let mut settings: Settings = utils::load_settings("controller", "CONTROLLER");
 | 
			
		||||
 | 
			
		||||
	for (num, relay) in settings.relays.iter_mut().enumerate() {
 | 
			
		||||
		if relay.number.is_none() {
 | 
			
		||||
			relay.number = Some(num as i64);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	settings
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
use std::str::FromStr;
 | 
			
		||||
use actix_cors::Cors;
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use actix_web::middleware::TrailingSlash;
 | 
			
		||||
use actix_web::{middleware, web, App, HttpServer};
 | 
			
		||||
| 
						 | 
				
			
			@ -13,8 +13,8 @@ mod settings;
 | 
			
		|||
async fn main() -> std::io::Result<()> {
 | 
			
		||||
	let settings = settings::init();
 | 
			
		||||
 | 
			
		||||
	let log_level: LevelFilter = LevelFilter::from_str(&settings.logging.level)
 | 
			
		||||
		.expect("Error parsing log level.");
 | 
			
		||||
	let log_level: LevelFilter =
 | 
			
		||||
		LevelFilter::from_str(&settings.logging.level).expect("Error parsing log level.");
 | 
			
		||||
	trace!("Log level set to {:?}", log_level);
 | 
			
		||||
 | 
			
		||||
	SimpleLogger::new()
 | 
			
		||||
| 
						 | 
				
			
			@ -26,7 +26,6 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
 | 
			
		||||
	log::info!("Starting server on {}:{}", settings.host, settings.port);
 | 
			
		||||
	HttpServer::new(move || {
 | 
			
		||||
 | 
			
		||||
		let cors = Cors::default()
 | 
			
		||||
			.allow_any_method()
 | 
			
		||||
			.allow_any_header()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,11 +5,11 @@ use sqlx::pool::PoolConnection;
 | 
			
		|||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ControllerUid;
 | 
			
		||||
use crate::db::DbTag;
 | 
			
		||||
use crate::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
pub struct DbController {
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub uid: ControllerUid,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
| 
						 | 
				
			
			@ -17,11 +17,11 @@ pub struct Controller {
 | 
			
		|||
	pub active: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Controller {
 | 
			
		||||
impl DbController {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<Controller>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Controller, "SELECT * FROM controllers")
 | 
			
		||||
	) -> Result<Vec<DbController>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbController, "SELECT * FROM controllers")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -29,12 +29,8 @@ impl Controller {
 | 
			
		|||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Controller, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Controller,
 | 
			
		||||
			"SELECT * FROM controllers WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbController, "SELECT * FROM controllers WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
| 
						 | 
				
			
			@ -43,9 +39,9 @@ impl Controller {
 | 
			
		|||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		filter_uid: &ControllerUid,
 | 
			
		||||
	) -> Result<Controller, DatabaseError> {
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Controller,
 | 
			
		||||
			DbController,
 | 
			
		||||
			"SELECT * FROM controllers WHERE uid = ?",
 | 
			
		||||
			filter_uid
 | 
			
		||||
		)
 | 
			
		||||
| 
						 | 
				
			
			@ -56,9 +52,9 @@ impl Controller {
 | 
			
		|||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tag: &Tag,
 | 
			
		||||
	) -> Result<Vec<Controller>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Controller, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbController>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbController, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -81,10 +77,10 @@ impl Controller {
 | 
			
		|||
		new_uid: &ControllerUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
		new_active: bool
 | 
			
		||||
	) -> Result<Controller, DatabaseError> {
 | 
			
		||||
		new_active: bool,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Controller,
 | 
			
		||||
			DbController,
 | 
			
		||||
			"INSERT INTO controllers (uid, name, relay_count, active) VALUES (?, ?, ?, ?) RETURNING *",
 | 
			
		||||
			new_uid,
 | 
			
		||||
			new_name,
 | 
			
		||||
| 
						 | 
				
			
			@ -101,8 +97,8 @@ impl Controller {
 | 
			
		|||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
		new_active: bool
 | 
			
		||||
	) -> Result<Controller, DatabaseError> {
 | 
			
		||||
		new_active: bool,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		sqlx::query!(
 | 
			
		||||
			"UPDATE controllers SET name = ?, relay_count = ?, active = ? WHERE id = ?",
 | 
			
		||||
			new_name,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,20 +6,19 @@ use std::str::FromStr;
 | 
			
		|||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::types::ScheduleUid;
 | 
			
		||||
use crate::types::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
mod controllers;
 | 
			
		||||
pub mod errors;
 | 
			
		||||
mod model_utils;
 | 
			
		||||
mod relays;
 | 
			
		||||
mod schedules;
 | 
			
		||||
mod tag;
 | 
			
		||||
pub mod types;
 | 
			
		||||
mod controllers;
 | 
			
		||||
mod relays;
 | 
			
		||||
 | 
			
		||||
pub use controllers::Controller;
 | 
			
		||||
pub use relays::Relay;
 | 
			
		||||
pub use schedules::{Periods, Schedule};
 | 
			
		||||
pub use tag::Tag;
 | 
			
		||||
pub use controllers::DbController;
 | 
			
		||||
pub use relays::DbRelay;
 | 
			
		||||
pub use schedules::{DbPeriods, DbSchedule};
 | 
			
		||||
pub use tag::DbTag;
 | 
			
		||||
 | 
			
		||||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,16 +31,16 @@ async fn init_schedule(
 | 
			
		|||
	pool: &Pool<Sqlite>,
 | 
			
		||||
	uid: &ScheduleUid,
 | 
			
		||||
	name: &str,
 | 
			
		||||
	periods: Periods,
 | 
			
		||||
	periods: DbPeriods,
 | 
			
		||||
) -> Result<(), DatabaseError> {
 | 
			
		||||
	trace!("Initializing schedule {:?}", name);
 | 
			
		||||
	match Schedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
 | 
			
		||||
	match DbSchedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
 | 
			
		||||
		Ok(_) => Ok(()),
 | 
			
		||||
		Err(err) => match err {
 | 
			
		||||
			DatabaseError::NotFound => {
 | 
			
		||||
				trace!("Schedule {:?} not found, inserting", name);
 | 
			
		||||
				sqlx::query_as!(
 | 
			
		||||
					Schedule,
 | 
			
		||||
					DbSchedule,
 | 
			
		||||
					"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
					uid,
 | 
			
		||||
					name,
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +70,7 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
 | 
			
		|||
 | 
			
		||||
	run_migrations(&pool).await;
 | 
			
		||||
 | 
			
		||||
	init_schedule(&pool, &ScheduleUid::Off, "Off", Periods(vec![]))
 | 
			
		||||
	init_schedule(&pool, &ScheduleUid::Off, "Off", DbPeriods(vec![]))
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Error initializing schedule Off");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -79,7 +78,7 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
 | 
			
		|||
		&pool,
 | 
			
		||||
		&ScheduleUid::On,
 | 
			
		||||
		"On",
 | 
			
		||||
		Periods(vec![Period::new_on()]),
 | 
			
		||||
		DbPeriods(vec![Period::new_on()]),
 | 
			
		||||
	)
 | 
			
		||||
	.await
 | 
			
		||||
	.expect("Error initializing schedule On");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
use crate::db::Periods;
 | 
			
		||||
use crate::db::DbPeriods;
 | 
			
		||||
use chrono::{NaiveTime, Timelike};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::database::HasArguments;
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +51,7 @@ impl Period {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for Periods {
 | 
			
		||||
impl Type<Sqlite> for DbPeriods {
 | 
			
		||||
	fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -61,22 +61,22 @@ impl Type<Sqlite> for Periods {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for Periods {
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for DbPeriods {
 | 
			
		||||
	//noinspection DuplicatedCode
 | 
			
		||||
	fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
		<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for Periods {
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for DbPeriods {
 | 
			
		||||
	fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
 | 
			
		||||
		let blob = <&[u8] as Decode<Sqlite>>::decode(value)?;
 | 
			
		||||
		Ok(Periods::from(Vec::from(blob)))
 | 
			
		||||
		Ok(DbPeriods::from(Vec::from(blob)))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&Periods> for Vec<u8> {
 | 
			
		||||
	fn from(periods: &Periods) -> Vec<u8> {
 | 
			
		||||
impl From<&DbPeriods> for Vec<u8> {
 | 
			
		||||
	fn from(periods: &DbPeriods) -> Vec<u8> {
 | 
			
		||||
		periods
 | 
			
		||||
			.0
 | 
			
		||||
			.iter()
 | 
			
		||||
| 
						 | 
				
			
			@ -93,7 +93,7 @@ impl From<&Periods> for Vec<u8> {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for Periods {
 | 
			
		||||
impl From<Vec<u8>> for DbPeriods {
 | 
			
		||||
	fn from(value: Vec<u8>) -> Self {
 | 
			
		||||
		let mut vec = Vec::new();
 | 
			
		||||
		for i in (3..value.len()).step_by(4) {
 | 
			
		||||
| 
						 | 
				
			
			@ -106,6 +106,6 @@ impl From<Vec<u8>> for Periods {
 | 
			
		|||
				end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
		Periods(vec)
 | 
			
		||||
		DbPeriods(vec)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,16 +1,15 @@
 | 
			
		|||
use serde_derive::Serialize;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbController;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use crate::db::Controller;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::DbTag;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
#[derive(sqlx::FromRow)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
#[derive(Debug, Serialize, Clone, sqlx::FromRow)]
 | 
			
		||||
pub struct DbRelay {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
| 
						 | 
				
			
			@ -19,34 +18,41 @@ pub struct Relay {
 | 
			
		|||
	pub controller_id: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Relay {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<Relay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Relay, "SELECT * FROM relays")
 | 
			
		||||
impl DbRelay {
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbRelay, "SELECT * FROM relays")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Relay,
 | 
			
		||||
			"SELECT * FROM relays WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
	pub async fn get(conn: &mut PoolConnection<Sqlite>, id: i64) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_controller_and_num(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		controller: &DbController,
 | 
			
		||||
		number: i64,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbRelay,
 | 
			
		||||
			"SELECT * FROM relays WHERE controller_id = ? AND number = ?",
 | 
			
		||||
			controller.id,
 | 
			
		||||
			number
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tag: &Tag,
 | 
			
		||||
	) -> Result<Vec<Relay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Relay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbRelay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -55,10 +61,10 @@ impl Relay {
 | 
			
		|||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_number: i64,
 | 
			
		||||
		new_controller: &Controller,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		new_controller: &DbController,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Relay,
 | 
			
		||||
			DbRelay,
 | 
			
		||||
			"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_number,
 | 
			
		||||
| 
						 | 
				
			
			@ -69,10 +75,7 @@ impl Relay {
 | 
			
		|||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn delete(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
	pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
| 
						 | 
				
			
			@ -87,8 +90,8 @@ impl Relay {
 | 
			
		|||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_number: i64,
 | 
			
		||||
		new_controller: &Controller,
 | 
			
		||||
	) -> Result<Relay, DatabaseError> {
 | 
			
		||||
		new_controller: &DbController,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		sqlx::query!(
 | 
			
		||||
			"UPDATE relays SET name = ?, number = ?, controller_id = ? WHERE id = ?",
 | 
			
		||||
			new_name,
 | 
			
		||||
| 
						 | 
				
			
			@ -99,14 +102,20 @@ impl Relay {
 | 
			
		|||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Relay::get(conn, self.id).await
 | 
			
		||||
		DbRelay::get(conn, self.id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_controller(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Controller, DatabaseError> {
 | 
			
		||||
		Controller::get(conn, self.controller_id).await
 | 
			
		||||
	pub async fn get_controller(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
		DbController::get(conn, self.controller_id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
	pub async fn get_tags(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
| 
						 | 
				
			
			@ -122,7 +131,7 @@ impl Relay {
 | 
			
		|||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			tag.link_relay(conn, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,27 +7,27 @@ use sqlx::Sqlite;
 | 
			
		|||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ScheduleUid;
 | 
			
		||||
use crate::db::DbTag;
 | 
			
		||||
use crate::types::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
pub struct DbSchedule {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	#[serde(rename(serialize = "id"))]
 | 
			
		||||
	pub uid: ScheduleUid,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
	pub periods: Periods,
 | 
			
		||||
	pub periods: DbPeriods,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 | 
			
		||||
pub struct Periods(pub Vec<Period>);
 | 
			
		||||
pub struct DbPeriods(pub Vec<Period>);
 | 
			
		||||
 | 
			
		||||
impl Schedule {
 | 
			
		||||
impl DbSchedule {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<Schedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Schedule, "SELECT * FROM schedules")
 | 
			
		||||
	) -> Result<Vec<DbSchedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbSchedule, "SELECT * FROM schedules")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -35,12 +35,8 @@ impl Schedule {
 | 
			
		|||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
			"SELECT * FROM schedules WHERE id = ?",
 | 
			
		||||
			id
 | 
			
		||||
		)
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbSchedule, "SELECT * FROM schedules WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
| 
						 | 
				
			
			@ -49,9 +45,9 @@ impl Schedule {
 | 
			
		|||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		filter_uid: &ScheduleUid,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
			DbSchedule,
 | 
			
		||||
			"SELECT * FROM schedules WHERE uid = ?",
 | 
			
		||||
			filter_uid
 | 
			
		||||
		)
 | 
			
		||||
| 
						 | 
				
			
			@ -62,9 +58,9 @@ impl Schedule {
 | 
			
		|||
 | 
			
		||||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tag: &Tag,
 | 
			
		||||
	) -> Result<Vec<Schedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Schedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbSchedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbSchedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -91,11 +87,11 @@ impl Schedule {
 | 
			
		|||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &Periods,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		new_periods: &DbPeriods,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		let uid = ScheduleUid::default();
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
			DbSchedule,
 | 
			
		||||
			"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
			uid,
 | 
			
		||||
			new_name,
 | 
			
		||||
| 
						 | 
				
			
			@ -110,8 +106,8 @@ impl Schedule {
 | 
			
		|||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &Periods,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		new_periods: &DbPeriods,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		// overwrite periods on protected schedules
 | 
			
		||||
		let new_periods = match self.uid {
 | 
			
		||||
			ScheduleUid::Off | ScheduleUid::On => self.periods.borrow(),
 | 
			
		||||
| 
						 | 
				
			
			@ -127,7 +123,7 @@ impl Schedule {
 | 
			
		|||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Schedule::get(conn, self.id).await
 | 
			
		||||
		DbSchedule::get(conn, self.id).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_tags(
 | 
			
		||||
| 
						 | 
				
			
			@ -149,7 +145,7 @@ impl Schedule {
 | 
			
		|||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?;
 | 
			
		||||
			tag.link_schedule(conn, self).await?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,28 +5,28 @@ use sqlx::pool::PoolConnection;
 | 
			
		|||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::{Relay, Schedule};
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Tag {
 | 
			
		||||
pub struct DbTag {
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub tag: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct JunctionTag {
 | 
			
		||||
pub struct DbJunctionTag {
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub tag_id: i64,
 | 
			
		||||
	pub relay_id: Option<i64>,
 | 
			
		||||
	pub schedule_id: Option<i64>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Tag {
 | 
			
		||||
impl DbTag {
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
	) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Tag,
 | 
			
		||||
			DbTag,
 | 
			
		||||
			"INSERT INTO tags (tag) VALUES (?) RETURNING *",
 | 
			
		||||
			new_tag
 | 
			
		||||
		)
 | 
			
		||||
| 
						 | 
				
			
			@ -35,11 +35,8 @@ impl Tag {
 | 
			
		|||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(Tag, "SELECT * FROM tags WHERE id = ?", id)
 | 
			
		||||
	pub async fn get(conn: &mut PoolConnection<Sqlite>, id: i64) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
| 
						 | 
				
			
			@ -48,10 +45,10 @@ impl Tag {
 | 
			
		|||
	pub async fn get_by_tag_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		match Tag::get_by_tag(conn, target_tag).await {
 | 
			
		||||
	) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		match DbTag::get_by_tag(conn, target_tag).await {
 | 
			
		||||
			Ok(tag) => Ok(tag),
 | 
			
		||||
			Err(DatabaseError::NotFound) => Tag::create(conn, target_tag).await,
 | 
			
		||||
			Err(DatabaseError::NotFound) => DbTag::create(conn, target_tag).await,
 | 
			
		||||
			Err(e) => Err(e),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -59,8 +56,8 @@ impl Tag {
 | 
			
		|||
	pub async fn get_by_tag(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
 | 
			
		||||
	) -> Result<DbTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE tag = ?", target_tag)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
| 
						 | 
				
			
			@ -69,10 +66,10 @@ impl Tag {
 | 
			
		|||
	pub async fn link_relay(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_relay: &Relay,
 | 
			
		||||
	) -> Result<JunctionTag, DatabaseError> {
 | 
			
		||||
		target_relay: &DbRelay,
 | 
			
		||||
	) -> Result<DbJunctionTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			JunctionTag,
 | 
			
		||||
			DbJunctionTag,
 | 
			
		||||
			"INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *",
 | 
			
		||||
			self.id,
 | 
			
		||||
			target_relay.id
 | 
			
		||||
| 
						 | 
				
			
			@ -85,10 +82,10 @@ impl Tag {
 | 
			
		|||
	pub async fn link_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_schedule: &Schedule,
 | 
			
		||||
	) -> Result<JunctionTag, DatabaseError> {
 | 
			
		||||
		target_schedule: &DbSchedule,
 | 
			
		||||
	) -> Result<DbJunctionTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			JunctionTag,
 | 
			
		||||
			DbJunctionTag,
 | 
			
		||||
			"INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *",
 | 
			
		||||
			self.id,
 | 
			
		||||
			target_schedule.id
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,68 +0,0 @@
 | 
			
		|||
use serde::{Serialize, Serializer};
 | 
			
		||||
use sqlx::{Decode, Encode, Sqlite, Type};
 | 
			
		||||
use sqlx::database::HasArguments;
 | 
			
		||||
use sqlx::encode::IsNull;
 | 
			
		||||
use sqlx::error::BoxDynError;
 | 
			
		||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug)]
 | 
			
		||||
pub struct ControllerUid(Uuid);
 | 
			
		||||
 | 
			
		||||
impl ControllerUid {
 | 
			
		||||
    pub fn new() -> Self {
 | 
			
		||||
        Self(Uuid::new_v4())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for ControllerUid {
 | 
			
		||||
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
        where
 | 
			
		||||
            S: Serializer,
 | 
			
		||||
    {
 | 
			
		||||
        String::from(self).serialize(serializer)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&ControllerUid> for String {
 | 
			
		||||
    fn from(uid: &ControllerUid) -> String {
 | 
			
		||||
        uid.0.as_hyphenated().to_string()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for ControllerUid {
 | 
			
		||||
    fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
        <&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn compatible(ty: &SqliteTypeInfo) -> bool {
 | 
			
		||||
        <&[u8] as Type<Sqlite>>::compatible(ty)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for ControllerUid {
 | 
			
		||||
    //noinspection DuplicatedCode
 | 
			
		||||
    fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
        let uuid_val = self.0.as_bytes().to_vec();
 | 
			
		||||
        <&Vec<u8> as Encode<Sqlite>>::encode(&uuid_val, buf)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for ControllerUid {
 | 
			
		||||
    //noinspection DuplicatedCode
 | 
			
		||||
    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
 | 
			
		||||
        Ok(Self::from(<&[u8] as Decode<Sqlite>>::decode(value)?))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&[u8]> for ControllerUid {
 | 
			
		||||
    fn from(value: &[u8]) -> Self {
 | 
			
		||||
        Self(Uuid::from_slice(&value).unwrap())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for ControllerUid {
 | 
			
		||||
    fn from(value: Vec<u8>) -> Self {
 | 
			
		||||
        Self::from(value.as_slice())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,3 +1,3 @@
 | 
			
		|||
pub mod relays;
 | 
			
		||||
pub mod schedules;
 | 
			
		||||
pub mod ws;
 | 
			
		||||
pub mod relays;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,13 +1,12 @@
 | 
			
		|||
use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
			
		||||
use actix_web::{get, web, HttpResponse};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::Relay;
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::DbRelay;
 | 
			
		||||
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::return_models::ReturnRelay;
 | 
			
		||||
use crate::models::Relay;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestRelay {
 | 
			
		||||
| 
						 | 
				
			
			@ -19,10 +18,12 @@ pub struct RequestRelay {
 | 
			
		|||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let relays = Relay::get_all(&mut pool_conn).await?;
 | 
			
		||||
	let relays = DbRelay::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let return_relays: Vec<ReturnRelay> =
 | 
			
		||||
		relays.into_iter().map(|s| ReturnRelay::from_relay(s, &mut pool_conn)).collect();
 | 
			
		||||
	let return_relays: Vec<Relay> = relays
 | 
			
		||||
		.into_iter()
 | 
			
		||||
		.map(|s| Relay::from_db_relay(s, &mut pool_conn))
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_relays))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,16 +4,16 @@ use sqlx::pool::PoolConnection;
 | 
			
		|||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::{Periods, Schedule};
 | 
			
		||||
use crate::db::Tag;
 | 
			
		||||
use crate::db::types::ScheduleUid;
 | 
			
		||||
use crate::db::DbTag;
 | 
			
		||||
use crate::db::{DbPeriods, DbSchedule};
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::return_models::ReturnSchedule;
 | 
			
		||||
use crate::models::Schedule;
 | 
			
		||||
use crate::types::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestSchedule {
 | 
			
		||||
	name: String,
 | 
			
		||||
	periods: Periods,
 | 
			
		||||
	periods: DbPeriods,
 | 
			
		||||
	tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -21,10 +21,12 @@ pub struct RequestSchedule {
 | 
			
		|||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let schedules = Schedule::get_all(&mut pool_conn).await?;
 | 
			
		||||
	let schedules = DbSchedule::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.into_iter().map(|s| ReturnSchedule::from_schedule(s, &mut pool_conn)).collect();
 | 
			
		||||
	let return_schedules: Vec<Schedule> = schedules
 | 
			
		||||
		.into_iter()
 | 
			
		||||
		.map(|s| Schedule::from_schedule(s, &mut pool_conn))
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -37,12 +39,14 @@ pub async fn tagged(
 | 
			
		|||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let (tag,) = path.into_inner();
 | 
			
		||||
	let tag_db = Tag::get_by_tag(&mut pool_conn, &tag).await?;
 | 
			
		||||
	let tag_db = DbTag::get_by_tag(&mut pool_conn, &tag).await?;
 | 
			
		||||
 | 
			
		||||
	let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
	let schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
 | 
			
		||||
	let return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.into_iter().map(|s| ReturnSchedule::from_schedule(s, &mut pool_conn)).collect();
 | 
			
		||||
	let return_schedules: Vec<Schedule> = schedules
 | 
			
		||||
		.into_iter()
 | 
			
		||||
		.map(|s| Schedule::from_schedule(s, &mut pool_conn))
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -57,9 +61,9 @@ pub async fn show(
 | 
			
		|||
	let (schedule_uid,) = path.into_inner();
 | 
			
		||||
	let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
 | 
			
		||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
	let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	let return_schedule = Schedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,22 +74,22 @@ pub async fn add(
 | 
			
		|||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let new_schedule = Schedule::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
			
		||||
	let new_schedule = DbSchedule::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
			
		||||
 | 
			
		||||
	new_schedule
 | 
			
		||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(new_schedule, &mut pool_conn);
 | 
			
		||||
	let return_schedule = Schedule::from_schedule(new_schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Created().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn add_list_single(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	request_schedule: &RequestSchedule,
 | 
			
		||||
) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
	let new_schedule =
 | 
			
		||||
		Schedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
 | 
			
		||||
		DbSchedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
 | 
			
		||||
 | 
			
		||||
	new_schedule
 | 
			
		||||
		.set_tags(conn, request_schedule.tags.as_slice())
 | 
			
		||||
| 
						 | 
				
			
			@ -101,7 +105,7 @@ pub async fn add_list(
 | 
			
		|||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let result: Vec<Result<Schedule, DatabaseError>> = data
 | 
			
		||||
	let result: Vec<Result<DbSchedule, DatabaseError>> = data
 | 
			
		||||
		.as_slice()
 | 
			
		||||
		.iter()
 | 
			
		||||
		.map(|request_schedule| {
 | 
			
		||||
| 
						 | 
				
			
			@ -109,10 +113,12 @@ pub async fn add_list(
 | 
			
		|||
		})
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> = Vec::new();
 | 
			
		||||
	let mut return_schedules: Vec<Schedule> = Vec::new();
 | 
			
		||||
	for schedule in result {
 | 
			
		||||
		match schedule {
 | 
			
		||||
			Ok(schedule) => return_schedules.push(ReturnSchedule::from_schedule(schedule, &mut pool_conn)),
 | 
			
		||||
			Ok(schedule) => {
 | 
			
		||||
				return_schedules.push(Schedule::from_schedule(schedule, &mut pool_conn))
 | 
			
		||||
			}
 | 
			
		||||
			Err(e) => return Ok(HttpResponse::from(e)),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -130,7 +136,7 @@ pub async fn update(
 | 
			
		|||
	let (schedule_uid,) = path.into_inner();
 | 
			
		||||
	let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
 | 
			
		||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
	let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
 | 
			
		||||
	let schedule = schedule
 | 
			
		||||
		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
			
		||||
| 
						 | 
				
			
			@ -140,7 +146,7 @@ pub async fn update(
 | 
			
		|||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	let return_schedule = Schedule::from_schedule(schedule, &mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -158,7 +164,7 @@ pub async fn delete(
 | 
			
		|||
		ScheduleUid::Off => Err(ApiError::ProtectedSchedule),
 | 
			
		||||
		ScheduleUid::On => Err(ApiError::ProtectedSchedule),
 | 
			
		||||
		ScheduleUid::Any(_) => {
 | 
			
		||||
			Schedule::delete_by_uid(&mut pool_conn, uid).await?;
 | 
			
		||||
			DbSchedule::delete_by_uid(&mut pool_conn, uid).await?;
 | 
			
		||||
			Ok(HttpResponse::Ok().json("schedule got deleted"))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
use crate::db::Schedule;
 | 
			
		||||
use crate::db::DbSchedule;
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use actix::{Actor, StreamHandler};
 | 
			
		||||
use actix_web::{get, web, HttpRequest, HttpResponse};
 | 
			
		||||
| 
						 | 
				
			
			@ -15,10 +15,10 @@ impl Actor for ControllerWs {
 | 
			
		|||
	type Context = ws::WebsocketContext<Self>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn get_schedules(pool: &mut Pool<Sqlite>) -> Result<Vec<Schedule>, ApiError> {
 | 
			
		||||
async fn get_schedules(pool: &mut Pool<Sqlite>) -> Result<Vec<DbSchedule>, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	Ok(Schedule::get_all(&mut pool_conn).await?)
 | 
			
		||||
	Ok(DbSchedule::get_all(&mut pool_conn).await?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		||||
| 
						 | 
				
			
			@ -28,9 +28,9 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		|||
		match msg {
 | 
			
		||||
			Ok(Message::Ping(msg)) => ctx.pong(&msg),
 | 
			
		||||
			Ok(Message::Text(text)) => {
 | 
			
		||||
				println!("Got text: {}", text.to_string());
 | 
			
		||||
				println!("Got text: {}", text);
 | 
			
		||||
				ctx.text(schedules_json)
 | 
			
		||||
			},
 | 
			
		||||
			}
 | 
			
		||||
			_ => {}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
pub mod constants;
 | 
			
		||||
pub mod db;
 | 
			
		||||
pub mod handlers;
 | 
			
		||||
pub mod return_models;
 | 
			
		||||
pub mod models;
 | 
			
		||||
pub mod types;
 | 
			
		||||
pub mod utils;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										51
									
								
								emgauwa-lib/src/models/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								emgauwa-lib/src/models/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
use crate::db;
 | 
			
		||||
use futures::executor;
 | 
			
		||||
use serde_derive::Serialize;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Debug)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub schedule: db::DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Debug)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub relay: db::DbRelay,
 | 
			
		||||
	pub controller: db::DbController,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Debug)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub controller: db::DbController,
 | 
			
		||||
	pub relays: Vec<Relay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Schedule {
 | 
			
		||||
	pub fn from_schedule(schedule: db::DbSchedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let schedule = schedule.clone();
 | 
			
		||||
		let tags = executor::block_on(schedule.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
		Schedule { schedule, tags }
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Relay {
 | 
			
		||||
	pub fn from_db_relay(relay: db::DbRelay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let relay = relay.clone();
 | 
			
		||||
		let controller =
 | 
			
		||||
			executor::block_on(db::DbController::get(conn, relay.controller_id)).unwrap();
 | 
			
		||||
		let tags = executor::block_on(relay.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
		Relay {
 | 
			
		||||
			relay,
 | 
			
		||||
			controller,
 | 
			
		||||
			tags,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,50 +0,0 @@
 | 
			
		|||
use crate::db::{Controller, Relay, Schedule};
 | 
			
		||||
use futures::executor;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
use crate::db::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct ReturnSchedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub schedule: Schedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ReturnSchedule {
 | 
			
		||||
	pub fn from_schedule(schedule: Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let schedule = schedule.clone();
 | 
			
		||||
		let tags = executor::block_on(schedule.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
		ReturnSchedule {
 | 
			
		||||
			schedule,
 | 
			
		||||
			tags,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct ReturnRelay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub relay: Relay,
 | 
			
		||||
	pub controller: Controller,
 | 
			
		||||
	pub controller_id: ControllerUid,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ReturnRelay {
 | 
			
		||||
	pub fn from_relay(relay: Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
			
		||||
		let relay = relay.clone();
 | 
			
		||||
		let controller = executor::block_on(Controller::get(conn, relay.controller_id)).unwrap();
 | 
			
		||||
		let controller_uid = controller.uid.clone();
 | 
			
		||||
		let tags = executor::block_on(relay.get_tags(conn)).unwrap();
 | 
			
		||||
 | 
			
		||||
		ReturnRelay {
 | 
			
		||||
			relay,
 | 
			
		||||
			controller,
 | 
			
		||||
			controller_id: controller_uid,
 | 
			
		||||
			tags,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										68
									
								
								emgauwa-lib/src/types/controller_uid.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								emgauwa-lib/src/types/controller_uid.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,68 @@
 | 
			
		|||
use serde::{Serialize, Serializer};
 | 
			
		||||
use sqlx::database::HasArguments;
 | 
			
		||||
use sqlx::encode::IsNull;
 | 
			
		||||
use sqlx::error::BoxDynError;
 | 
			
		||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
 | 
			
		||||
use sqlx::{Decode, Encode, Sqlite, Type};
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug)]
 | 
			
		||||
pub struct ControllerUid(Uuid);
 | 
			
		||||
 | 
			
		||||
impl Default for ControllerUid {
 | 
			
		||||
	fn default() -> Self {
 | 
			
		||||
		Self(Uuid::new_v4())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for ControllerUid {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		String::from(self).serialize(serializer)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&ControllerUid> for String {
 | 
			
		||||
	fn from(uid: &ControllerUid) -> String {
 | 
			
		||||
		uid.0.as_hyphenated().to_string()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for ControllerUid {
 | 
			
		||||
	fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn compatible(ty: &SqliteTypeInfo) -> bool {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::compatible(ty)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for ControllerUid {
 | 
			
		||||
	//noinspection DuplicatedCode
 | 
			
		||||
	fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
		let uuid_val = self.0.as_bytes().to_vec();
 | 
			
		||||
		<&Vec<u8> as Encode<Sqlite>>::encode(&uuid_val, buf)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for ControllerUid {
 | 
			
		||||
	//noinspection DuplicatedCode
 | 
			
		||||
	fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
 | 
			
		||||
		Ok(Self::from(<&[u8] as Decode<Sqlite>>::decode(value)?))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&[u8]> for ControllerUid {
 | 
			
		||||
	fn from(value: &[u8]) -> Self {
 | 
			
		||||
		Self(Uuid::from_slice(value).unwrap())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for ControllerUid {
 | 
			
		||||
	fn from(value: Vec<u8>) -> Self {
 | 
			
		||||
		Self::from(value.as_slice())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
mod schedule_uid;
 | 
			
		||||
mod controller_uid;
 | 
			
		||||
mod schedule_uid;
 | 
			
		||||
 | 
			
		||||
pub use schedule_uid::ScheduleUid;
 | 
			
		||||
pub use controller_uid::ControllerUid;
 | 
			
		||||
pub use schedule_uid::ScheduleUid;
 | 
			
		||||
| 
						 | 
				
			
			@ -1,9 +1,8 @@
 | 
			
		|||
pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> T
 | 
			
		||||
where
 | 
			
		||||
	for<'de> T: serde::Deserialize<'de>
 | 
			
		||||
	for<'de> T: serde::Deserialize<'de>,
 | 
			
		||||
{
 | 
			
		||||
	let default_file = config::File::with_name(&format!("emgauwa-{}", config_name))
 | 
			
		||||
		.required(false);
 | 
			
		||||
	let default_file = config::File::with_name(&format!("emgauwa-{}", config_name)).required(false);
 | 
			
		||||
 | 
			
		||||
	config::Config::builder()
 | 
			
		||||
		.add_source(default_file)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue