updated zabbixlaunch

This commit is contained in:
Paul Lecuq 2022-05-16 14:29:19 +02:00
parent ca349dc187
commit f3a1d5c900
2 changed files with 47 additions and 29 deletions

View File

@ -17,13 +17,21 @@ pub enum ReloadFrequency {
Low = 10, Low = 10,
} }
#[derive(Debug, Clone)]
pub enum Mode {
Draw,
Test,
Input,
Effect,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context { pub struct Context {
pub cfg: Config, pub cfg: Config,
configfile: String, configfile: String,
inotify: Inotify, inotify: Inotify,
pub datamatrix: DataMatrix, pub datamatrix: DataMatrix,
pub mode: String, pub mode: Option<Mode>,
} }
impl Context { impl Context {
@ -37,7 +45,16 @@ impl Context {
configfile: configfile, configfile: configfile,
inotify: Inotify::init(InitFlags::IN_NONBLOCK).unwrap(), inotify: Inotify::init(InitFlags::IN_NONBLOCK).unwrap(),
datamatrix: DataMatrix::new(), datamatrix: DataMatrix::new(),
mode: cli.value_of("mode").unwrap_or("draw").to_string(), mode: match cli.value_of("mode").unwrap_or("draw") {
"draw" => Some(Mode::Draw),
"test" => Some(Mode::Test),
"input" => Some(Mode::Input),
"effect" => Some(Mode::Effect),
_ => {
println!("No valid mode choosen");
None
}
},
}; };
println!("Loading {configfile} file ...", configfile = ctx.configfile); println!("Loading {configfile} file ...", configfile = ctx.configfile);

View File

@ -1,4 +1,4 @@
use crate::config::Context; use crate::config::{Context, Mode};
use crate::zabbix::api::get_zabbix_problems; use crate::zabbix::api::get_zabbix_problems;
use launchy::Color; use launchy::Color;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad}; use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
@ -13,23 +13,22 @@ const ONE_COLOR: f32 = 1.;
const SLOWEFFECT_FACTOR: f32 = 800.0; const SLOWEFFECT_FACTOR: f32 = 800.0;
pub fn run(ctx: &mut Context) { pub fn run(ctx: &mut Context) {
match ctx.mode.as_str() { match ctx.mode {
"draw" => { Some(Mode::Draw) => {
let (mut canvas, mut _poller) = initpad(); let (mut canvas, mut _poller) = initpad();
draw(&mut canvas, ctx); draw(&mut canvas, ctx);
} }
"input" => { Some(Mode::Input) => {
let (mut canvas, mut poller) = initpad(); let (mut canvas, mut poller) = initpad();
input(&mut canvas, &mut poller); input(&mut canvas, &mut poller);
} }
"effect" => { Some(Mode::Effect) => {
let (mut canvas, mut poller) = initpad(); let (mut canvas, mut poller) = initpad();
effect(&mut canvas, &mut poller) effect(&mut canvas, &mut poller)
} }
"test" => test(ctx), Some(Mode::Test) => test(ctx),
_ => { None => {
println!("No valid option choosen for mode"); println!("No valid option choosen for mode");
std::process::exit(1)
} }
} }
} }
@ -103,14 +102,13 @@ fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) {
x: ((i as i32) % MATRIX_WIDTH), x: ((i as i32) % MATRIX_WIDTH),
y: ((i as i32) / MATRIX_WIDTH) + 1, y: ((i as i32) / MATRIX_WIDTH) + 1,
}; };
let c = match j.status { canvas[p] = match j.status {
5 => Color::RED, 5 => Color::RED,
4 => Color::from_hue(0.05), 4 => Color::from_hue(0.05),
3 => Color::from_hue(0.1), 3 => Color::from_hue(0.1),
2 => Color::from_hue(1.22), 2 => Color::from_hue(1.22),
_ => Color::GREEN, _ => Color::GREEN,
}; };
canvas[p] = c;
if ctx.cfg.sloweffect.unwrap() { if ctx.cfg.sloweffect.unwrap() {
sleep(Duration::from_millis( sleep(Duration::from_millis(
(SLOWEFFECT_FACTOR * (1.0 / ctx.datamatrix.layout.len() as f32)) as u64, (SLOWEFFECT_FACTOR * (1.0 / ctx.datamatrix.layout.len() as f32)) as u64,
@ -127,24 +125,27 @@ fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) {
} }
fn effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { fn effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) {
for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) { loop {
for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) { for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) {
canvas[msg.pad()] = color; for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) {
println!("{msg:?}", msg = msg.pad()) canvas[msg.pad()] = color;
println!("{msg:?}", msg = msg.pad())
}
canvas.flush().unwrap();
canvas.iter().for_each(|pad| {
let surrounding_color = pad
.neighbors_5()
.iter()
.map(|&p| canvas.get(p).unwrap_or(Color::GREEN))
.sum::<Color>()
/ 5.0
/ 1.05;
canvas[pad] = canvas[pad].mix(surrounding_color, 0.4);
});
} }
canvas.flush().unwrap(); clear(canvas);
canvas.iter().for_each(|pad| {
let surrounding_color = pad
.neighbors_5()
.iter()
.map(|&p| canvas.get(p).unwrap_or(Color::GREEN))
.sum::<Color>()
/ 5.0
/ 1.05;
canvas[pad] = canvas[pad].mix(surrounding_color, 0.4);
});
} }
} }