Compare commits
No commits in common. "41cc9e0622b69868eb13db843b1323337cb7a544" and "f26e66d68796bcd3e391a36100c12c3bb9fa210a" have entirely different histories.
41cc9e0622
...
f26e66d687
27 changed files with 102 additions and 197 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Cargo.lock
generated
BIN
Cargo.lock
generated
Binary file not shown.
|
@ -13,7 +13,6 @@ actix-web-actors = "4.2"
|
|||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_with = "3.8"
|
||||
|
||||
simple_logger = "5.0"
|
||||
log = "0.4"
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
DROP VIEW v_relays;
|
|
@ -1,8 +0,0 @@
|
|||
CREATE VIEW v_relays
|
||||
AS
|
||||
SELECT
|
||||
relays.*,
|
||||
controllers.uid AS controller_uid
|
||||
FROM
|
||||
relays
|
||||
INNER JOIN controllers ON controllers.id = relays.controller_id;
|
|
@ -158,7 +158,7 @@ impl DbController {
|
|||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
DbRelay,
|
||||
"SELECT * FROM v_relays WHERE v_relays.controller_id = ?",
|
||||
"SELECT * FROM relays WHERE controller_id = ?",
|
||||
self.id
|
||||
)
|
||||
.fetch_all(conn.deref_mut())
|
||||
|
|
|
@ -5,10 +5,11 @@ use sqlx::Sqlite;
|
|||
|
||||
use crate::db::{DbRelay, DbSchedule};
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::types::Weekday;
|
||||
|
||||
pub struct DbJunctionRelaySchedule {
|
||||
pub id: i64,
|
||||
pub weekday: i64,
|
||||
pub weekday: Weekday,
|
||||
pub relay_id: i64,
|
||||
pub schedule_id: i64,
|
||||
}
|
||||
|
@ -31,7 +32,7 @@ impl DbJunctionRelaySchedule {
|
|||
pub async fn get_junction_by_relay_and_weekday(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
relay: &DbRelay,
|
||||
weekday: i64,
|
||||
weekday: Weekday,
|
||||
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
DbJunctionRelaySchedule,
|
||||
|
@ -50,8 +51,8 @@ impl DbJunctionRelaySchedule {
|
|||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
DbRelay,
|
||||
r#"SELECT v_relays.* FROM v_relays INNER JOIN junction_relay_schedule
|
||||
ON junction_relay_schedule.relay_id = v_relays.id
|
||||
r#"SELECT relays.* FROM relays INNER JOIN junction_relay_schedule
|
||||
ON junction_relay_schedule.relay_id = relays.id
|
||||
WHERE junction_relay_schedule.schedule_id = ?
|
||||
ORDER BY junction_relay_schedule.weekday"#,
|
||||
schedule.id
|
||||
|
@ -64,7 +65,7 @@ impl DbJunctionRelaySchedule {
|
|||
pub async fn get_schedule(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
relay: &DbRelay,
|
||||
weekday: i64,
|
||||
weekday: Weekday,
|
||||
) -> Result<Option<DbSchedule>, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
DbSchedule,
|
||||
|
@ -100,7 +101,7 @@ impl DbJunctionRelaySchedule {
|
|||
conn: &mut PoolConnection<Sqlite>,
|
||||
relay: &DbRelay,
|
||||
schedule: &DbSchedule,
|
||||
weekday: i64,
|
||||
weekday: Weekday,
|
||||
) -> Result<DbJunctionRelaySchedule, DatabaseError> {
|
||||
match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? {
|
||||
None => sqlx::query_as!(
|
||||
|
@ -138,7 +139,7 @@ impl DbJunctionRelaySchedule {
|
|||
schedules: Vec<&DbSchedule>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
for (weekday, schedule) in schedules.iter().enumerate() {
|
||||
Self::set_schedule(conn, relay, schedule, weekday as i64).await?;
|
||||
Self::set_schedule(conn, relay, schedule, weekday as Weekday).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use chrono::{NaiveTime, Timelike};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Decode, Encode, Sqlite, Type};
|
||||
use sqlx::database::HasArguments;
|
||||
use sqlx::encode::IsNull;
|
||||
use sqlx::error::BoxDynError;
|
||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
|
||||
use sqlx::{Decode, Encode, Sqlite, Type};
|
||||
|
||||
use crate::db::DbPeriods;
|
||||
|
||||
|
|
|
@ -4,9 +4,10 @@ use serde_derive::{Deserialize, Serialize};
|
|||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::{DbController, DbJunctionTag, DbTag};
|
||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::types::EmgauwaUid;
|
||||
use crate::types::Weekday;
|
||||
use crate::utils;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DbRelay {
|
||||
|
@ -14,15 +15,13 @@ pub struct DbRelay {
|
|||
pub id: i64,
|
||||
pub name: String,
|
||||
pub number: i64,
|
||||
#[serde(rename = "controller_id")]
|
||||
pub controller_uid: EmgauwaUid,
|
||||
#[serde(skip)]
|
||||
pub controller_id: i64,
|
||||
}
|
||||
|
||||
impl DbRelay {
|
||||
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM v_relays")
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays")
|
||||
.fetch_all(conn.deref_mut())
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
|
@ -32,7 +31,7 @@ impl DbRelay {
|
|||
conn: &mut PoolConnection<Sqlite>,
|
||||
id: i64,
|
||||
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM v_relays WHERE v_relays.id = ?", id)
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
|
@ -45,7 +44,7 @@ impl DbRelay {
|
|||
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
DbRelay,
|
||||
"SELECT * FROM v_relays WHERE v_relays.controller_id = ? AND v_relays.number = ?",
|
||||
"SELECT * FROM relays WHERE controller_id = ? AND number = ?",
|
||||
controller.id,
|
||||
number
|
||||
)
|
||||
|
@ -73,7 +72,7 @@ impl DbRelay {
|
|||
conn: &mut PoolConnection<Sqlite>,
|
||||
tag: &DbTag,
|
||||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||
sqlx::query_as!(DbRelay, "SELECT v_relays.* FROM v_relays INNER JOIN junction_tag ON junction_tag.relay_id = v_relays.id WHERE junction_tag.tag_id = ?", tag.id)
|
||||
sqlx::query_as!(DbRelay, "SELECT relay.* FROM relays AS relay INNER JOIN junction_tag ON junction_tag.relay_id = relay.id WHERE junction_tag.tag_id = ?", tag.id)
|
||||
.fetch_all(conn.deref_mut())
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
|
@ -85,25 +84,16 @@ impl DbRelay {
|
|||
new_number: i64,
|
||||
new_controller: &DbController,
|
||||
) -> Result<DbRelay, DatabaseError> {
|
||||
let result = sqlx::query!(
|
||||
"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?)",
|
||||
sqlx::query_as!(
|
||||
DbRelay,
|
||||
"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
|
||||
new_name,
|
||||
new_number,
|
||||
new_controller.id,
|
||||
)
|
||||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
let last_insert_id = result.last_insert_rowid();
|
||||
|
||||
sqlx::query_as!(
|
||||
DbRelay,
|
||||
"SELECT * FROM v_relays WHERE id = ?",
|
||||
last_insert_id
|
||||
)
|
||||
.fetch_one(conn.deref_mut())
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
||||
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
|
||||
|
@ -173,4 +163,14 @@ impl DbRelay {
|
|||
.await?
|
||||
.ok_or(DatabaseError::NotFound)
|
||||
}
|
||||
|
||||
pub async fn get_active_schedule(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
) -> Result<DbSchedule, DatabaseError> {
|
||||
let weekday = utils::get_weekday();
|
||||
DbJunctionRelaySchedule::get_schedule(conn, self, weekday as Weekday)
|
||||
.await?
|
||||
.ok_or(DatabaseError::NotFound)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,9 +47,7 @@ impl From<&EmgauwaError> for String {
|
|||
EmgauwaError::Database(err) => String::from(err),
|
||||
EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),
|
||||
EmgauwaError::Internal(_) => String::from("internal error"),
|
||||
EmgauwaError::Connection(uid) => {
|
||||
format!("unable to connect to controller with uid: {}", uid)
|
||||
}
|
||||
EmgauwaError::Connection(uid) => format!("unable to connect to controller with uid: {}", uid),
|
||||
EmgauwaError::Other(err) => format!("other error: {}", err),
|
||||
EmgauwaError::Hardware(err) => format!("hardware error: {}", err),
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@ use sqlx::Sqlite;
|
|||
|
||||
use crate::db::DbController;
|
||||
use crate::errors::{DatabaseError, EmgauwaError};
|
||||
use crate::models::{convert_db_list, FromDbModel, Relay};
|
||||
use crate::types::{EmgauwaNow, RelayState, RelayStates};
|
||||
use crate::models::{convert_db_list_cache, FromDbModel, Relay};
|
||||
use crate::types::RelayStates;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
|
||||
pub struct Controller {
|
||||
|
@ -28,7 +28,7 @@ impl FromDbModel for Controller {
|
|||
db_model: Self::DbModel,
|
||||
) -> Result<Self, DatabaseError> {
|
||||
let relays_db = block_on(db_model.get_relays(conn))?;
|
||||
let cache = convert_db_list(conn, relays_db)?;
|
||||
let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?;
|
||||
Self::from_db_model_cache(conn, db_model, cache)
|
||||
}
|
||||
|
||||
|
@ -57,20 +57,19 @@ impl Controller {
|
|||
self.relays
|
||||
.iter_mut()
|
||||
.zip(relay_states.iter())
|
||||
.for_each(|(relay, state)| relay.apply_state(state));
|
||||
.for_each(|(relay, is_on)| {
|
||||
relay.is_on = *is_on;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_relay_states(&self) -> RelayStates {
|
||||
self.relays.iter().map(RelayState::from).collect()
|
||||
self.relays.iter().map(|r| r.is_on).collect()
|
||||
}
|
||||
|
||||
pub fn check_next_time(&mut self, now: &EmgauwaNow) -> Option<NaiveTime> {
|
||||
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
|
||||
self.relays
|
||||
.iter_mut()
|
||||
.filter_map(|r| {
|
||||
r.reload_active_schedule(now.weekday);
|
||||
r.get_next_time(&now.time)
|
||||
})
|
||||
.iter()
|
||||
.filter_map(|r| r.active_schedule.get_next_time(now))
|
||||
.min()
|
||||
}
|
||||
|
||||
|
@ -81,8 +80,6 @@ impl Controller {
|
|||
.find(|r| r.r.number == relay_num)
|
||||
.ok_or(EmgauwaError::Other(String::from("Relay not found")))?;
|
||||
|
||||
log::debug!("Pulsing relay {} until {:?}", relay_num, until);
|
||||
|
||||
relay.pulsing = Some(until);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -3,13 +3,13 @@ use serde_derive::{Deserialize, Serialize};
|
|||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::{DbJunctionRelaySchedule, DbMacroAction, DbSchedule};
|
||||
use crate::db::{DbJunctionRelaySchedule, DbMacroAction};
|
||||
use crate::errors::{DatabaseError, EmgauwaError};
|
||||
use crate::models::{FromDbModel, Relay};
|
||||
use crate::models::{FromDbModel, Relay, Schedule};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct MacroAction {
|
||||
pub schedule: DbSchedule,
|
||||
pub schedule: Schedule,
|
||||
pub relay: Relay,
|
||||
pub weekday: i64,
|
||||
}
|
||||
|
@ -30,7 +30,8 @@ impl FromDbModel for MacroAction {
|
|||
db_model: Self::DbModel,
|
||||
_cache: Self::DbModelCache,
|
||||
) -> Result<Self, DatabaseError> {
|
||||
let schedule = block_on(db_model.get_schedule(conn))?;
|
||||
let schedule_db = block_on(db_model.get_schedule(conn))?;
|
||||
let schedule = Schedule::from_db_model(conn, schedule_db)?;
|
||||
|
||||
let relay_db = block_on(db_model.get_relay(conn))?;
|
||||
let relay = Relay::from_db_model(conn, relay_db)?;
|
||||
|
@ -47,7 +48,7 @@ impl FromDbModel for MacroAction {
|
|||
|
||||
impl MacroAction {
|
||||
pub async fn execute(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> {
|
||||
DbJunctionRelaySchedule::set_schedule(conn, &self.relay.r, &self.schedule, self.weekday)
|
||||
DbJunctionRelaySchedule::set_schedule(conn, &self.relay.r, &self.schedule.s, self.weekday)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,65 +1,66 @@
|
|||
use std::time::Instant;
|
||||
|
||||
use chrono::{NaiveTime, Weekday};
|
||||
use chrono::NaiveTime;
|
||||
use futures::executor::block_on;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::{DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::FromDbModel;
|
||||
use crate::types::RelayState;
|
||||
use crate::utils;
|
||||
use crate::types::EmgauwaUid;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Relay {
|
||||
#[serde(flatten)]
|
||||
pub r: DbRelay,
|
||||
pub controller: DbController,
|
||||
pub controller_id: EmgauwaUid,
|
||||
pub schedules: Vec<DbSchedule>,
|
||||
pub active_schedule: Option<DbSchedule>,
|
||||
pub override_schedule: Option<DbSchedule>,
|
||||
pub active_schedule: DbSchedule,
|
||||
pub is_on: Option<bool>,
|
||||
pub tags: Vec<String>,
|
||||
|
||||
// for internal use only.
|
||||
#[serde(skip)]
|
||||
pub pulsing: Option<Instant>,
|
||||
#[serde(skip, default = "utils::default_weekday")]
|
||||
pub override_schedule_weekday: Weekday,
|
||||
}
|
||||
|
||||
impl FromDbModel for Relay {
|
||||
type DbModel = DbRelay;
|
||||
type DbModelCache = ();
|
||||
type DbModelCache = DbController;
|
||||
|
||||
fn from_db_model(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
db_model: Self::DbModel,
|
||||
) -> Result<Self, DatabaseError> {
|
||||
Self::from_db_model_cache(conn, db_model, ())
|
||||
let cache = block_on(db_model.get_controller(conn))?;
|
||||
Self::from_db_model_cache(conn, db_model, cache)
|
||||
}
|
||||
|
||||
fn from_db_model_cache(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
db_model: Self::DbModel,
|
||||
_cache: Self::DbModelCache,
|
||||
cache: Self::DbModelCache,
|
||||
) -> Result<Self, DatabaseError> {
|
||||
let tags = block_on(db_model.get_tags(conn))?;
|
||||
let controller_id = cache.uid.clone();
|
||||
|
||||
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
||||
let active_schedule = block_on(db_model.get_active_schedule(conn))?;
|
||||
|
||||
let is_on = None;
|
||||
|
||||
Ok(Relay {
|
||||
r: db_model,
|
||||
controller: cache,
|
||||
controller_id,
|
||||
schedules,
|
||||
active_schedule: None,
|
||||
override_schedule: None,
|
||||
active_schedule,
|
||||
is_on,
|
||||
tags,
|
||||
pulsing: None,
|
||||
override_schedule_weekday: utils::default_weekday(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -68,24 +69,25 @@ impl Relay {
|
|||
pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
|
||||
self.r = block_on(self.r.reload(conn))?;
|
||||
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
|
||||
self.reload_active_schedule(conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_on(&self, now: &NaiveTime) -> bool {
|
||||
if let Some(active_schedule) = &self.active_schedule {
|
||||
active_schedule.is_on(now)
|
||||
} else {
|
||||
false
|
||||
pub fn reload_active_schedule(
|
||||
&mut self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
self.active_schedule = block_on(self.r.get_active_schedule(conn))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_on(&self, now: &NaiveTime) -> bool {
|
||||
self.active_schedule.is_on(now)
|
||||
}
|
||||
|
||||
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
|
||||
if let Some(active_schedule) = &self.active_schedule {
|
||||
active_schedule.get_next_time(now)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
self.active_schedule.get_next_time(now)
|
||||
}
|
||||
|
||||
pub fn check_pulsing(&mut self, now: &Instant) -> Option<Instant> {
|
||||
|
@ -101,30 +103,4 @@ impl Relay {
|
|||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reload_active_schedule(&mut self, weekday: Weekday) {
|
||||
if let Some(schedule) = &self.override_schedule {
|
||||
if self.override_schedule_weekday == weekday {
|
||||
self.active_schedule = Some(schedule.clone());
|
||||
return;
|
||||
}
|
||||
if self.override_schedule_weekday != weekday {
|
||||
self.override_schedule = None;
|
||||
}
|
||||
}
|
||||
|
||||
self.active_schedule = Some(self.schedules.get(weekday as usize).unwrap().clone())
|
||||
}
|
||||
|
||||
pub fn apply_state(&mut self, state: &RelayState) {
|
||||
self.active_schedule.clone_from(&state.active_schedule);
|
||||
self.override_schedule.clone_from(&state.override_schedule);
|
||||
self.is_on = state.is_on;
|
||||
}
|
||||
|
||||
pub fn find_and_apply_state(&mut self, stated_relays: &[Relay]) {
|
||||
if let Some(stated_relay) = stated_relays.iter().find(|r| r.r.id == self.r.id) {
|
||||
self.apply_state(&stated_relay.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ pub struct Server {
|
|||
#[allow(unused)]
|
||||
pub struct Logging {
|
||||
pub level: String,
|
||||
pub file: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
|
@ -39,6 +40,7 @@ impl Default for Logging {
|
|||
fn default() -> Self {
|
||||
Logging {
|
||||
level: String::from("info"),
|
||||
file: String::from("stdout"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
use std::time::Instant;
|
||||
|
||||
use chrono::{Local, NaiveTime, Timelike, Weekday};
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub struct EmgauwaNow {
|
||||
pub time: NaiveTime,
|
||||
pub instant: Instant,
|
||||
pub weekday: Weekday,
|
||||
pub midnight: NaiveTime,
|
||||
}
|
||||
|
||||
impl EmgauwaNow {
|
||||
pub fn now(midnight: &NaiveTime) -> EmgauwaNow {
|
||||
EmgauwaNow {
|
||||
time: Local::now().time(),
|
||||
instant: Instant::now(),
|
||||
weekday: utils::get_weekday(midnight),
|
||||
midnight: *midnight,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn num_seconds_from_midnight(&self) -> u32 {
|
||||
self.time.num_seconds_from_midnight()
|
||||
}
|
||||
}
|
|
@ -1,13 +1,9 @@
|
|||
mod emgauwa_now;
|
||||
mod emgauwa_uid;
|
||||
mod request;
|
||||
mod schedule_uid;
|
||||
mod relay_state;
|
||||
|
||||
use actix::Message;
|
||||
pub use emgauwa_now::EmgauwaNow;
|
||||
pub use emgauwa_uid::EmgauwaUid;
|
||||
pub use relay_state::{RelayState, RelayStates};
|
||||
pub use request::*;
|
||||
pub use schedule_uid::ScheduleUid;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
@ -16,6 +12,10 @@ use crate::db::DbSchedule;
|
|||
use crate::errors::EmgauwaError;
|
||||
use crate::models::{Controller, Relay};
|
||||
|
||||
pub type Weekday = i64;
|
||||
|
||||
pub type RelayStates = Vec<Option<bool>>;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Message)]
|
||||
#[rtype(result = "Result<(), EmgauwaError>")]
|
||||
pub enum ControllerWsAction {
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
use serde_derive::{Deserialize, Serialize};
|
||||
use crate::db::DbSchedule;
|
||||
use crate::models::Relay;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RelayState {
|
||||
pub active_schedule: Option<DbSchedule>,
|
||||
pub override_schedule: Option<DbSchedule>,
|
||||
pub is_on: Option<bool>
|
||||
}
|
||||
|
||||
pub type RelayStates = Vec<RelayState>;
|
||||
|
||||
impl From<&Relay> for RelayState {
|
||||
fn from(relay: &Relay) -> Self {
|
||||
RelayState {
|
||||
active_schedule: relay.active_schedule.clone(),
|
||||
override_schedule: relay.override_schedule.clone(),
|
||||
is_on: relay.is_on
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,12 +23,7 @@ pub struct RequestScheduleUpdate {
|
|||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestRelayUpdate {
|
||||
pub name: Option<String>,
|
||||
#[serde(
|
||||
default, // <- important for deserialization
|
||||
skip_serializing_if = "Option::is_none", // <- important for serialization
|
||||
with = "::serde_with::rust::double_option",
|
||||
)]
|
||||
pub override_schedule: Option<Option<RequestScheduleId>>,
|
||||
pub active_schedule: Option<RequestScheduleId>,
|
||||
pub schedules: Option<Vec<RequestScheduleId>>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
}
|
||||
|
|
32
src/utils.rs
32
src/utils.rs
|
@ -2,17 +2,17 @@ use std::ffi::CString;
|
|||
use std::io::{Error, ErrorKind};
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::{Datelike, NaiveTime, Weekday};
|
||||
use chrono::Datelike;
|
||||
use log::LevelFilter;
|
||||
use simple_logger::SimpleLogger;
|
||||
|
||||
use crate::errors::EmgauwaError;
|
||||
use crate::settings::{Logging, Permissions};
|
||||
use crate::types::RelayStates;
|
||||
use crate::settings::Permissions;
|
||||
use crate::types::{RelayStates, Weekday};
|
||||
|
||||
pub fn init_logging(logging: &Logging) -> Result<(), EmgauwaError> {
|
||||
let log_level: LevelFilter = LevelFilter::from_str(&logging.level)
|
||||
.map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", logging.level)))?;
|
||||
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
|
||||
let log_level: LevelFilter = LevelFilter::from_str(level)
|
||||
.map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", level)))?;
|
||||
log::trace!("Log level set to {:?}", log_level);
|
||||
|
||||
SimpleLogger::new()
|
||||
|
@ -92,24 +92,18 @@ fn drop_privileges_user(user: &str) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_weekday(midnight: &NaiveTime) -> Weekday {
|
||||
let dt = chrono::offset::Local::now().naive_local();
|
||||
let weekday = dt.weekday();
|
||||
if dt.time().lt(midnight) {
|
||||
weekday.pred()
|
||||
} else {
|
||||
weekday
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_weekday() -> Weekday {
|
||||
Weekday::Mon
|
||||
pub fn get_weekday() -> Weekday {
|
||||
(chrono::offset::Local::now()
|
||||
.date_naive()
|
||||
.weekday()
|
||||
.number_from_monday()
|
||||
- 1) as Weekday
|
||||
}
|
||||
|
||||
pub fn printable_relay_states(relay_states: &RelayStates) -> String {
|
||||
let mut relay_debug = String::new();
|
||||
relay_states.iter().for_each(|state| {
|
||||
relay_debug.push_str(match state.is_on {
|
||||
relay_debug.push_str(match state {
|
||||
Some(true) => "+",
|
||||
Some(false) => "-",
|
||||
None => "?",
|
||||
|
|
Loading…
Reference in a new issue