Rename ControllerUid to a more general EmgauwaUid (for macros)
This commit is contained in:
		
							parent
							
								
									07d3322c5a
								
							
						
					
					
						commit
						51aa0d3c99
					
				
					 11 changed files with 55 additions and 57 deletions
				
			
		| 
						 | 
				
			
			@ -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<Sqlite>,
 | 
			
		||||
		filter_uid: &ControllerUid,
 | 
			
		||||
		filter_uid: &EmgauwaUid,
 | 
			
		||||
	) -> Result<Option<DbController>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbController,
 | 
			
		||||
| 
						 | 
				
			
			@ -55,7 +55,7 @@ impl DbController {
 | 
			
		|||
 | 
			
		||||
	pub async fn get_by_uid_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		uid: &ControllerUid,
 | 
			
		||||
		uid: &EmgauwaUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +77,7 @@ impl DbController {
 | 
			
		|||
 | 
			
		||||
	pub async fn delete_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		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<Sqlite>,
 | 
			
		||||
		new_uid: &ControllerUid,
 | 
			
		||||
		new_uid: &EmgauwaUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_relay_count: i64,
 | 
			
		||||
	) -> Result<DbController, DatabaseError> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<DbSchedule>,
 | 
			
		||||
	pub active_schedule: DbSchedule,
 | 
			
		||||
	pub is_on: Option<bool>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	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<D>(deserializer: D) -> Result<Self, D::Error>
 | 
			
		||||
	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<Sqlite> for ControllerUid {
 | 
			
		||||
impl Type<Sqlite> for EmgauwaUid {
 | 
			
		||||
	fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -59,27 +59,27 @@ impl Type<Sqlite> 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 <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
		<Vec<u8> as Encode<Sqlite>>::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, BoxDynError> {
 | 
			
		||||
		Self::try_from(<&[u8] as Decode<Sqlite>>::decode(value)?).map_err(Into::into)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&ControllerUid> for Vec<u8> {
 | 
			
		||||
	fn from(uid: &ControllerUid) -> Vec<u8> {
 | 
			
		||||
impl From<&EmgauwaUid> for Vec<u8> {
 | 
			
		||||
	fn from(uid: &EmgauwaUid) -> Vec<u8> {
 | 
			
		||||
		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<Self, Self::Error> {
 | 
			
		||||
| 
						 | 
				
			
			@ -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<ControllerUid, uuid::Error> {
 | 
			
		||||
	fn try_from(value: &[u8]) -> Result<EmgauwaUid, uuid::Error> {
 | 
			
		||||
		Ok(Self(Uuid::from_slice(value)?))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for ControllerUid {
 | 
			
		||||
impl From<Vec<u8>> for EmgauwaUid {
 | 
			
		||||
	fn from(value: Vec<u8>) -> 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")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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<DbSchedule>),
 | 
			
		||||
	Relays(Vec<Relay>),
 | 
			
		||||
	Controller(Controller),
 | 
			
		||||
	RelayStates((ControllerUid, RelayStates)),
 | 
			
		||||
	RelayStates((EmgauwaUid, RelayStates)),
 | 
			
		||||
	RelayPulse((i64, Option<u32>)),
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue