removed some deps, using heap to store the current ringer (makes it more dynamic)

This commit is contained in:
2026-03-02 20:54:34 +01:00
parent a86a28fd85
commit 8ed9d872dc
7 changed files with 68 additions and 968 deletions

970
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,10 +10,6 @@ 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"] }
actix-web = "4.12.1"
paperclip = { version = "0.9.5", features = ["actix4"] }
apistos = { version = "0.6.0", features = ["swagger-ui"] }
schemars = { package = "apistos-schemars", version = "0.8" }
sea-orm = { version = "1.1.19", features = [ sea-orm = { version = "1.1.19", features = [
"macros", "macros",
"runtime-tokio", "runtime-tokio",

View File

@@ -1,7 +1,5 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use actix_web::{App, HttpServer, web::{self, resource}};
use apistos::{SwaggerUIConfig, app::{BuildConfig, OpenApiWrapper}, info::Info, spec::Spec};
use cron_tab::Cron; use cron_tab::Cron;
use gpio_cdev::Chip; use gpio_cdev::Chip;
use sea_orm::Database; use sea_orm::Database;
@@ -10,7 +8,7 @@ use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter; use utoipa_axum::router::OpenApiRouter;
use utoipa_swagger_ui::SwaggerUi; use utoipa_swagger_ui::SwaggerUi;
use crate::{ringer::{BeepRinger, SilentRinger}, scheduler::Scheduler}; use crate::{ringer::{BeepRinger, Ringer, SilentRinger}, scheduler::Scheduler};
mod scheduler; mod scheduler;
mod ringer; mod ringer;
@@ -22,17 +20,32 @@ mod model;
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
scheduler: Arc<Scheduler<SilentRinger>> scheduler: Arc<Scheduler>
}
fn construct_ringer() -> Arc<Mutex<dyn Ringer>> {
let result = Chip::new("/dev/gpiochip0");
match result {
Ok(chip) => {
let chip = Arc::new(Mutex::new(chip));
Arc::new(Mutex::new(BeepRinger::new(chip))) as Arc<Mutex<dyn Ringer>>
},
Err(e) => {
println!("Error opening chip (falling back to silent ringer): {}", e);
Arc::new(Mutex::new(SilentRinger::new())) as Arc<Mutex<dyn Ringer>>
}
}
} }
async fn app_state() -> AppState { async fn app_state() -> AppState {
// let chip: Arc<Mutex<Chip>> = Arc::new(Mutex::new(Chip::new("/dev/gpiochip0").unwrap()));
let cron = Arc::new(Mutex::new(Cron::new(chrono::Local))); let cron = Arc::new(Mutex::new(Cron::new(chrono::Local)));
let db = Database::connect("sqlite://snooze-pal.db").await.unwrap(); let db = Database::connect("sqlite://snooze-pal.db").await.unwrap();
let silent = Arc::new(Mutex::new(SilentRinger::new())); let ringer = construct_ringer();
AppState { AppState {
scheduler: Arc::new(Scheduler::new( scheduler: Arc::new(Scheduler::new(
silent, ringer,
cron.clone(), cron.clone(),
Arc::new(db) Arc::new(db)
)) ))

View File

@@ -4,14 +4,13 @@ use axum::http::StatusCode;
use axum::debug_handler; use axum::debug_handler;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use utoipa::{IntoResponses, ToSchema}; use utoipa::{IntoResponses, ToSchema};
use crate::AppState; use crate::AppState;
use crate::types::Alarm; use crate::types::Alarm;
#[derive(Debug, Deserialize, JsonSchema, ToSchema)] #[derive(Debug, Deserialize, ToSchema)]
pub struct RequestBody { pub struct RequestBody {
time: DateTime<Local>, time: DateTime<Local>,
} }
@@ -55,6 +54,14 @@ impl From<Alarm> for OkResponseBody {
)] )]
#[debug_handler] #[debug_handler]
pub async fn post_handler(State(AppState { scheduler, ..}): State<AppState>, Json(body): Json<RequestBody>) -> Responses { pub async fn post_handler(State(AppState { scheduler, ..}): State<AppState>, Json(body): Json<RequestBody>) -> Responses {
let alarm = scheduler.add_alarm(body.time).await.unwrap(); let result = scheduler.add_alarm(body.time).await;
match result {
Ok(alarm) => {
Responses::Ok(alarm.into()) Responses::Ok(alarm.into())
},
Err(e) => {
Responses::Error(e.to_string())
}
}
} }

View File

@@ -1,11 +1,12 @@
use std::{ use std::{
fmt::Debug,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread, thread,
}; };
use gpio_cdev::{Chip, LineRequestFlags}; use gpio_cdev::{Chip, LineRequestFlags};
pub trait Ringer: Send + Sync { pub trait Ringer: Send + Sync + Debug {
fn ring(&self) -> Result<(), String>; fn ring(&self) -> Result<(), String>;
} }
@@ -71,6 +72,7 @@ impl Ringer for BeepRinger {
/// Used for local testing without an actual beeper or similar. The only thing it does is print /// Used for local testing without an actual beeper or similar. The only thing it does is print
/// that it's ringing. /// that it's ringing.
#[derive(Debug)]
pub struct SilentRinger; pub struct SilentRinger;
impl SilentRinger { impl SilentRinger {

View File

@@ -9,16 +9,16 @@ use crate::ringer::Ringer;
use crate::types::Alarm; use crate::types::Alarm;
#[derive(Debug)] #[derive(Debug)]
pub struct Scheduler<T: Ringer + 'static> { pub struct Scheduler {
ringer: Arc<Mutex<T>>, ringer: Arc<Mutex<dyn Ringer>>,
cron: Arc<Mutex<Cron<Local>>>, cron: Arc<Mutex<Cron<Local>>>,
alarms: Arc<Mutex<Vec<Alarm>>>, alarms: Arc<Mutex<Vec<Alarm>>>,
db: Arc<DatabaseConnection>, db: Arc<DatabaseConnection>,
} }
impl<T: Ringer> Scheduler<T> { impl Scheduler {
pub fn new( pub fn new(
ringer: Arc<Mutex<T>>, ringer: Arc<Mutex<dyn Ringer>>,
cron: Arc<Mutex<Cron<Local>>>, cron: Arc<Mutex<Cron<Local>>>,
db: Arc<DatabaseConnection>, db: Arc<DatabaseConnection>,
) -> Self { ) -> Self {

View File

@@ -1,9 +1,7 @@
use apistos::ApiComponent;
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, ApiComponent)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Alarm { pub struct Alarm {
pub enabled: bool, pub enabled: bool,
pub time: DateTime<Local>, pub time: DateTime<Local>,