database persistence in progress

This commit is contained in:
2026-02-05 23:22:00 +01:00
parent a398911527
commit f7e55536ab
17 changed files with 3600 additions and 22 deletions

22
src/dao/alarm.rs Normal file
View File

@@ -0,0 +1,22 @@
use chrono::{DateTime, Local};
use sea_orm::{
ActiveModelTrait, ActiveValue::Set, ConnectionTrait, DbErr
};
use crate::model::{self, alarm};
pub async fn create_alarm<C: ConnectionTrait>(
db: &C,
title: &str,
time: DateTime<Local>,
) -> Result<alarm::Model, DbErr> {
let alarm_to_create = model::alarm::ActiveModel {
title: Set(title.to_string()),
time: Set(time.naive_utc()),
enabled: Set(true),
..Default::default()
};
Ok(alarm_to_create.insert(db).await?)
}

2
src/dao/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod alarm;

View File

@@ -4,6 +4,7 @@ use actix_web::{App, HttpServer, web};
use apistos::{SwaggerUIConfig, app::{BuildConfig, OpenApiWrapper}, info::Info, spec::Spec};
use cron_tab::Cron;
use gpio_cdev::Chip;
use sea_orm::Database;
use crate::{ringer::BeepRinger, scheduler::Scheduler};
@@ -11,6 +12,8 @@ mod scheduler;
mod ringer;
mod types;
mod resources;
mod dao;
mod model;
#[derive(Clone)]
@@ -18,20 +21,22 @@ struct AppState {
scheduler: Arc<Scheduler<BeepRinger>>
}
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 db = Database::connect("sqlite://snooze-pal.db").await.unwrap();
AppState {
scheduler: Arc::new(Scheduler::new(
Arc::new(Mutex::new(BeepRinger::new(chip.clone()))),
cron.clone()
cron.clone(),
Arc::new(db)
))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_state = app_state();
let app_state = app_state().await;
app_state.scheduler.start();

18
src/model/alarm.rs Normal file
View File

@@ -0,0 +1,18 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "alarm")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub time: DateTime,
pub enabled: bool,
pub title: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

5
src/model/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
pub mod prelude;
pub mod alarm;

3
src/model/prelude.rs Normal file
View File

@@ -0,0 +1,3 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
pub use super::alarm::Entity as Alarm;

View File

@@ -2,24 +2,31 @@ use std::sync::{Arc, Mutex};
use chrono::{DateTime, Local, Timelike};
use cron_tab::Cron;
use sea_orm::DatabaseConnection;
use crate::dao::alarm::create_alarm;
use crate::ringer::Ringer;
use crate::types::Alarm;
#[derive(Debug)]
pub struct Scheduler<T: Ringer + 'static> {
ringer: Arc<Mutex<T>>,
cron: Arc<Mutex<Cron<Local>>>,
alarms: Arc<Mutex<Vec<Alarm>>>,
db: Arc<DatabaseConnection>,
}
impl<T: Ringer> Scheduler<T> {
pub fn new(ringer: Arc<Mutex<T>>, cron: Arc<Mutex<Cron<Local>>>) -> Self {
pub fn new(
ringer: Arc<Mutex<T>>,
cron: Arc<Mutex<Cron<Local>>>,
db: Arc<DatabaseConnection>,
) -> Self {
Self {
ringer,
cron,
alarms: Arc::new(Mutex::new(Vec::new())),
db,
}
}
@@ -41,12 +48,15 @@ impl<T: Ringer> Scheduler<T> {
todo!()
}
pub fn add_alarm(&self, time: DateTime<Local>) -> Result<Alarm, String> {
let mut alarms = self.alarms.lock().map_err(|e| e.to_string())?;
pub async fn add_alarm(&self, time: DateTime<Local>) -> Result<Alarm, String> {
let cron_schedule = format!("{} {} {} * * *", "*", time.minute(), time.hour());
let alarm = Alarm::new(true, time);
alarms.push(alarm.clone());
self.schedule(&cron_schedule).map_err(|e| e.to_string())?;
let alarm = Alarm::new(true, time);
create_alarm(&*self.db, "something", time)
.await
.map_err(|e| e.to_string())?;
self.schedule(&cron_schedule).map_err(|e| e.to_string())?;
println!("Added alarm {}", cron_schedule);
Ok(alarm)