diff --git a/src/database/mod.rs b/src/database/mod.rs index bc51812..07668da 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -3,3 +3,51 @@ mod migrations; pub use migration::*; pub use migrations::create_migrations; + +pub use rusqlite::Connection as DbConn; + +use eyre::Result; + +use crate::model::{Program, Programs, Section, Sections}; +use rusqlite::NO_PARAMS; + +pub fn setup_db() -> Result { + // let conn = DbConn::open_in_memory()?; + let mut conn = DbConn::open("test.db")?; + + let migs = create_migrations(); + migs.apply(&mut conn)?; + + Ok(conn) +} + +pub fn query_sections(conn: &DbConn) -> Result { + let mut statement = conn.prepare_cached( + "SELECT s.id, s.name, s.interface_id \ + FROM sections AS s;", + )?; + let rows = statement.query_map(NO_PARAMS, Section::from_sql)?; + let mut sections = Sections::new(); + for row in rows { + let section = row?; + sections.insert(section.id, section.into()); + } + Ok(sections) +} + +pub fn query_programs(conn: &DbConn) -> Result { + let mut statement = conn.prepare_cached( + " + SELECT p.id, p.name, ps.sequence, p.enabled, p.schedule + FROM programs AS p + INNER JOIN program_sequences AS ps + ON ps.program_id = p.id;", + )?; + let rows = statement.query_map(NO_PARAMS, Program::from_sql)?; + let mut programs = Programs::new(); + for row in rows { + let program = row?; + programs.insert(program.id, program.into()); + } + Ok(programs) +} diff --git a/src/main.rs b/src/main.rs index b773725..8acebb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,6 @@ #![warn(clippy::all)] #![warn(clippy::print_stdout)] -use color_eyre::eyre::Result; -use rusqlite::Connection as DbConnection; -use rusqlite::NO_PARAMS; -use tracing::info; -use tracing_subscriber::EnvFilter; - mod database; mod model; mod option_future; @@ -17,49 +11,10 @@ mod section_runner; #[cfg(test)] mod trace_listeners; -use model::{Program, Programs, Section, Sections}; +use color_eyre::eyre::Result; use std::sync::Arc; - -fn setup_db() -> Result { - // let conn = DbConnection::open_in_memory()?; - let mut conn = DbConnection::open("test.db")?; - - let migs = database::create_migrations(); - migs.apply(&mut conn)?; - - Ok(conn) -} - -fn query_sections(conn: &DbConnection) -> Result { - let mut statement = conn.prepare_cached( - "SELECT s.id, s.name, s.interface_id \ - FROM sections AS s;", - )?; - let rows = statement.query_map(NO_PARAMS, Section::from_sql)?; - let mut sections = Sections::new(); - for row in rows { - let section = row?; - sections.insert(section.id, section.into()); - } - Ok(sections) -} - -fn query_programs(conn: &DbConnection) -> Result { - let mut statement = conn.prepare_cached( - " - SELECT p.id, p.name, ps.sequence, p.enabled, p.schedule - FROM programs AS p - INNER JOIN program_sequences AS ps - ON ps.program_id = p.id;", - )?; - let rows = statement.query_map(NO_PARAMS, Program::from_sql)?; - let mut programs = Programs::new(); - for row in rows { - let program = row?; - programs.insert(program.id, program.into()); - } - Ok(programs) -} +use tracing::info; +use tracing_subscriber::EnvFilter; #[tokio::main] async fn main() -> Result<()> { @@ -71,9 +26,9 @@ async fn main() -> Result<()> { .init(); color_eyre::install()?; - let conn = setup_db()?; + let conn = database::setup_db()?; - let sections = query_sections(&conn)?; + let sections = database::query_sections(&conn)?; for sec in sections.values() { info!(section = debug(&sec), "read section"); } @@ -83,7 +38,19 @@ async fn main() -> Result<()> { let mut section_runner = section_runner::SectionRunner::new(section_interface); let mut program_runner = program_runner::ProgramRunner::new(section_runner.clone()); - let programs = query_programs(&conn)?; + let mut programs = database::query_programs(&conn)?; + + // TEMP: just so the program runs right away + if let Some(prog) = programs.get_mut(&1) { + let prog_mut = Arc::make_mut(prog); + if let Some(first_time) = prog_mut.schedule.times.get_mut(0) { + *first_time = chrono::Local::now().time() + chrono::Duration::seconds(5); + } + } + + for prog in programs.values() { + info!(program = debug(&prog), "read program"); + } program_runner.update_sections(sections.clone()).await?; program_runner.update_programs(programs.clone()).await?;