diff --git a/emgauwa-controller/src/main.rs b/emgauwa-controller/src/main.rs index bde6b6d..e290d06 100644 --- a/emgauwa-controller/src/main.rs +++ b/emgauwa-controller/src/main.rs @@ -3,7 +3,7 @@ use emgauwa_lib::db; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::models::{Controller, FromDbModel}; -use emgauwa_lib::types::ControllerUid; +use emgauwa_lib::types::EmgauwaUid; use emgauwa_lib::utils::{drop_privileges, init_logging}; use rppal_pfd::PiFaceDigital; use sqlx::pool::PoolConnection; @@ -26,7 +26,7 @@ async fn create_this_controller( ) -> Result { DbController::create( conn, - &ControllerUid::default(), + &EmgauwaUid::default(), &settings.name, settings.relays.len() as i64, ) diff --git a/emgauwa-core/src/app_state.rs b/emgauwa-core/src/app_state.rs index 4edbd9d..c31a092 100644 --- a/emgauwa-core/src/app_state.rs +++ b/emgauwa-core/src/app_state.rs @@ -4,7 +4,7 @@ use actix::{Actor, Addr, Context, Handler, Message, Recipient}; use emgauwa_lib::db::DbController; use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::models::{convert_db_list, Controller, Relay}; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RelayStates}; +use emgauwa_lib::types::{ControllerWsAction, EmgauwaUid, RelayStates}; use futures::executor::block_on; use sqlx::{Pool, Sqlite}; @@ -13,7 +13,7 @@ use crate::handlers::v1::ws::relays::{RelaysWs, SendRelays}; #[derive(Message)] #[rtype(result = "Result<(), EmgauwaError>")] pub struct DisconnectController { - pub controller_uid: ControllerUid, + pub controller_uid: EmgauwaUid, } #[derive(Message)] @@ -26,7 +26,7 @@ pub struct ConnectController { #[derive(Message)] #[rtype(result = "()")] pub struct UpdateRelayStates { - pub controller_uid: ControllerUid, + pub controller_uid: EmgauwaUid, pub relay_states: RelayStates, } @@ -37,7 +37,7 @@ pub struct GetRelays {} #[derive(Message)] #[rtype(result = "Result<(), EmgauwaError>")] pub struct Action { - pub controller_uid: ControllerUid, + pub controller_uid: EmgauwaUid, pub action: ControllerWsAction, } @@ -49,7 +49,7 @@ pub struct ConnectRelayClient { pub struct AppState { pub pool: Pool, - pub connected_controllers: HashMap)>, + pub connected_controllers: HashMap)>, pub connected_relay_clients: Vec>, } diff --git a/emgauwa-core/src/handlers/v1/controllers.rs b/emgauwa-core/src/handlers/v1/controllers.rs index 24404fa..2bfafca 100644 --- a/emgauwa-core/src/handlers/v1/controllers.rs +++ b/emgauwa-core/src/handlers/v1/controllers.rs @@ -3,7 +3,7 @@ use actix_web::{delete, get, put, web, HttpResponse}; use emgauwa_lib::db::DbController; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel}; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestControllerUpdate}; +use emgauwa_lib::types::{ControllerWsAction, EmgauwaUid, RequestControllerUpdate}; use sqlx::{Pool, Sqlite}; use crate::app_state; @@ -28,7 +28,7 @@ pub async fn show( let mut pool_conn = pool.acquire().await?; let (controller_uid,) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? @@ -40,15 +40,15 @@ pub async fn show( #[put("/controllers/{controller_id}")] pub async fn update( - pool: web::Data>, - app_state: web::Data>, - path: web::Path<(String,)>, - data: web::Json, + pool: web::Data>, + app_state: web::Data>, + path: web::Path<(String,)>, + data: web::Json, ) -> Result { let mut pool_conn = pool.acquire().await?; let (controller_uid,) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? @@ -79,7 +79,7 @@ pub async fn delete( let mut pool_conn = pool.acquire().await?; let (controller_uid,) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; app_state .send(app_state::DisconnectController { diff --git a/emgauwa-core/src/handlers/v1/relays.rs b/emgauwa-core/src/handlers/v1/relays.rs index dd7a3d8..578745d 100644 --- a/emgauwa-core/src/handlers/v1/relays.rs +++ b/emgauwa-core/src/handlers/v1/relays.rs @@ -3,9 +3,7 @@ use actix_web::{get, post, put, web, HttpResponse}; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbTag}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay}; -use emgauwa_lib::types::{ - ControllerUid, ControllerWsAction, RequestRelayPulse, RequestRelayUpdate, -}; +use emgauwa_lib::types::{ControllerWsAction, EmgauwaUid, RequestRelayPulse, RequestRelayUpdate}; use emgauwa_lib::utils; use sqlx::{Pool, Sqlite}; @@ -49,7 +47,7 @@ pub async fn index_for_controller( let mut pool_conn = pool.acquire().await?; let (controller_uid,) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? @@ -69,7 +67,7 @@ pub async fn show_for_controller( let mut pool_conn = pool.acquire().await?; let (controller_uid, relay_num) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? @@ -93,7 +91,7 @@ pub async fn update_for_controller( let mut pool_conn = pool.acquire().await?; let (controller_uid, relay_num) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? @@ -162,7 +160,7 @@ pub async fn pulse( let mut pool_conn = pool.acquire().await?; let (controller_uid, relay_num) = path.into_inner(); - let uid = ControllerUid::try_from(controller_uid.as_str())?; + let uid = EmgauwaUid::try_from(controller_uid.as_str())?; let controller = DbController::get_by_uid(&mut pool_conn, &uid) .await? diff --git a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs index 7becb41..8980f60 100644 --- a/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs +++ b/emgauwa-core/src/handlers/v1/ws/controllers/handlers.rs @@ -2,7 +2,7 @@ use actix::{Actor, AsyncContext}; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{Controller, FromDbModel}; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RelayStates}; +use emgauwa_lib::types::{ControllerWsAction, EmgauwaUid, RelayStates}; use emgauwa_lib::utils; use futures::executor::block_on; use sqlx::pool::PoolConnection; @@ -97,7 +97,7 @@ impl ControllersWs { pub fn handle_relay_states( &mut self, - controller_uid: ControllerUid, + controller_uid: EmgauwaUid, relay_states: RelayStates, ) -> Result<(), EmgauwaError> { log::debug!( diff --git a/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs b/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs index 88aaf24..a54c9aa 100644 --- a/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs +++ b/emgauwa-core/src/handlers/v1/ws/controllers/mod.rs @@ -7,7 +7,7 @@ use actix_web_actors::ws; use actix_web_actors::ws::ProtocolError; use emgauwa_lib::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT}; use emgauwa_lib::errors::EmgauwaError; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction}; +use emgauwa_lib::types::{ControllerWsAction, EmgauwaUid}; use futures::executor::block_on; use sqlx::pool::PoolConnection; use sqlx::{Pool, Sqlite}; @@ -18,7 +18,7 @@ use crate::utils::flatten_result; pub struct ControllersWs { pub pool: Pool, - pub controller_uid: Option, + pub controller_uid: Option, pub app_state: Addr, pub hb: Instant, } diff --git a/emgauwa-lib/src/db/controllers.rs b/emgauwa-lib/src/db/controllers.rs index e9a0732..bf0a1f0 100644 --- a/emgauwa-lib/src/db/controllers.rs +++ b/emgauwa-lib/src/db/controllers.rs @@ -6,14 +6,14 @@ use sqlx::Sqlite; use crate::db::{DbRelay, DbTag}; use crate::errors::DatabaseError; -use crate::types::ControllerUid; +use crate::types::EmgauwaUid; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DbController { #[serde(skip)] pub id: i64, #[serde(rename = "id")] - pub uid: ControllerUid, + pub uid: EmgauwaUid, pub name: String, pub relay_count: i64, pub active: bool, @@ -41,7 +41,7 @@ impl DbController { pub async fn get_by_uid( conn: &mut PoolConnection, - filter_uid: &ControllerUid, + filter_uid: &EmgauwaUid, ) -> Result, DatabaseError> { sqlx::query_as!( DbController, @@ -55,7 +55,7 @@ impl DbController { pub async fn get_by_uid_or_create( conn: &mut PoolConnection, - uid: &ControllerUid, + uid: &EmgauwaUid, new_name: &str, new_relay_count: i64, ) -> Result { @@ -77,7 +77,7 @@ impl DbController { pub async fn delete_by_uid( conn: &mut PoolConnection, - filter_uid: ControllerUid, + filter_uid: EmgauwaUid, ) -> Result<(), DatabaseError> { if sqlx::query_scalar!("SELECT 1 FROM controllers WHERE uid = ?", filter_uid) .fetch_optional(conn.deref_mut()) @@ -98,7 +98,7 @@ impl DbController { pub async fn create( conn: &mut PoolConnection, - new_uid: &ControllerUid, + new_uid: &EmgauwaUid, new_name: &str, new_relay_count: i64, ) -> Result { diff --git a/emgauwa-lib/src/errors/emgauwa_error.rs b/emgauwa-lib/src/errors/emgauwa_error.rs index 3cf72d5..0e8ce65 100644 --- a/emgauwa-lib/src/errors/emgauwa_error.rs +++ b/emgauwa-lib/src/errors/emgauwa_error.rs @@ -13,7 +13,7 @@ use serde::ser::SerializeStruct; use serde::{Serialize, Serializer}; use crate::errors::{ApiError, DatabaseError}; -use crate::types::ControllerUid; +use crate::types::EmgauwaUid; #[derive(Debug)] pub enum EmgauwaError { @@ -23,7 +23,7 @@ pub enum EmgauwaError { Database(DatabaseError), Other(String), Internal(String), - Connection(ControllerUid), + Connection(EmgauwaUid), Hardware(String), } diff --git a/emgauwa-lib/src/models/relay.rs b/emgauwa-lib/src/models/relay.rs index 2be2137..a7d0e60 100644 --- a/emgauwa-lib/src/models/relay.rs +++ b/emgauwa-lib/src/models/relay.rs @@ -9,14 +9,14 @@ use sqlx::Sqlite; use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use crate::errors::DatabaseError; use crate::models::FromDbModel; -use crate::types::ControllerUid; +use crate::types::EmgauwaUid; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Relay { #[serde(flatten)] pub r: DbRelay, pub controller: DbController, - pub controller_id: ControllerUid, + pub controller_id: EmgauwaUid, pub schedules: Vec, pub active_schedule: DbSchedule, pub is_on: Option, diff --git a/emgauwa-lib/src/types/controller_uid.rs b/emgauwa-lib/src/types/emgauwa_uid.rs similarity index 67% rename from emgauwa-lib/src/types/controller_uid.rs rename to emgauwa-lib/src/types/emgauwa_uid.rs index bbaded8..8d29fbd 100644 --- a/emgauwa-lib/src/types/controller_uid.rs +++ b/emgauwa-lib/src/types/emgauwa_uid.rs @@ -10,21 +10,21 @@ use sqlx::{Decode, Encode, Sqlite, Type}; use uuid::Uuid; #[derive(Clone, Debug, Eq, PartialEq, Hash)] -pub struct ControllerUid(Uuid); +pub struct EmgauwaUid(Uuid); -impl Default for ControllerUid { +impl Default for EmgauwaUid { fn default() -> Self { Self(Uuid::new_v4()) } } -impl Display for ControllerUid { +impl Display for EmgauwaUid { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", String::from(self)) } } -impl Serialize for ControllerUid { +impl Serialize for EmgauwaUid { fn serialize(&self, serializer: S) -> Result where S: Serializer, @@ -33,23 +33,23 @@ impl Serialize for ControllerUid { } } -impl<'de> Deserialize<'de> for ControllerUid { +impl<'de> Deserialize<'de> for EmgauwaUid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Self::try_from(String::deserialize(deserializer)?.as_str()) - .map_err(|_| serde::de::Error::custom("invalid controller uid")) + .map_err(|_| serde::de::Error::custom("invalid uid")) } } -impl From<&ControllerUid> for String { - fn from(uid: &ControllerUid) -> String { +impl From<&EmgauwaUid> for String { + fn from(uid: &EmgauwaUid) -> String { uid.0.as_hyphenated().to_string() } } -impl Type for ControllerUid { +impl Type for EmgauwaUid { fn type_info() -> SqliteTypeInfo { <&[u8] as Type>::type_info() } @@ -59,27 +59,27 @@ impl Type for ControllerUid { } } -impl<'q> Encode<'q, Sqlite> for ControllerUid { +impl<'q> Encode<'q, Sqlite> for EmgauwaUid { //noinspection DuplicatedCode fn encode_by_ref(&self, buf: &mut >::ArgumentBuffer) -> IsNull { as Encode>::encode(Vec::from(self), buf) } } -impl<'r> Decode<'r, Sqlite> for ControllerUid { +impl<'r> Decode<'r, Sqlite> for EmgauwaUid { //noinspection DuplicatedCode fn decode(value: SqliteValueRef<'r>) -> Result { Self::try_from(<&[u8] as Decode>::decode(value)?).map_err(Into::into) } } -impl From<&ControllerUid> for Vec { - fn from(uid: &ControllerUid) -> Vec { +impl From<&EmgauwaUid> for Vec { + fn from(uid: &EmgauwaUid) -> Vec { uid.0.as_bytes().to_vec() } } -impl TryFrom<&str> for ControllerUid { +impl TryFrom<&str> for EmgauwaUid { type Error = uuid::Error; fn try_from(value: &str) -> Result { @@ -88,16 +88,16 @@ impl TryFrom<&str> for ControllerUid { } } -impl TryFrom<&[u8]> for ControllerUid { +impl TryFrom<&[u8]> for EmgauwaUid { type Error = uuid::Error; - fn try_from(value: &[u8]) -> Result { + fn try_from(value: &[u8]) -> Result { Ok(Self(Uuid::from_slice(value)?)) } } -impl From> for ControllerUid { +impl From> for EmgauwaUid { fn from(value: Vec) -> Self { - Self::try_from(value.as_slice()).expect("Failed to parse controller uid from database") + Self::try_from(value.as_slice()).expect("Failed to parse uid from database") } } diff --git a/emgauwa-lib/src/types/mod.rs b/emgauwa-lib/src/types/mod.rs index be2942b..dbbfb77 100644 --- a/emgauwa-lib/src/types/mod.rs +++ b/emgauwa-lib/src/types/mod.rs @@ -1,9 +1,9 @@ -mod controller_uid; +mod emgauwa_uid; mod request; mod schedule_uid; use actix::Message; -pub use controller_uid::ControllerUid; +pub use emgauwa_uid::EmgauwaUid; pub use request::*; pub use schedule_uid::ScheduleUid; use serde_derive::{Deserialize, Serialize}; @@ -24,6 +24,6 @@ pub enum ControllerWsAction { Schedules(Vec), Relays(Vec), Controller(Controller), - RelayStates((ControllerUid, RelayStates)), + RelayStates((EmgauwaUid, RelayStates)), RelayPulse((i64, Option)), }