From 55dc677b46d7276bfa6f9296c49a5794b499af43 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 16 Feb 2022 17:48:24 +0100 Subject: [PATCH] * Code cleanup, feature updated - cleanup unsed variables - added dynamic slow effect based on number of issues --- .drone.yml | 17 +++++++++++++++++ src/config/cli.yml | 2 +- src/config/mod.rs | 37 ++++++++++++++++++++++++++----------- src/padcontrol/mod.rs | 35 +++++++++++++++++++++-------------- 4 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..9b896de --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +--- +kind: pipeline +type: docker +name: default + +steps: + - name: test + image: rust:1 + commands: + - cargo build --verbose --all + - cargo test --verbose --all + - name: release + image: rust:1 + commands: + - cargo build --release --verbose --all + when: + event: tag diff --git a/src/config/cli.yml b/src/config/cli.yml index 4096f08..6d6bf8b 100644 --- a/src/config/cli.yml +++ b/src/config/cli.yml @@ -14,4 +14,4 @@ args: short: m value_name: MODE help: Handles mode - takes_value: true \ No newline at end of file + takes_value: true diff --git a/src/config/mod.rs b/src/config/mod.rs index 80e897d..025ff38 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -68,10 +68,7 @@ impl Context { Err(_) => vec![], }; if events.len() > 0 { - println!( - "Reloading {cfg}", - cfg = self.configfile - ); + println!("Reloading {cfg}", cfg = self.configfile); self.cfg.load(self.configfile.as_str()); get_zabbix_authtoken(&mut self.cfg); break; @@ -118,7 +115,7 @@ impl<'a> Config { }; if !fileexists { - let _a = File::create(configfile); + File::create(configfile).unwrap(); } fileopen = File::open(configfile); @@ -135,15 +132,33 @@ impl<'a> Config { Ok(ncfg) => { let tmpcfg = Config::new(); Config { - server: ncfg["server"].as_str().unwrap_or(tmpcfg.server.as_str()).to_string(), - username: ncfg["username"].as_str().unwrap_or(tmpcfg.username.as_str()).to_string(), - password: ncfg["password"].as_str().unwrap_or(tmpcfg.password.as_str()).to_string(), - authtoken: Some(ncfg["authtoken"].as_str().unwrap_or(self.authtoken.clone().unwrap().as_str()).to_string()), - sloweffect: Some(ncfg["sloweffect"].as_bool().unwrap_or(tmpcfg.sloweffect.unwrap())), + server: ncfg["server"] + .as_str() + .unwrap_or(tmpcfg.server.as_str()) + .to_string(), + username: ncfg["username"] + .as_str() + .unwrap_or(tmpcfg.username.as_str()) + .to_string(), + password: ncfg["password"] + .as_str() + .unwrap_or(tmpcfg.password.as_str()) + .to_string(), + authtoken: Some( + ncfg["authtoken"] + .as_str() + .unwrap_or(self.authtoken.clone().unwrap().as_str()) + .to_string(), + ), + sloweffect: Some( + ncfg["sloweffect"] + .as_bool() + .unwrap_or(tmpcfg.sloweffect.unwrap()), + ), refresh: Some(ncfg["refresh"].as_u64().unwrap_or(tmpcfg.refresh.unwrap())), limit: Some(ncfg["limit"].as_u64().unwrap_or(tmpcfg.limit.unwrap())), } - }, + } Err(err) => { errorreading = true; println!("error occured: '{}'", err); diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index d050458..61c9514 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -10,6 +10,7 @@ pub const MATRIX_SIZE: i32 = MATRIX_WIDTH * MATRIX_WIDTH; pub const REQUEST_TIMEOUT: i32 = 5; pub const ZERO_COLOR: f32 = 0.; pub const ONE_COLOR: f32 = 1.; +pub const SLOWEFFECT_FACTOR: f32 = 800.0; pub fn run(ctx: &mut Context) { match ctx.mode.as_str() { @@ -36,16 +37,14 @@ pub fn run(ctx: &mut Context) { fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { let (mut canvas, poller) = launchy::CanvasLayout::new_polling(); match canvas.add_by_guess_rotated::(0, 0, launchy::Rotation::None) { - Ok(o) => o, + Ok(o) => { + println!("Connected to device using USB {o:?}"); + clear(&mut canvas); + } Err(err) => { - eprintln!( - "Failed to connect to device, check USB connection {err}", - err = err - ); + eprintln!("Failed to connect to device, check USB connection {err}",); } }; - println!("Connected to device using USB"); - clear(&mut canvas); (canvas, poller) } @@ -55,7 +54,7 @@ fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { canvas[msg.pad()] = color; println!("{msg:?}", msg = msg.pad()) } - let _res = canvas.flush(); + canvas.flush().unwrap(); } } @@ -69,7 +68,10 @@ fn test(ctx: &mut Context) { let zabbix_data = match get_zabbix_problems(&ctx.cfg) { Ok(z) => z, Err(_) => { - println!("Error requesting zabbix service, retrying in {} seconds", REQUEST_TIMEOUT); + println!( + "Error requesting zabbix service, retrying in {} seconds", + REQUEST_TIMEOUT + ); sleep(Duration::from_secs(REQUEST_TIMEOUT as u64)); continue; } @@ -85,7 +87,10 @@ pub fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) { let zabbix_data = match get_zabbix_problems(&ctx.cfg) { Ok(zabbix_data) => zabbix_data, Err(_) => { - println!("Error requesting zabbix service, retrying in {}", REQUEST_TIMEOUT); + println!( + "Error requesting zabbix service, retrying in {}", + REQUEST_TIMEOUT + ); sleep(Duration::from_secs(REQUEST_TIMEOUT as u64)); continue; } @@ -107,7 +112,9 @@ pub fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) { }; canvas[p] = c; if ctx.cfg.sloweffect.unwrap() { - sleep(Duration::from_millis(15)); + sleep(Duration::from_millis( + (SLOWEFFECT_FACTOR * (1.0 / ctx.datamatrix.layout.len() as f32)) as u64, + )); } canvas.flush().unwrap(); max += 1; @@ -125,7 +132,7 @@ fn effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { canvas[msg.pad()] = color; println!("{msg:?}", msg = msg.pad()) } - let _res = canvas.flush(); + canvas.flush().unwrap(); canvas.iter().for_each(|pad| { let surrounding_color = pad @@ -143,7 +150,7 @@ fn effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { fn clear(canvas: &mut CanvasLayout) { for a in 0..MATRIX_WIDTH { - for b in 0..MATRIX_WIDTH+1 { + for b in 0..MATRIX_WIDTH + 1 { canvas[Pad { x: a, y: b }] = Color { r: ZERO_COLOR, g: ZERO_COLOR, @@ -151,5 +158,5 @@ fn clear(canvas: &mut CanvasLayout) { }; } } - let _res = canvas.flush(); + canvas.flush().unwrap(); }