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

1
Cargo.lock generated
View File

@@ -130,6 +130,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"wasm-bindgen", "wasm-bindgen",
"windows-link", "windows-link",
] ]

View File

@@ -6,7 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
gpio-cdev = "0.6.0" gpio-cdev = "0.6.0"
cron_tab = { version = "0.2", features = ["async"] } cron_tab = { version = "0.2", features = ["async"] }
chrono = "0.4.42" chrono = { version = "0.4.42", features = ["serde"] }
axum = { version = "0.8.8", features = ["macros"] } axum = { version = "0.8.8", features = ["macros"] }
tokio = { version = "1.49.0", features = ["full"] } tokio = { version = "1.49.0", features = ["full"] }
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }

View File

@@ -1,20 +1,35 @@
use axum::{Json, extract::State}; use axum::{Json, extract::State, http::StatusCode};
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{AppState}; use crate::{AppState};
use crate::types::Alarm;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct CreateAlarmRequest { pub struct CreateAlarmRequest {
hour: String, time: DateTime<Local>
minute: String, }
#[derive(Deserialize, Serialize)]
pub struct CreateAlarmResponse {
alarm: Alarm
}
#[derive(Deserialize, Serialize)]
pub struct AlarmError {
message: String
} }
#[axum::debug_handler] #[axum::debug_handler]
pub async fn create_alarm(State(state): State<AppState>, Json(alarm): Json<CreateAlarmRequest>) -> String { pub async fn create_alarm(
let result = state.scheduler.add_alarm(alarm.hour.as_str(), alarm.minute.as_str()); State(state): State<AppState>,
Json(create_request): Json<CreateAlarmRequest>
) -> Result<(StatusCode, Json<CreateAlarmResponse>), (StatusCode, Json<AlarmError>)> {
let result = state.scheduler.add_alarm(create_request.time);
match result { match result {
Ok(_) => "Alarm created".to_string(), Ok(alarm) => Ok((StatusCode::CREATED, Json(CreateAlarmResponse { alarm }))),
Err(e) => e.to_string(), Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, Json(AlarmError { message: e }))),
} }
} }

View File

@@ -2,7 +2,6 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use chrono::{DateTime, Local};
use cron_tab::Cron; use cron_tab::Cron;
use gpio_cdev::Chip; use gpio_cdev::Chip;
@@ -12,11 +11,8 @@ mod handler;
mod router; mod router;
mod scheduler; mod scheduler;
mod ringer; mod ringer;
mod types;
struct Alarm {
enabled: bool,
time: DateTime<Local>
}
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {

View File

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

14
src/types.rs Normal file
View File

@@ -0,0 +1,14 @@
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Alarm {
enabled: bool,
time: DateTime<Local>,
}
impl Alarm {
pub fn new(enabled: bool, time: DateTime<Local>) -> Self {
Self { enabled, time }
}
}