Browse Source

Flesh out main a bit more

There is now an instance of SectionRunner and ProgramRunner
master
Alex Mikhalev 4 years ago
parent
commit
b6bcde020c
  1. 2
      Cargo.toml
  2. 59
      src/main.rs

2
Cargo.toml

@ -11,7 +11,7 @@ rusqlite = "0.23.1"
color-eyre = "0.5.1" color-eyre = "0.5.1"
eyre = "0.6.0" eyre = "0.6.0"
thiserror = "1.0.20" 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 = { version = "0.1.19", features = ["log"] }
tracing-futures = "0.2.4" tracing-futures = "0.2.4"
pin-project = "0.4.23" pin-project = "0.4.23"

59
src/main.rs

@ -18,7 +18,10 @@ mod section_runner;
#[cfg(test)] #[cfg(test)]
mod trace_listeners; 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<DbConnection> { fn setup_db() -> Result<DbConnection> {
// let conn = DbConnection::open_in_memory()?; // let conn = DbConnection::open_in_memory()?;
@ -30,13 +33,19 @@ fn setup_db() -> Result<DbConnection> {
Ok(conn) Ok(conn)
} }
fn query_sections(conn: &DbConnection) -> Result<Vec<Section>> { fn query_sections(conn: &DbConnection) -> Result<Sections> {
let mut statement = conn.prepare_cached("SELECT id, name, interface_id FROM sections;")?; let mut statement = conn.prepare_cached("SELECT id, name, interface_id FROM sections;")?;
let rows = statement.query_map(NO_PARAMS, Section::from_sql)?; let rows = statement.query_map(NO_PARAMS, Section::from_sql)?;
Ok(rows.collect::<Result<Vec<_>, _>>()?) 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() tracing_subscriber::fmt()
.with_ansi(true) .with_ansi(true)
.with_env_filter( .with_env_filter(
@ -48,9 +57,49 @@ fn main() -> Result<()> {
let conn = setup_db()?; let conn = setup_db()?;
let sections = query_sections(&conn)?; let sections = query_sections(&conn)?;
for sec in sections { for sec in sections.values() {
info!(section = debug(&sec), "read section"); 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(()) Ok(())
} }

Loading…
Cancel
Save