Rest endpoint is now a little bit more fleshed out

This commit is contained in:
2026-01-19 11:29:37 +01:00
parent 251d245aeb
commit 9b093e6739
6 changed files with 49 additions and 19 deletions

View File

@@ -1,15 +1,17 @@
use std::sync::{Arc, Mutex};
use chrono::Local;
use chrono::{DateTime, Local, Timelike};
use cron_tab::Cron;
use crate::ringer::Ringer;
use crate::types::Alarm;
#[derive(Debug)]
pub struct Scheduler<T: Ringer + 'static> {
ringer: Arc<Mutex<T>>,
cron: Arc<Mutex<Cron<Local>>>,
alarms: Arc<Mutex<Vec<String>>>,
alarms: Arc<Mutex<Vec<Alarm>>>,
}
impl<T: Ringer> Scheduler<T> {
@@ -39,13 +41,15 @@ impl<T: Ringer> Scheduler<T> {
todo!()
}
pub fn add_alarm(&self, hour: &str, minute: &str) -> Result<(), String> {
pub fn add_alarm(&self, time: DateTime<Local>) -> Result<Alarm, String> {
let mut alarms = self.alarms.lock().map_err(|e| e.to_string())?;
let cron_schedule = format!("{} {} {} * * *", "*", minute, hour);
alarms.push(cron_schedule.clone());
let cron_schedule = format!("{} {} {} * * *", "*", time.minute(), time.hour());
let alarm = Alarm::new(true, time);
alarms.push(alarm.clone());
self.schedule(&cron_schedule).map_err(|e| e.to_string())?;
println!("Added alarm {}", cron_schedule);
Ok(())
Ok(alarm)
}
pub fn start(&self) {