From 473883f6dc62a6e62398df4c882e7f24c99cae69 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Fri, 21 Jan 2022 00:05:26 +0100 Subject: [PATCH] updated zabbixlaunch (non working async example) --- src/config/mod.rs | 56 +++++++++++++++----------- src/padcontrol/mod.rs | 91 +++++++++++++++++++++++++------------------ 2 files changed, 87 insertions(+), 60 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index e0df7fe..840a9a5 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -50,13 +50,12 @@ impl Context { ctx.inotify .add_watch(ctx.configfile.as_str(), AddWatchFlags::IN_MODIFY) .unwrap(); - let tokenrefresh = get_zabbix_authtoken(&mut ctx.cfg); - tokenrefresh.await; + get_zabbix_authtoken(&mut ctx.cfg).await; ctx } - pub async fn hotreload(&mut self) { + /*pub async fn hotreload(&mut self) { let events = match self.inotify.read_events() { Ok(ev) => ev, Err(_) => vec![], @@ -66,6 +65,20 @@ impl Context { get_zabbix_authtoken(&mut self.cfg).await; self.cfg.load(self.configfile.as_str()); } + std::thread::sleep(std::time::Duration::from_secs(5)); + }*/ +} + +pub async fn hotreload(rwctx: &tokio::sync::RwLock) { + let mut ctx = rwctx.write().await; + let configfile = ctx.configfile.clone(); + let events = match ctx.inotify.read_events() { + Ok(ev) => ev, + Err(_) => vec![], + }; + if events.len() > 0 { + println!("Reloading {cfg}", cfg = ctx.configfile); + ctx.cfg.load(configfile.as_str()).await; } } @@ -74,6 +87,7 @@ pub struct Config { pub server: String, pub username: String, pub password: String, + #[serde(skip_serializing)] pub authtoken: Option, pub sloweffect: Option, pub refresh: Option, @@ -93,20 +107,8 @@ impl<'a> Config { } } - async fn load(&'a mut self, configfile: &str) { - let fileopen: Result; - let filemeta = std::fs::metadata(configfile); - let fileexists = match filemeta { - Ok(_) => true, - Err(_) => false, - }; - - if !fileexists { - let _a = File::create(configfile); - } - fileopen = File::open(configfile); - - let mut file = match fileopen { + async fn load(&mut self, configfile: &str) { + let mut file = match File::open(configfile) { Ok(f) => f, Err(err) => { panic!("{err}", err = err); @@ -132,9 +134,9 @@ impl<'a> Config { .unwrap_or(tmpcfg.password.as_str()) .to_string(), authtoken: Some( - cfg["authtoken"] - .as_str() - .unwrap_or(tmpcfg.authtoken.unwrap().as_str()) + self.authtoken + .clone() + .unwrap_or(tmpcfg.authtoken.unwrap().to_string()) .to_string(), ), sloweffect: Some( @@ -151,11 +153,21 @@ impl<'a> Config { Config::new() } }; - self.save(&configfile); + self.save(&configfile).await; } async fn save(&self, configfile: &str) { - let file = File::create(configfile).unwrap(); + let file: File; + let filemeta = std::fs::metadata(configfile); + let fileexists = match filemeta { + Ok(_) => true, + Err(_) => false, + }; + if fileexists { + file = File::create(configfile).unwrap(); + } else { + file = File::open(configfile).unwrap(); + } serde_json::to_writer_pretty(file, &self).unwrap(); } } diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index 22fe54b..75e39fd 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -1,11 +1,13 @@ -use crate::config::Context; +use crate::config::{hotreload, Context}; use crate::zabbix::api::*; use launchy::Color; use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad}; //use std::process::exit; //use std::sync::mpsc::{channel, Receiver, Sender}; +use std::sync::Arc; use std::thread::sleep; use std::time::Duration; +use tokio::sync::RwLock; const MATRIX_WIDTH: i32 = 8; const MATRIX_SIZE: i32 = MATRIX_WIDTH * MATRIX_WIDTH; @@ -15,39 +17,32 @@ const ONE_COLOR: f32 = 1.; pub async fn run() { println!("test1 🦀"); - let mut ctx = Context::new().await; + let ctx = Context::new().await; println!("test2 🦀"); + let e = Arc::new(RwLock::new(ctx)); + let mutedctx = e.clone(); + println!("test3 🦀{}", e); loop { - ctx.hotreload().await; - test(&mut ctx).await; + println!("test4 🦀"); + tokio::spawn(async move { test(&mutedctx) }); + println!("test5 🦀"); + /*tokio::select! { + _ = test(&mutedctx) => { + println!("fn test 🦀 {:?}", mutedctx); + } + _ = hotreload(&mutedctx) => { + println!("fn hotreload 🦀 {:?}", mutedctx); + } + };*/ } - /* - let (send, recv): (Sender, Receiver) = channel(); - match ctx.mode.as_str() { - /*"draw" => { - let (mut canvas, mut _poller) = initpad(); - draw(&mut canvas, c); - } - "input" => { - let (mut canvas, mut poller) = initpad(); - input(&mut canvas, &mut poller); - } - "effect" => { - let (mut canvas, mut poller) = initpad(); - effect(&mut canvas, &mut poller); - }*/ - "test" => loop { - let mut data = cm.lock().unwrap(); - let future1 = test(&mut data); - let mut data = cm.lock().unwrap(); - let future2 = data.hotreload(); - //futures::join!(future1, future2); - }, - _ => { - println!("No valid option choosen for mode"); - std::process::exit(1) - } - }*/ +} + +async fn test(roctx: &'static tokio::sync::RwLock) { + let ctx = roctx.read().await; + println!("{:?}", ctx); + let zabbix_data = get_zabbix_problems(&ctx.cfg).await.unwrap(); + println!("{} {:?}", zabbix_data, ctx.cfg); + sleep(Duration::from_secs(ctx.cfg.refresh.unwrap())); } fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { @@ -76,13 +71,6 @@ fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { } } -async fn test(ctx: &mut Context) { - let sec = ctx.cfg.refresh.unwrap(); - let zabbix_data = get_zabbix_problems(&ctx.cfg).await.unwrap(); - println!("{}", zabbix_data); - sleep(Duration::from_secs(sec)); -} - /* async fn draw(canvas: &mut CanvasLayout<'_>, ctx: &mut Context) { println!("Refresh rate is {} seconds", ctx.cfg.refresh.unwrap()); @@ -162,3 +150,30 @@ fn clear(canvas: &mut CanvasLayout) { } let _res = canvas.flush(); } + +/* +let (send, recv): (Sender, Receiver) = channel(); +match ctx.mode.as_str() { + /*"draw" => { + let (mut canvas, mut _poller) = initpad(); + draw(&mut canvas, c); + } + "input" => { + let (mut canvas, mut poller) = initpad(); + input(&mut canvas, &mut poller); + } + "effect" => { + let (mut canvas, mut poller) = initpad(); + effect(&mut canvas, &mut poller); + }*/ "test" => loop { + let mut data = cm.lock().unwrap(); + let future1 = test(&mut data); + let mut data = cm.lock().unwrap(); + let future2 = data.hotreload(); + //futures::join!(future1, future2); + }, + _ => { + println!("No valid option choosen for mode"); + std::process::exit(1) + } +}*/