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,
}
#[derive(Debug, Clone)]
pub enum Mode {
Draw,
Test,
Input,
Effect,
}
#[derive(Debug, Clone)]
pub struct Context {
pub cfg: Config,
configfile: String,
inotify: Inotify,
pub datamatrix: DataMatrix,
pub mode: String,
pub mode: Option<Mode>,
}
impl Context {
@ -37,7 +45,16 @@ impl Context {
configfile: configfile,
inotify: Inotify::init(InitFlags::IN_NONBLOCK).unwrap(),
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);

View File

@ -1,4 +1,4 @@
use crate::config::Context;
use crate::config::{Context, Mode};
use crate::zabbix::api::get_zabbix_problems;
use launchy::Color;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
@ -13,23 +13,22 @@ const ONE_COLOR: f32 = 1.;
const SLOWEFFECT_FACTOR: f32 = 800.0;
pub fn run(ctx: &mut Context) {
match ctx.mode.as_str() {
"draw" => {
match ctx.mode {
Some(Mode::Draw) => {
let (mut canvas, mut _poller) = initpad();
draw(&mut canvas, ctx);
}
"input" => {
Some(Mode::Input) => {
let (mut canvas, mut poller) = initpad();
input(&mut canvas, &mut poller);
}
"effect" => {
Some(Mode::Effect) => {
let (mut canvas, mut poller) = initpad();
effect(&mut canvas, &mut poller)
}
"test" => test(ctx),
_ => {
Some(Mode::Test) => test(ctx),
None => {
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),
y: ((i as i32) / MATRIX_WIDTH) + 1,
};
let c = match j.status {
canvas[p] = match j.status {
5 => Color::RED,
4 => Color::from_hue(0.05),
3 => Color::from_hue(0.1),
2 => Color::from_hue(1.22),
_ => Color::GREEN,
};
canvas[p] = c;
if ctx.cfg.sloweffect.unwrap() {
sleep(Duration::from_millis(
(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) {
for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) {
for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) {
canvas[msg.pad()] = color;
println!("{msg:?}", msg = msg.pad())
loop {
for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) {
for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) {
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();
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);
});
clear(canvas);
}
}