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

83
'
View File

@@ -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<Mutex<Chip>>,
cron: Arc<Mutex<Cron<Local>>>,
scheduler: Arc<Scheduler<BeepRinger>>
}
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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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(())
}

2
Cargo.lock generated
View File

@@ -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",
]

View File

@@ -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"] }

View File

@@ -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<AppState>, Json(alarm): Json<CreateAlarmRequest>) -> 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(),
}
}

View File

@@ -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<Local>
}
#[derive(Clone)]
struct AppState {
scheduler: Arc<Scheduler<BeepRinger>>
@@ -30,10 +36,8 @@ fn app_state() -> AppState {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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;

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();
}