From b6bcde020cba6701b1b54d80cc89601cb1f36106 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Fri, 18 Sep 2020 22:56:18 -0600 Subject: [PATCH] Flesh out main a bit more There is now an instance of SectionRunner and ProgramRunner --- Cargo.toml | 2 +- src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cad8b47..79097ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ rusqlite = "0.23.1" color-eyre = "0.5.1" eyre = "0.6.0" thiserror = "1.0.20" -tokio = { version = "0.2.22", features = ["rt-core", "time", "stream", "sync", "macros", "test-util"] } +tokio = { version = "0.2.22", features = ["rt-core", "time", "stream", "sync", "signal", "macros", "test-util"] } tracing = { version = "0.1.19", features = ["log"] } tracing-futures = "0.2.4" pin-project = "0.4.23" diff --git a/src/main.rs b/src/main.rs index 4566e83..858a851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,10 @@ mod section_runner; #[cfg(test)] mod trace_listeners; -use model::Section; +use im::ordmap; +use model::{Program, ProgramItem, ProgramRef, Section, Sections}; +use schedule::Schedule; +use std::{sync::Arc, time::Duration}; fn setup_db() -> Result { // let conn = DbConnection::open_in_memory()?; @@ -30,13 +33,19 @@ fn setup_db() -> Result { Ok(conn) } -fn query_sections(conn: &DbConnection) -> Result> { +fn query_sections(conn: &DbConnection) -> Result { let mut statement = conn.prepare_cached("SELECT id, name, interface_id FROM sections;")?; let rows = statement.query_map(NO_PARAMS, Section::from_sql)?; - Ok(rows.collect::, _>>()?) + let mut sections = Sections::new(); + for row in rows { + let section = row?; + sections.insert(section.id, section.into()); + } + Ok(sections) } -fn main() -> Result<()> { +#[tokio::main] +async fn main() -> Result<()> { tracing_subscriber::fmt() .with_ansi(true) .with_env_filter( @@ -48,9 +57,49 @@ fn main() -> Result<()> { let conn = setup_db()?; let sections = query_sections(&conn)?; - for sec in sections { + for sec in sections.values() { info!(section = debug(&sec), "read section"); } + // TODO: Section interface which actual does something. Preferrably selectable somehow + let section_interface: Arc<_> = section_interface::MockSectionInterface::new(6).into(); + let mut section_runner = section_runner::SectionRunner::new(section_interface); + let mut program_runner = program_runner::ProgramRunner::new(section_runner.clone()); + + let run_time = chrono::Local::now().time() + chrono::Duration::seconds(5); + let schedule = Schedule::new( + vec![run_time], + schedule::every_day(), + schedule::DateTimeBound::None, + schedule::DateTimeBound::None, + ); + let program: ProgramRef = Program { + id: 1, + name: "Test Program".into(), + sequence: sections + .values() + .map(|sec| ProgramItem { + section_id: sec.id, + duration: Duration::from_secs(2), + }) + .collect(), + enabled: true, + schedule, + } + .into(); + let programs = ordmap![1 => program]; + + program_runner.update_sections(sections.clone()).await?; + program_runner.update_programs(programs.clone()).await?; + + info!("sprinklers_rs initialized"); + + tokio::signal::ctrl_c().await?; + info!("Interrupt received, shutting down"); + + program_runner.quit().await?; + section_runner.quit().await?; + tokio::task::yield_now().await; + Ok(()) }