Add setting to change "midnight" of day

This commit is contained in:
Tobias Reisinger 2024-05-30 02:37:37 +02:00
parent ce7a79d1de
commit 277b159200
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
6 changed files with 30 additions and 28 deletions

View file

@ -5,11 +5,10 @@ use sqlx::Sqlite;
use crate::db::{DbRelay, DbSchedule};
use crate::errors::DatabaseError;
use crate::types::Weekday;
pub struct DbJunctionRelaySchedule {
pub id: i64,
pub weekday: Weekday,
pub weekday: i64,
pub relay_id: i64,
pub schedule_id: i64,
}
@ -32,7 +31,7 @@ impl DbJunctionRelaySchedule {
pub async fn get_junction_by_relay_and_weekday(
conn: &mut PoolConnection<Sqlite>,
relay: &DbRelay,
weekday: Weekday,
weekday: i64,
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
sqlx::query_as!(
DbJunctionRelaySchedule,
@ -65,7 +64,7 @@ impl DbJunctionRelaySchedule {
pub async fn get_schedule(
conn: &mut PoolConnection<Sqlite>,
relay: &DbRelay,
weekday: Weekday,
weekday: i64,
) -> Result<Option<DbSchedule>, DatabaseError> {
sqlx::query_as!(
DbSchedule,
@ -101,7 +100,7 @@ impl DbJunctionRelaySchedule {
conn: &mut PoolConnection<Sqlite>,
relay: &DbRelay,
schedule: &DbSchedule,
weekday: Weekday,
weekday: i64,
) -> Result<DbJunctionRelaySchedule, DatabaseError> {
match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? {
None => sqlx::query_as!(
@ -139,7 +138,7 @@ impl DbJunctionRelaySchedule {
schedules: Vec<&DbSchedule>,
) -> Result<(), DatabaseError> {
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(())
}

View file

@ -4,10 +4,8 @@ use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
use crate::db::{DbController, DbJunctionTag, DbTag};
use crate::errors::DatabaseError;
use crate::types::Weekday;
use crate::utils;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbRelay {

View file

@ -1,6 +1,6 @@
use std::time::Instant;
use chrono::NaiveTime;
use chrono::{NaiveTime, Weekday};
use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection;
@ -9,7 +9,7 @@ use sqlx::Sqlite;
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
use crate::errors::DatabaseError;
use crate::models::FromDbModel;
use crate::types::{EmgauwaUid, RelayState, Weekday};
use crate::types::{EmgauwaUid, RelayState};
use crate::utils;
#[derive(Serialize, Deserialize, Debug, Clone)]
@ -29,7 +29,7 @@ pub struct Relay {
pub pulsing: Option<Instant>,
#[serde(
skip,
default = "utils::get_weekday",
default = "utils::default_weekday",
)]
pub override_schedule_weekday: Weekday,
}
@ -68,7 +68,7 @@ impl FromDbModel for Relay {
is_on,
tags,
pulsing: None,
override_schedule_weekday: Weekday::default(),
override_schedule_weekday: utils::default_weekday(),
})
}
}

View file

@ -1,26 +1,27 @@
use std::time::Instant;
use chrono::{Local, NaiveTime, Timelike};
use chrono::{Local, NaiveTime, Timelike, Weekday};
use crate::types::Weekday;
use crate::utils;
pub struct EmgauwaNow {
pub time: NaiveTime,
pub instant: Instant,
pub weekday: Weekday,
pub midnight: NaiveTime,
}
impl EmgauwaNow {
pub fn now() -> EmgauwaNow {
pub fn now(midnight: &NaiveTime) -> EmgauwaNow {
EmgauwaNow {
time: Local::now().time(),
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()
}
}

View file

@ -16,8 +16,6 @@ use crate::db::DbSchedule;
use crate::errors::EmgauwaError;
use crate::models::{Controller, Relay};
pub type Weekday = i64;
#[derive(Debug, Serialize, Deserialize, Message)]
#[rtype(result = "Result<(), EmgauwaError>")]
pub enum ControllerWsAction {

View file

@ -2,13 +2,13 @@ use std::ffi::CString;
use std::io::{Error, ErrorKind};
use std::str::FromStr;
use chrono::Datelike;
use chrono::{Datelike, NaiveTime, Weekday};
use log::LevelFilter;
use simple_logger::SimpleLogger;
use crate::errors::EmgauwaError;
use crate::settings::Permissions;
use crate::types::{RelayStates, Weekday};
use crate::types::RelayStates;
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
let log_level: LevelFilter = LevelFilter::from_str(level)
@ -92,12 +92,18 @@ fn drop_privileges_user(user: &str) -> Result<(), Error> {
Ok(())
}
pub fn get_weekday() -> Weekday {
(chrono::offset::Local::now()
.date_naive()
.weekday()
.number_from_monday()
- 1) as Weekday
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 printable_relay_states(relay_states: &RelayStates) -> String {