removed some deps, using heap to store the current ringer (makes it more dynamic)
This commit is contained in:
970
Cargo.lock
generated
970
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -10,10 +10,6 @@ chrono = { version = "0.4.42", features = ["serde"] }
|
||||
axum = { version = "0.8.8", features = ["macros"] }
|
||||
tokio = { version = "1.49.0", features = ["full"] }
|
||||
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 = [
|
||||
"macros",
|
||||
"runtime-tokio",
|
||||
|
||||
31
src/main.rs
31
src/main.rs
@@ -1,7 +1,5 @@
|
||||
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 gpio_cdev::Chip;
|
||||
use sea_orm::Database;
|
||||
@@ -10,7 +8,7 @@ use utoipa::OpenApi;
|
||||
use utoipa_axum::router::OpenApiRouter;
|
||||
use utoipa_swagger_ui::SwaggerUi;
|
||||
|
||||
use crate::{ringer::{BeepRinger, SilentRinger}, scheduler::Scheduler};
|
||||
use crate::{ringer::{BeepRinger, Ringer, SilentRinger}, scheduler::Scheduler};
|
||||
|
||||
mod scheduler;
|
||||
mod ringer;
|
||||
@@ -22,19 +20,34 @@ mod model;
|
||||
|
||||
#[derive(Clone)]
|
||||
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 {
|
||||
// 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 db = Database::connect("sqlite://snooze-pal.db").await.unwrap();
|
||||
let silent = Arc::new(Mutex::new(SilentRinger::new()));
|
||||
let ringer = construct_ringer();
|
||||
|
||||
AppState {
|
||||
scheduler: Arc::new(Scheduler::new(
|
||||
silent,
|
||||
cron.clone(),
|
||||
Arc::new(db)
|
||||
ringer,
|
||||
cron.clone(),
|
||||
Arc::new(db)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,13 @@ use axum::http::StatusCode;
|
||||
use axum::debug_handler;
|
||||
use axum::response::IntoResponse;
|
||||
use chrono::{DateTime, Local};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::{IntoResponses, ToSchema};
|
||||
|
||||
use crate::AppState;
|
||||
use crate::types::Alarm;
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema, ToSchema)]
|
||||
#[derive(Debug, Deserialize, ToSchema)]
|
||||
pub struct RequestBody {
|
||||
time: DateTime<Local>,
|
||||
}
|
||||
@@ -55,6 +54,14 @@ impl From<Alarm> for OkResponseBody {
|
||||
)]
|
||||
#[debug_handler]
|
||||
pub async fn post_handler(State(AppState { scheduler, ..}): State<AppState>, Json(body): Json<RequestBody>) -> Responses {
|
||||
let alarm = scheduler.add_alarm(body.time).await.unwrap();
|
||||
Responses::Ok(alarm.into())
|
||||
let result = scheduler.add_alarm(body.time).await;
|
||||
|
||||
match result {
|
||||
Ok(alarm) => {
|
||||
Responses::Ok(alarm.into())
|
||||
},
|
||||
Err(e) => {
|
||||
Responses::Error(e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
};
|
||||
|
||||
use gpio_cdev::{Chip, LineRequestFlags};
|
||||
|
||||
pub trait Ringer: Send + Sync {
|
||||
pub trait Ringer: Send + Sync + Debug {
|
||||
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
|
||||
/// that it's ringing.
|
||||
#[derive(Debug)]
|
||||
pub struct SilentRinger;
|
||||
|
||||
impl SilentRinger {
|
||||
|
||||
@@ -9,16 +9,16 @@ use crate::ringer::Ringer;
|
||||
use crate::types::Alarm;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Scheduler<T: Ringer + 'static> {
|
||||
ringer: Arc<Mutex<T>>,
|
||||
pub struct Scheduler {
|
||||
ringer: Arc<Mutex<dyn Ringer>>,
|
||||
cron: Arc<Mutex<Cron<Local>>>,
|
||||
alarms: Arc<Mutex<Vec<Alarm>>>,
|
||||
db: Arc<DatabaseConnection>,
|
||||
}
|
||||
|
||||
impl<T: Ringer> Scheduler<T> {
|
||||
impl Scheduler {
|
||||
pub fn new(
|
||||
ringer: Arc<Mutex<T>>,
|
||||
ringer: Arc<Mutex<dyn Ringer>>,
|
||||
cron: Arc<Mutex<Cron<Local>>>,
|
||||
db: Arc<DatabaseConnection>,
|
||||
) -> Self {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use apistos::ApiComponent;
|
||||
use chrono::{DateTime, Local};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, ApiComponent)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Alarm {
|
||||
pub enabled: bool,
|
||||
pub time: DateTime<Local>,
|
||||
|
||||
Reference in New Issue
Block a user