|
|
@ -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(()) |
|
|
|
} |
|
|
|
} |
|
|
|