From a4c913eedcffef1b758c4c239899a78e4044fd71 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sat, 1 Jul 2023 23:16:52 +0100 Subject: [PATCH] chore: mount new path --- src/animations.rs | 50 ++++++++++++++++++++++++++++++++++++++---- src/main.rs | 55 ++++++++++------------------------------------- 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 25cd049..2996d8a 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -9,7 +9,10 @@ use rocket::{ }; use crate::DBPool; -use crate::{utils::ApiResponse, ASender, Action, DBConn}; +use crate::{ + utils::{ApiResponse, ApiResponseActiveList}, + AReceiver, ASender, Action, DBConn, RenderMessage, +}; #[derive(Debug, Deserialize, Serialize, Clone)] #[serde(crate = "rocket::serde")] @@ -457,7 +460,6 @@ pub fn get_animation(name: &str, db: &State) -> Result return Ok(None); } - println!("got here 2"); let mut animation = next.unwrap()?; @@ -482,7 +484,6 @@ pub fn get_animation(name: &str, db: &State) -> Result }) })?; - println!("got here 4"); while let Some(key_frame_id) = map.next() { @@ -511,7 +512,6 @@ pub fn get_animation(name: &str, db: &State) -> Result animation.animation.key_frames.push(key_frame_id.keyframe); } - println!("got here 6"); Ok(Some(animation.animation)) @@ -550,3 +550,45 @@ pub fn remove_animation(name: &str, db: &State) -> Result<(), Box>, + messages: &State>, +) -> String { + println!("Getting active"); + + let send = action.lock().unwrap().send(Action::GetActiveList); + + if send.is_err() || send.ok().is_none() { + return json!(ApiResponse { + code: 500, + message: "Failed to get list".to_string() + }) + .to_string(); + } + + let data = messages.lock().unwrap().recv(); + + if data.is_err() || data.as_ref().ok().is_none() { + return json!(ApiResponse { + code: 500, + message: "Failed to get list".to_string() + }) + .to_string(); + } + + if let RenderMessage::ActiveList(list) = data.ok().unwrap() { + return json!(ApiResponseActiveList { + code: 200, + active_animations: list + }) + .to_string(); + } + + return json!(ApiResponse { + code: 500, + message: "Failed to get list".to_string() + }) + .to_string(); +} diff --git a/src/main.rs b/src/main.rs index b66c3ba..7fd3602 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,15 +5,15 @@ use ::std::sync::Arc; mod animations; mod configure; +mod db; mod render; mod utils; -mod db; use animations::Animation; use r2d2::PooledConnection; -use utils::{ApiResponse, ApiResponseActiveList}; +use utils::ApiResponse; -use rocket::{serde::json::{serde_json::json, self}, State}; +use rocket::{serde::json::serde_json::json, State}; use r2d2_sqlite::SqliteConnectionManager; @@ -50,7 +50,7 @@ pub enum Action { pub enum RenderMessage { ActiveList(Vec), // TODO remove none where other are added - None + None, } #[get("/quit")] @@ -97,42 +97,6 @@ async fn reload(action: &State>) -> String { .to_string() } -#[get("/active")] -async fn get_active(action: &State>, messages: &State>) -> String { - println!("Getting active"); - - let send = action.lock().unwrap().send(Action::GetActiveList); - - if send.is_err() || send.ok().is_none() { - return json!(ApiResponse{ - code: 500, - message: "Failed to get list".to_string() - }).to_string(); - } - - let data = messages.lock().unwrap().recv(); - - if data.is_err() || data.as_ref().ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Failed to get list".to_string() - }).to_string(); - } - - if let RenderMessage::ActiveList(list) = data.ok().unwrap() { - return json!(ApiResponseActiveList { - code: 200, - active_animations: list - }).to_string(); - } - - return json!(ApiResponse { - code: 500, - message: "Failed to get list".to_string() - }).to_string(); - -} - fn ligth_controll( pool: DBPool, action: Receiver, @@ -171,7 +135,10 @@ fn ligth_controll( break 'mainloop; } Action::GetActiveList => { - if message_server.send(RenderMessage::ActiveList(render.get_active_animations())).is_err() { + if message_server + .send(RenderMessage::ActiveList(render.get_active_animations())) + .is_err() + { println!("Failed to send message ups"); } } @@ -199,7 +166,6 @@ fn ligth_controll( println!("stoped main loop"); } - #[rocket::main] async fn main() -> GResult { println!("Program start"); @@ -225,7 +191,7 @@ async fn main() -> GResult { mpsc::channel(); let (messager_sender, messager_revicer): (Sender, Receiver) = - mpsc::channel(); + mpsc::channel(); let pool_clone = conn_pool_arc.clone(); @@ -235,7 +201,7 @@ async fn main() -> GResult { action_receiver, start_animation_receiver, stop_animation_receiver, - messager_sender + messager_sender, ); }); @@ -251,6 +217,7 @@ async fn main() -> GResult { quit, reload, configure::configure, + animations::get_active, animations::animation, animations::start_animation, animations::stop_animation,