diff --git a/' b/' deleted file mode 100644 index 5985589..0000000 --- a/' +++ /dev/null @@ -1,83 +0,0 @@ -use std::{ - sync::{Arc, Mutex}, - thread, -}; - -use chrono::Local; -use cron_tab::Cron; -use gpio_cdev::{Chip, LineRequestFlags}; - -use ringer::Ringer; - -use crate::{ringer::BeepRinger, scheduler::Scheduler}; - -mod handler; -mod router; -mod scheduler; -mod ringer; - -#[derive(Clone)] -struct AppState { - chip: Arc>, - cron: Arc>>, - scheduler: Arc> -} - -fn app_state() -> AppState { - let chip = Arc::new(Mutex::new(Chip::new("/dev/gpiochip0").unwrap())); - let cron = Arc::new(Mutex::new(Cron::new(chrono::Local))); - AppState { - chip: chip.clone(), - cron: cron.clone(), - scheduler: Arc::new(Scheduler::new( - BeepRinger::new(chip.clone()), - cron.clone() - )) - } -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let chip = Arc::new(Mutex::new(Chip::new("/dev/gpiochip0")?)); - let mut cron = Cron::new(chrono::Local); - let static_time = "0 30 7 * * *"; - let app_state = app_state(); - - println!("Adding alarm {}", static_time); - let job_result = cron.add_fn(static_time, move || { - let mut guard = chip.lock().unwrap(); - alarm(&mut *guard).unwrap(); - }); - - let _ = job_result.expect("Failed to add job"); - - cron.start(); - - start_server(app_state).await; - - Ok(()) -} - - -async fn start_server(app_state: AppState) { - let router = router::router(app_state); - let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); - - println!("Listening on http://0.0.0.0:8080"); - axum::serve(listener, router).await.unwrap(); -} - -fn alarm(chip: &mut Chip) -> Result<(), Box> { - let beeper = chip - .get_line(17)? - .request(LineRequestFlags::OUTPUT, 0, "my-gpio")?; - - for _ in 0..5 { - beeper.set_value(1)?; - thread::sleep(std::time::Duration::from_secs(1)); - beeper.set_value(0)?; - thread::sleep(std::time::Duration::from_secs(1)); - } - - Ok(()) -} diff --git a/Cargo.lock b/Cargo.lock index e0e0ca9..591ed11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,6 +586,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", + "serde_derive", ] [[package]] @@ -679,6 +680,7 @@ dependencies = [ "chrono", "cron_tab", "gpio-cdev", + "serde", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 839f48e..706d3bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ cron_tab = { version = "0.2", features = ["async"] } chrono = "0.4.42" axum = { version = "0.8.8", features = ["macros"] } tokio = { version = "1.49.0", features = ["full"] } +serde = { version = "1.0.228", features = ["derive"] } diff --git a/src/handler/alarm.rs b/src/handler/alarm.rs index a2cf06f..82c3f22 100644 --- a/src/handler/alarm.rs +++ b/src/handler/alarm.rs @@ -1,4 +1,20 @@ -#[axum::debug_handler] -pub async fn create_alarm() -> String { - "hello world".to_string() +use axum::{Json, extract::State}; +use serde::{Deserialize, Serialize}; + +use crate::{AppState}; + +#[derive(Deserialize, Serialize)] +pub struct CreateAlarmRequest { + hour: String, + minute: String, +} + +#[axum::debug_handler] +pub async fn create_alarm(State(state): State, Json(alarm): Json) -> String { + let result = state.scheduler.add_alarm(alarm.hour.as_str(), alarm.minute.as_str()); + + match result { + Ok(_) => "Alarm created".to_string(), + Err(e) => e.to_string(), + } } diff --git a/src/main.rs b/src/main.rs index 50d7413..f13919c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::{ sync::{Arc, Mutex}, }; +use chrono::{DateTime, Local}; use cron_tab::Cron; use gpio_cdev::Chip; @@ -12,6 +13,11 @@ mod router; mod scheduler; mod ringer; +struct Alarm { + enabled: bool, + time: DateTime +} + #[derive(Clone)] struct AppState { scheduler: Arc> @@ -30,10 +36,8 @@ fn app_state() -> AppState { #[tokio::main] async fn main() -> Result<(), Box> { - let static_time = "0 30 7 * * *"; let app_state = app_state(); - app_state.scheduler.schedule(static_time)?; app_state.scheduler.start(); start_server(app_state).await; diff --git a/src/scheduler.rs b/src/scheduler.rs index 1fa8a1c..7e62cc4 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -9,11 +9,16 @@ use crate::ringer::Ringer; pub struct Scheduler { ringer: Arc>, cron: Arc>>, + alarms: Arc>>, } impl Scheduler { pub fn new(ringer: Arc>, cron: Arc>>) -> 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 Scheduler { 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(); }