This commit is contained in:
2026-01-19 09:42:16 +01:00
parent 45c332e610
commit 251d245aeb
6 changed files with 43 additions and 89 deletions

View File

@@ -9,11 +9,16 @@ use crate::ringer::Ringer;
pub struct Scheduler<T: Ringer + 'static> {
ringer: Arc<Mutex<T>>,
cron: Arc<Mutex<Cron<Local>>>,
alarms: Arc<Mutex<Vec<String>>>,
}
impl<T: Ringer> Scheduler<T> {
pub fn new(ringer: Arc<Mutex<T>>, cron: Arc<Mutex<Cron<Local>>>) -> Self {
Self { ringer, cron }
Self {
ringer,
cron,
alarms: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn schedule(&self, cron_schedule: &str) -> Result<(), String> {
@@ -34,6 +39,15 @@ impl<T: Ringer> Scheduler<T> {
todo!()
}
pub fn add_alarm(&self, hour: &str, minute: &str) -> Result<(), String> {
let mut alarms = self.alarms.lock().map_err(|e| e.to_string())?;
let cron_schedule = format!("{} {} {} * * *", "*", minute, hour);
alarms.push(cron_schedule.clone());
println!("Added alarm {}", cron_schedule);
Ok(())
}
pub fn start(&self) {
self.cron.lock().expect("Failed to lock cron").start();
}