Compare commits
2 commits
929985c64a
...
277b159200
Author | SHA1 | Date | |
---|---|---|---|
277b159200 | |||
ce7a79d1de |
8 changed files with 47 additions and 50 deletions
|
@ -5,11 +5,10 @@ use sqlx::Sqlite;
|
||||||
|
|
||||||
use crate::db::{DbRelay, DbSchedule};
|
use crate::db::{DbRelay, DbSchedule};
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::types::Weekday;
|
|
||||||
|
|
||||||
pub struct DbJunctionRelaySchedule {
|
pub struct DbJunctionRelaySchedule {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub weekday: Weekday,
|
pub weekday: i64,
|
||||||
pub relay_id: i64,
|
pub relay_id: i64,
|
||||||
pub schedule_id: i64,
|
pub schedule_id: i64,
|
||||||
}
|
}
|
||||||
|
@ -32,7 +31,7 @@ impl DbJunctionRelaySchedule {
|
||||||
pub async fn get_junction_by_relay_and_weekday(
|
pub async fn get_junction_by_relay_and_weekday(
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
relay: &DbRelay,
|
relay: &DbRelay,
|
||||||
weekday: Weekday,
|
weekday: i64,
|
||||||
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
|
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
DbJunctionRelaySchedule,
|
DbJunctionRelaySchedule,
|
||||||
|
@ -65,7 +64,7 @@ impl DbJunctionRelaySchedule {
|
||||||
pub async fn get_schedule(
|
pub async fn get_schedule(
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
relay: &DbRelay,
|
relay: &DbRelay,
|
||||||
weekday: Weekday,
|
weekday: i64,
|
||||||
) -> Result<Option<DbSchedule>, DatabaseError> {
|
) -> Result<Option<DbSchedule>, DatabaseError> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
DbSchedule,
|
DbSchedule,
|
||||||
|
@ -101,7 +100,7 @@ impl DbJunctionRelaySchedule {
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
relay: &DbRelay,
|
relay: &DbRelay,
|
||||||
schedule: &DbSchedule,
|
schedule: &DbSchedule,
|
||||||
weekday: Weekday,
|
weekday: i64,
|
||||||
) -> Result<DbJunctionRelaySchedule, DatabaseError> {
|
) -> Result<DbJunctionRelaySchedule, DatabaseError> {
|
||||||
match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? {
|
match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? {
|
||||||
None => sqlx::query_as!(
|
None => sqlx::query_as!(
|
||||||
|
@ -139,7 +138,7 @@ impl DbJunctionRelaySchedule {
|
||||||
schedules: Vec<&DbSchedule>,
|
schedules: Vec<&DbSchedule>,
|
||||||
) -> Result<(), DatabaseError> {
|
) -> Result<(), DatabaseError> {
|
||||||
for (weekday, schedule) in schedules.iter().enumerate() {
|
for (weekday, schedule) in schedules.iter().enumerate() {
|
||||||
Self::set_schedule(conn, relay, schedule, weekday as Weekday).await?;
|
Self::set_schedule(conn, relay, schedule, weekday as i64).await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,8 @@ use serde_derive::{Deserialize, Serialize};
|
||||||
use sqlx::pool::PoolConnection;
|
use sqlx::pool::PoolConnection;
|
||||||
use sqlx::Sqlite;
|
use sqlx::Sqlite;
|
||||||
|
|
||||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
|
use crate::db::{DbController, DbJunctionTag, DbTag};
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::types::Weekday;
|
|
||||||
use crate::utils;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DbRelay {
|
pub struct DbRelay {
|
||||||
|
@ -163,14 +161,4 @@ impl DbRelay {
|
||||||
.await?
|
.await?
|
||||||
.ok_or(DatabaseError::NotFound)
|
.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ impl Controller {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.filter_map(|r| {
|
.filter_map(|r| {
|
||||||
r.reload_active_schedule(now.weekday);
|
r.reload_active_schedule(now.weekday);
|
||||||
r.active_schedule.get_next_time(&now.time)
|
r.get_next_time(&now.time)
|
||||||
})
|
})
|
||||||
.min()
|
.min()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use chrono::NaiveTime;
|
use chrono::{NaiveTime, Weekday};
|
||||||
use futures::executor::block_on;
|
use futures::executor::block_on;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use sqlx::pool::PoolConnection;
|
use sqlx::pool::PoolConnection;
|
||||||
|
@ -9,7 +9,7 @@ use sqlx::Sqlite;
|
||||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::models::FromDbModel;
|
use crate::models::FromDbModel;
|
||||||
use crate::types::{EmgauwaUid, RelayState, Weekday};
|
use crate::types::{EmgauwaUid, RelayState};
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
@ -19,7 +19,7 @@ pub struct Relay {
|
||||||
pub controller: DbController,
|
pub controller: DbController,
|
||||||
pub controller_id: EmgauwaUid,
|
pub controller_id: EmgauwaUid,
|
||||||
pub schedules: Vec<DbSchedule>,
|
pub schedules: Vec<DbSchedule>,
|
||||||
pub active_schedule: DbSchedule,
|
pub active_schedule: Option<DbSchedule>,
|
||||||
pub override_schedule: Option<DbSchedule>,
|
pub override_schedule: Option<DbSchedule>,
|
||||||
pub is_on: Option<bool>,
|
pub is_on: Option<bool>,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
|
@ -29,7 +29,7 @@ pub struct Relay {
|
||||||
pub pulsing: Option<Instant>,
|
pub pulsing: Option<Instant>,
|
||||||
#[serde(
|
#[serde(
|
||||||
skip,
|
skip,
|
||||||
default = "utils::get_weekday",
|
default = "utils::default_weekday",
|
||||||
)]
|
)]
|
||||||
pub override_schedule_weekday: Weekday,
|
pub override_schedule_weekday: Weekday,
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,6 @@ impl FromDbModel for Relay {
|
||||||
let controller_id = cache.uid.clone();
|
let controller_id = cache.uid.clone();
|
||||||
|
|
||||||
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
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;
|
let is_on = None;
|
||||||
|
|
||||||
|
@ -64,12 +63,12 @@ impl FromDbModel for Relay {
|
||||||
controller: cache,
|
controller: cache,
|
||||||
controller_id,
|
controller_id,
|
||||||
schedules,
|
schedules,
|
||||||
active_schedule,
|
active_schedule: None,
|
||||||
override_schedule: None,
|
override_schedule: None,
|
||||||
is_on,
|
is_on,
|
||||||
tags,
|
tags,
|
||||||
pulsing: None,
|
pulsing: None,
|
||||||
override_schedule_weekday: Weekday::default(),
|
override_schedule_weekday: utils::default_weekday(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,17 +78,23 @@ impl Relay {
|
||||||
self.r = block_on(self.r.reload(conn))?;
|
self.r = block_on(self.r.reload(conn))?;
|
||||||
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
|
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
|
||||||
|
|
||||||
self.reload_active_schedule(utils::get_weekday());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_on(&self, now: &NaiveTime) -> bool {
|
pub fn is_on(&self, now: &NaiveTime) -> bool {
|
||||||
self.active_schedule.is_on(now)
|
if let Some(active_schedule) = &self.active_schedule {
|
||||||
|
active_schedule.is_on(now)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
|
pub fn get_next_time(&self, now: &NaiveTime) -> Option<NaiveTime> {
|
||||||
self.active_schedule.get_next_time(now)
|
if let Some(active_schedule) = &self.active_schedule {
|
||||||
|
active_schedule.get_next_time(now)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_pulsing(&mut self, now: &Instant) -> Option<Instant> {
|
pub fn check_pulsing(&mut self, now: &Instant) -> Option<Instant> {
|
||||||
|
@ -109,7 +114,7 @@ impl Relay {
|
||||||
pub fn reload_active_schedule(&mut self, weekday: Weekday) {
|
pub fn reload_active_schedule(&mut self, weekday: Weekday) {
|
||||||
if let Some(schedule) = &self.override_schedule {
|
if let Some(schedule) = &self.override_schedule {
|
||||||
if self.override_schedule_weekday == weekday {
|
if self.override_schedule_weekday == weekday {
|
||||||
self.active_schedule = schedule.clone();
|
self.active_schedule = Some(schedule.clone());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.override_schedule_weekday != weekday {
|
if self.override_schedule_weekday != weekday {
|
||||||
|
@ -117,11 +122,11 @@ impl Relay {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.active_schedule = self.schedules.get(weekday as usize).unwrap().clone()
|
self.active_schedule = Some(self.schedules.get(weekday as usize).unwrap().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_state(&mut self, state: &RelayState) {
|
pub fn apply_state(&mut self, state: &RelayState) {
|
||||||
self.active_schedule = state.active_schedule.clone();
|
self.active_schedule.clone_from(&state.active_schedule);
|
||||||
self.override_schedule.clone_from(&state.override_schedule);
|
self.override_schedule.clone_from(&state.override_schedule);
|
||||||
self.is_on = state.is_on;
|
self.is_on = state.is_on;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,27 @@
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use chrono::{Local, NaiveTime, Timelike};
|
use chrono::{Local, NaiveTime, Timelike, Weekday};
|
||||||
|
|
||||||
use crate::types::Weekday;
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
pub struct EmgauwaNow {
|
pub struct EmgauwaNow {
|
||||||
pub time: NaiveTime,
|
pub time: NaiveTime,
|
||||||
pub instant: Instant,
|
pub instant: Instant,
|
||||||
pub weekday: Weekday,
|
pub weekday: Weekday,
|
||||||
|
pub midnight: NaiveTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EmgauwaNow {
|
impl EmgauwaNow {
|
||||||
pub fn now() -> EmgauwaNow {
|
pub fn now(midnight: &NaiveTime) -> EmgauwaNow {
|
||||||
EmgauwaNow {
|
EmgauwaNow {
|
||||||
time: Local::now().time(),
|
time: Local::now().time(),
|
||||||
instant: Instant::now(),
|
instant: Instant::now(),
|
||||||
weekday: utils::get_weekday(),
|
weekday: utils::get_weekday(midnight),
|
||||||
|
midnight: *midnight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn time_in_s(&self) -> u32 {
|
pub fn num_seconds_from_midnight(&self) -> u32 {
|
||||||
self.time.num_seconds_from_midnight()
|
self.time.num_seconds_from_midnight()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,6 @@ use crate::db::DbSchedule;
|
||||||
use crate::errors::EmgauwaError;
|
use crate::errors::EmgauwaError;
|
||||||
use crate::models::{Controller, Relay};
|
use crate::models::{Controller, Relay};
|
||||||
|
|
||||||
pub type Weekday = i64;
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Message)]
|
#[derive(Debug, Serialize, Deserialize, Message)]
|
||||||
#[rtype(result = "Result<(), EmgauwaError>")]
|
#[rtype(result = "Result<(), EmgauwaError>")]
|
||||||
pub enum ControllerWsAction {
|
pub enum ControllerWsAction {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::models::Relay;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct RelayState {
|
pub struct RelayState {
|
||||||
pub active_schedule: DbSchedule,
|
pub active_schedule: Option<DbSchedule>,
|
||||||
pub override_schedule: Option<DbSchedule>,
|
pub override_schedule: Option<DbSchedule>,
|
||||||
pub is_on: Option<bool>
|
pub is_on: Option<bool>
|
||||||
}
|
}
|
||||||
|
|
22
src/utils.rs
22
src/utils.rs
|
@ -2,13 +2,13 @@ use std::ffi::CString;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::Datelike;
|
use chrono::{Datelike, NaiveTime, Weekday};
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
|
|
||||||
use crate::errors::EmgauwaError;
|
use crate::errors::EmgauwaError;
|
||||||
use crate::settings::Permissions;
|
use crate::settings::Permissions;
|
||||||
use crate::types::{RelayStates, Weekday};
|
use crate::types::RelayStates;
|
||||||
|
|
||||||
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
|
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
|
||||||
let log_level: LevelFilter = LevelFilter::from_str(level)
|
let log_level: LevelFilter = LevelFilter::from_str(level)
|
||||||
|
@ -92,12 +92,18 @@ fn drop_privileges_user(user: &str) -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_weekday() -> Weekday {
|
pub fn get_weekday(midnight: &NaiveTime) -> Weekday {
|
||||||
(chrono::offset::Local::now()
|
let dt = chrono::offset::Local::now().naive_local();
|
||||||
.date_naive()
|
let weekday = dt.weekday();
|
||||||
.weekday()
|
if dt.time().lt(midnight) {
|
||||||
.number_from_monday()
|
weekday.pred()
|
||||||
- 1) as Weekday
|
} else {
|
||||||
|
weekday
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_weekday() -> Weekday {
|
||||||
|
Weekday::Mon
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn printable_relay_states(relay_states: &RelayStates) -> String {
|
pub fn printable_relay_states(relay_states: &RelayStates) -> String {
|
||||||
|
|
Loading…
Reference in a new issue