From 99e8a2568663f07933801cce008ea65b0e1bc171 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 2 Jul 2023 17:56:40 +0100 Subject: [PATCH] chore: moved to only one message --- src/animations.rs | 14 +++++++------- src/main.rs | 45 +++++++++++++-------------------------------- 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 521e9ca..5166a95 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -308,7 +308,7 @@ pub async fn stop_animation(name: &str, stop_sender: &State>) -> #[get("/start/")] pub async fn start_animation( name: &str, - start_sender: &State>, + action: &State>, db: &State, ) -> String { println!("try to get animation!"); @@ -335,7 +335,7 @@ pub async fn start_animation( let animation = animation.unwrap(); - let r = start_sender.lock().unwrap().send(animation); + let r = action.lock().unwrap().send(Action::Start(animation)); if r.is_err() || r.as_ref().ok().is_none() { return json!(ApiResponse { code: 500, @@ -658,10 +658,8 @@ fn get_error_message(message: &str) -> String { #[get("/toggle/")] pub async fn toggle_animation( name: &str, - start_sender: &State>, action: &State>, messages: &State>, - stop_sender: &State>, db: &State, ) -> String { let animation = get_animation_or_string_error(name, db); @@ -670,7 +668,9 @@ pub async fn toggle_animation( } let animation = animation.unwrap(); - let send = action.lock().unwrap().send(Action::GetActiveList); + let action = action.lock().unwrap(); + + let send = action.send(Action::GetActiveList); if send.is_err() || send.ok().is_none() { return get_error_message("Failed to get list"); } @@ -683,7 +683,7 @@ pub async fn toggle_animation( if let RenderMessage::ActiveList(list) = data.ok().unwrap() { if list.contains(&name.to_string()) { // Disable - let r = stop_sender.lock().unwrap().send(name.to_string()); + let r = action.send(Action::Stop(name.to_string())); if r.is_err() || r.ok().is_none() { return get_error_message("Something went wrong"); @@ -692,7 +692,7 @@ pub async fn toggle_animation( return create_message(200, "Configuration was successful"); } else { // Enable - let r = start_sender.lock().unwrap().send(animation); + let r = action.send(Action::Start(animation)); if r.is_err() || r.as_ref().ok().is_none() { return get_error_message("Probelms with the sender"); } diff --git a/src/main.rs b/src/main.rs index d879ecf..531b534 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,11 @@ fn index() -> &'static str { pub enum Action { Reload, - Stop, + Exit, Clear, GetActiveList, + Start(Animation), + Stop(String), } pub enum RenderMessage { @@ -54,7 +56,7 @@ pub enum RenderMessage { async fn quit(shutdown: rocket::Shutdown, action: &State>) -> String { println!("Got Shutdown"); - let send = action.lock().unwrap().send(Action::Stop); + let send = action.lock().unwrap().send(Action::Exit); if send.is_err() || send.ok().is_none() { return json!(ApiResponse { @@ -97,8 +99,6 @@ async fn reload(action: &State>) -> String { fn ligth_controll( pool: DBPool, action: Receiver, - start_ani: Receiver, - stop_ani: Receiver, message_server: Sender, ) { println!("Data loop started"); @@ -115,8 +115,8 @@ fn ligth_controll( 'mainloop: loop { let action = action.try_recv(); - if action.is_ok() { - match action.ok().unwrap() { + if let Ok(action) = action { + match action { Action::Reload => { let result = render.load_config(); if result.is_err() || result.as_ref().ok().is_none() { @@ -126,7 +126,7 @@ fn ligth_controll( Action::Clear => { render.blank(); } - Action::Stop => { + Action::Exit => { render.blank(); render.im_render(); break 'mainloop; @@ -139,24 +139,15 @@ fn ligth_controll( println!("Failed to send message ups"); } } + Action::Start(animation) => { + render.add_animation(animation); + } + Action::Stop(animation) => { + render.remove_animation(animation); + } } } - let ani = start_ani.try_recv(); - - if ani.is_ok() { - println!("Added animation"); - //TODO - render.add_animation(ani.ok().unwrap()); - //ani.ok().unwrap(). - } - - let ani = stop_ani.try_recv(); - - if ani.is_ok() { - render.remove_animation(ani.ok().unwrap()); - } - render.render(); } @@ -180,12 +171,6 @@ async fn main() -> GResult { } let (action_sender, action_receiver): (Sender, Receiver) = mpsc::channel(); - let (start_animation_sender, start_animation_receiver): ( - Sender, - Receiver, - ) = mpsc::channel(); - let (stop_animation_sender, stop_animation_receiver): (Sender, Receiver) = - mpsc::channel(); let (messager_sender, messager_revicer): (Sender, Receiver) = mpsc::channel(); @@ -196,8 +181,6 @@ async fn main() -> GResult { ligth_controll( pool_clone, action_receiver, - start_animation_receiver, - stop_animation_receiver, messager_sender, ); }); @@ -227,8 +210,6 @@ async fn main() -> GResult { ) .manage(conn_pool_arc.clone()) .manage(Arc::new(Mutex::new(action_sender))) - .manage(Arc::new(Mutex::new(start_animation_sender))) - .manage(Arc::new(Mutex::new(stop_animation_sender))) .manage(Arc::new(Mutex::new(messager_revicer))) .launch() .await?;