From c25a8d633e88ed4c24cb7e54d5a71d3e695aa5cf Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Mon, 10 Jul 2023 12:10:20 +0100 Subject: [PATCH] chore: change config --- src/main.rs | 10 ++----- src/render.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index a70aa00..2da7b78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,14 +118,10 @@ fn ligth_controll( if let Ok(action) = action { match action { Action::Reload => { - let new_render = render::Render::new(pool.clone()); - - if new_render.is_err() || new_render.as_ref().ok().is_none() { - println!("something every wrong {:?}", new_render.err()); - continue; + let result = render.reload_config(); + if result.is_err() || result.as_ref().ok().is_none() { + println!("Ehh failed to load config i guess: {:?}", result.err()); } - - render = new_render.unwrap(); } Action::Clear => { render.blank(); diff --git a/src/render.rs b/src/render.rs index c97e0bd..5c58bab 100644 --- a/src/render.rs +++ b/src/render.rs @@ -180,6 +180,84 @@ impl Render { Ok(()) } + pub fn reload_config(&mut self) -> Result<(), Box> { + println!("Reload config"); + + let conn = self.conn.get()?; + + let stmt = conn.prepare("SELECT ledcount, tags from configuration;"); + + if stmt.is_err() || stmt.as_ref().ok().is_none() { + Err(format!( + "Something went wrong while reading the database {:?}", + stmt.as_ref().err() + ))?; + } + + let mut stmt = stmt?; + + struct Data { + ledcount: u32, + tag: String, + } + + let version_iter = stmt.query_map([], |row| { + Ok(Data { + ledcount: row.get(0)?, + tag: row.get(1)?, + }) + }); + + if version_iter.is_err() || version_iter.as_ref().ok().is_none() { + Err(format!( + "Something went wrong while reading the database {:?}", + version_iter.as_ref().err() + ))?; + } + + let version_iter = version_iter.ok().unwrap(); + + let mut current_pos = 0; + + let mut local_led_config = Vec::new(); + + for data in version_iter { + let data = data?; + + let tag = if data.tag.is_empty() { + None + } else { + Some(data.tag) + }; + + let start = current_pos; + let end = current_pos + data.ledcount; + current_pos = end; + + let start: Result = start.try_into(); + let end: Result = end.try_into(); + + if start.is_err() || start.ok().is_none() || end.is_err() || end.ok().is_none() { + println!("Skiping loading configuration"); + continue; + } + + let start = start.ok().unwrap(); + let end = end.ok().unwrap(); + + local_led_config.push(LocalLedConfiguration { tag, start, end }); + } + + self.num_leds = current_pos; + println!( + "Loaded: {} leds, with config: {:?}", + self.num_leds, local_led_config + ); + self.local_led_config = local_led_config; + + Ok(()) + } + pub fn add_animation(&mut self, animation: Animation) { println!("Added animation");