diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index 67fe2be..4d06fd7 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -8,7 +8,7 @@ use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, pub const MATRIX_WIDTH: i32 = 8; -pub const MATRIX_SIZE: i32 = MATRIX_WIDTH*MATRIX_WIDTH; +pub const MATRIX_SIZE: i32 = MATRIX_WIDTH * MATRIX_WIDTH; pub fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { @@ -38,14 +38,20 @@ pub fn _input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { pub fn draw(canvas: &mut CanvasLayout, datamatrix: &mut ZabbixLayout, cfg: &mut Config) { println!("Refresh rate is {} seconds", cfg.refresh.unwrap()); loop { - let zabbix_data = get_zabbix_problems(cfg); + let zabbix_data_result = get_zabbix_problems(cfg); + let zabbix_data = match zabbix_data_result { + Ok(zabbix_data) => zabbix_data, + Err(_) => { + println!("Error requesting service"); + sleep(Duration::from_secs(10)); + continue; + } + }; datamatrix.compute(&zabbix_data); clear(canvas); - let mut x = 0i32; - let mut y = 1i32; - let mut xx = 0i32; - for j in datamatrix.layout.iter() { - let p = Pad { x: x, y: y }; + let mut max = 0; + for (i,j) in datamatrix.layout.iter().enumerate() { + let p = Pad { x: ((i as i32) % MATRIX_WIDTH ), y: ((i as i32) / MATRIX_WIDTH )+1 }; let c = match j.status { 5 => Color::RED, 4 => Color::from_hue(0.05), @@ -54,13 +60,8 @@ pub fn draw(canvas: &mut CanvasLayout, datamatrix: &mut ZabbixLayout, cfg: &mut _ => Color::GREEN, }; canvas[p] = c; - x += 1; - xx += 1; - if x % MATRIX_WIDTH == 0 { - y += 1; - x = 0; - }; - if xx >= MATRIX_SIZE { + max += 1; + if max >= MATRIX_SIZE { break; } if cfg.sloweffect.unwrap() { diff --git a/src/zabbix/api.rs b/src/zabbix/api.rs index ef0bcd7..e51157a 100644 --- a/src/zabbix/api.rs +++ b/src/zabbix/api.rs @@ -16,17 +16,20 @@ pub fn get_zabbix_authtoken(cfg: &mut Config) { cfg.authtoken = Some(values["result"].as_str().unwrap().to_string()); } -pub fn get_zabbix_problems(cfg: &mut Config) -> Value { +pub fn get_zabbix_problems(cfg: &mut Config) -> Result { let body = query_triggers( &cfg.authtoken.as_ref().unwrap_or(&String::from("")), ); let resp = reqwest::blocking::Client::new() .post(&cfg.server) .json(&body) - .send() - .unwrap(); + .send(); - resp.json().unwrap() + //let res = match resp { + // Ok(res) => res.json() + //}; + + Ok(resp.unwrap().json().unwrap()) } pub fn query_auth_token(zabbix_username: &String, zabbix_password: &String) -> Value {