chore: change config
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Andre Henriques 2023-07-10 12:10:20 +01:00
parent 53bda4eb06
commit c25a8d633e
2 changed files with 81 additions and 7 deletions

View File

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

View File

@ -180,6 +180,84 @@ impl Render {
Ok(())
}
pub fn reload_config(&mut self) -> Result<(), Box<dyn Error>> {
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<usize, _> = start.try_into();
let end: Result<usize, _> = 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");