Add setting to change "midnight" of day
This commit is contained in:
parent
ce7a79d1de
commit
277b159200
6 changed files with 30 additions and 28 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 {
|
||||||
|
|
|
@ -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)]
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ impl FromDbModel for Relay {
|
||||||
is_on,
|
is_on,
|
||||||
tags,
|
tags,
|
||||||
pulsing: None,
|
pulsing: None,
|
||||||
override_schedule_weekday: Weekday::default(),
|
override_schedule_weekday: utils::default_weekday(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
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