From df820fa7d87af2c6f3d49f0a42dbac32f43af13f Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Fri, 14 Apr 2023 19:01:49 +0100 Subject: [PATCH] added clearall --- src/animations.rs | 318 +++++++++++++++++++++++++++++++++------------- src/main.rs | 37 ++++-- 2 files changed, 256 insertions(+), 99 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 01a2811..f533c9b 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -1,9 +1,15 @@ -use std::{u8, error::Error}; +use std::{error::Error, u8}; -use rocket::{serde::{json::{Json, serde_json::json}, Deserialize, Serialize}, State}; +use rocket::{ + serde::{ + json::{serde_json::json, Json}, + Deserialize, Serialize, + }, + State, +}; -use crate::{utils::ApiResponse, DBConn, ASender, Action}; use crate::DBPool; +use crate::{utils::ApiResponse, ASender, Action, DBConn}; #[derive(Debug, Deserialize, Serialize, Clone)] #[serde(crate = "rocket::serde")] @@ -18,13 +24,13 @@ pub struct LigthSetting { } impl LigthSetting { - pub fn get_start(&self) -> Option{ + pub fn get_start(&self) -> Option { self.start } - pub fn get_end(&self) -> Option{ + pub fn get_end(&self) -> Option { self.end } - pub fn get_tags(&self) -> Option{ + pub fn get_tags(&self) -> Option { if self.tags.as_ref().is_some() { return Some(self.tags.as_ref().unwrap().to_string()); } @@ -41,13 +47,12 @@ impl LigthSetting { } } - #[derive(Debug, Deserialize, Serialize, Clone)] #[serde(crate = "rocket::serde")] #[serde(rename_all = "camelCase")] pub struct KeyFrame { duration: u32, - settings: Vec + settings: Vec, } impl KeyFrame { @@ -56,7 +61,7 @@ impl KeyFrame { } pub fn get_duration(&self) -> u32 { - self.duration + self.duration } } @@ -71,7 +76,6 @@ pub struct Animation { } impl Animation { - pub fn get_frames(&self) -> Vec { self.key_frames.clone() } @@ -79,9 +83,13 @@ impl Animation { pub fn get_name(&self) -> String { self.name.to_string() } - + pub fn get_priority(&self) -> u32 { - if self.priority.is_none() { 0 } else {self.priority.unwrap()} + if self.priority.is_none() { + 0 + } else { + self.priority.unwrap() + } } pub fn get_repeat(&self) -> bool { @@ -89,42 +97,69 @@ impl Animation { } } -#[post("/animation", data= "")] +#[post("/animation", data = "")] pub async fn animation(data: Json, db: &State) -> String { - if data.key_frames.is_empty() { - return json!(ApiResponse {code: 400, message: "KeyFrame are nessesary".to_string()}).to_string(); - } + return json!(ApiResponse { + code: 400, + message: "KeyFrame are nessesary".to_string() + }) + .to_string(); + } if data.name.is_empty() { - return json!(ApiResponse {code: 400, message: "Name must not be empty".to_string()}).to_string(); + return json!(ApiResponse { + code: 400, + message: "Name must not be empty".to_string() + }) + .to_string(); } for key_frame in data.key_frames.iter() { if key_frame.settings.is_empty() { - return json!(ApiResponse {code: 400, message: "keyFrames.settings can not be empty".to_string()}).to_string(); + return json!(ApiResponse { + code: 400, + message: "keyFrames.settings can not be empty".to_string() + }) + .to_string(); } if key_frame.duration == 0 { - return json!(ApiResponse {code: 400, message: "keyFrames.duration can not be 0".to_string()}).to_string(); + return json!(ApiResponse { + code: 400, + message: "keyFrames.duration can not be 0".to_string() + }) + .to_string(); } for setting in key_frame.settings.iter() { - if setting.tags.is_none() && (setting.start.is_none() || setting.end.is_none()) { - return json!(ApiResponse {code: 400, message: "Either tags or start end pair is needed".to_string()}).to_string(); + return json!(ApiResponse { + code: 400, + message: "Either tags or start end pair is needed".to_string() + }) + .to_string(); } - if !(setting.start.is_none() && setting.end.is_none()) && (setting.start.is_none() || setting.end.is_none()) { - return json!(ApiResponse {code: 400, message: "Both start and end need to be filled".to_string()}).to_string(); + if !(setting.start.is_none() && setting.end.is_none()) + && (setting.start.is_none() || setting.end.is_none()) + { + return json!(ApiResponse { + code: 400, + message: "Both start and end need to be filled".to_string() + }) + .to_string(); } - } } let db_status = add_animation_to_db(data, db); if db_status.is_err() || db_status.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: format!("Error setting the animation. Err: {:?}", db_status.err()).to_string()}).to_string(); + return json!(ApiResponse { + code: 500, + message: format!("Error setting the animation. Err: {:?}", db_status.err()).to_string() + }) + .to_string(); } let db_status = db_status.ok().unwrap(); @@ -133,15 +168,17 @@ pub async fn animation(data: Json, db: &State) -> String { return db_status; } - json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Configuration was successful".to_string() + }) + .to_string() } fn get_animation_from_name(conn: &DBConn, name: String) -> Result, Box> { let mut stmt = conn.prepare("SELECT id FROM animation WHERE name=?1")?; - let next = stmt.query_map([name], |row| { - row.get(0) - })?.next(); + let next = stmt.query_map([name], |row| row.get(0))?.next(); if next.is_none() { return Ok(None); @@ -150,12 +187,14 @@ fn get_animation_from_name(conn: &DBConn, name: String) -> Result, B Ok(next.unwrap()?) } -fn get_keyframe_from_index(conn: &DBConn, animation: u32, index: u32) -> Result, Box> { +fn get_keyframe_from_index( + conn: &DBConn, + animation: u32, + index: u32, +) -> Result, Box> { let mut stmt = conn.prepare("SELECT id FROM keyframe WHERE animation=?1 AND i=?2")?; - let next = stmt.query_map([animation, index], |row| { - row.get(0) - })?.next(); + let next = stmt.query_map([animation, index], |row| row.get(0))?.next(); if next.is_none() { return Ok(None); @@ -164,8 +203,15 @@ fn get_keyframe_from_index(conn: &DBConn, animation: u32, index: u32) -> Result< Ok(next.unwrap()?) } -fn add_animation_to_db(data: Json, pool: &State) -> Result>{ - let priority = if data.priority.is_some() { data.priority.unwrap() } else { 0 }; +fn add_animation_to_db( + data: Json, + pool: &State, +) -> Result> { + let priority = if data.priority.is_some() { + data.priority.unwrap() + } else { + 0 + }; let conn = pool.get()?; @@ -176,25 +222,43 @@ fn add_animation_to_db(data: Json, pool: &State) -> Result, pool: &State) -> Result")] pub async fn stop_animation(name: &str, stop_sender: &State>) -> String { - let r = stop_sender.lock().unwrap().send(name.to_string()); if r.is_err() || r.ok().is_none() { - return json!(ApiResponse {code: 500, message: "Something went wrong".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Something went wrong".to_string() + }) + .to_string(); } - json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Configuration was successful".to_string() + }) + .to_string() } - /** - * + * * Start animations - * + * */ #[get("/start/")] -pub async fn start_animation(name: &str, start_sender: &State>, db: &State) -> String { - +pub async fn start_animation( + name: &str, + start_sender: &State>, + db: &State, +) -> String { let animation = get_animation(name, db); if animation.is_err() || animation.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: format!("Probelms with the db: {:?}", animation.err()).to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: format!("Probelms with the db: {:?}", animation.err()).to_string() + }) + .to_string(); } let animation = animation.ok().unwrap(); if animation.is_none() { - return json!(ApiResponse {code: 404, message: "Animation not found".to_string()}).to_string() + return json!(ApiResponse { + code: 404, + message: "Animation not found".to_string() + }) + .to_string(); } let animation = animation.unwrap(); let r = start_sender.lock().unwrap().send(animation); if r.is_err() || r.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: "Probelms with the db".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Probelms with the db".to_string() + }) + .to_string(); } - json!(ApiResponse {code: 200, message: "Started animation".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Started animation".to_string() + }) + .to_string() } /** - * + * * Clear animations - * + * */ #[get("/clear/")] -pub async fn clear(name: &str, start_sender: &State>, db: &State, action: &State>) -> String { - +pub async fn clear( + name: &str, + start_sender: &State>, + db: &State, + action: &State>, +) -> String { let r = action.lock().unwrap().send(Action::Clear); if r.is_err() || r.ok().is_none() { - return json!(ApiResponse {code: 500, message: "Something went wrong".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Something went wrong".to_string() + }) + .to_string(); } let animation = get_animation(name, db); if animation.is_err() || animation.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: format!("Probelms with the db: {:?}", animation.err()).to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: format!("Probelms with the db: {:?}", animation.err()).to_string() + }) + .to_string(); } let animation = animation.ok().unwrap(); if animation.is_none() { - return json!(ApiResponse {code: 404, message: "Animation not found".to_string()}).to_string() + return json!(ApiResponse { + code: 404, + message: "Animation not found".to_string() + }) + .to_string(); } let animation = animation.unwrap(); let r = start_sender.lock().unwrap().send(animation); if r.is_err() || r.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: "Probelms with the db".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Probelms with the db".to_string() + }) + .to_string(); } - json!(ApiResponse {code: 200, message: "Started animation".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Started animation".to_string() + }) + .to_string() +} + +#[get("/clearall")] +pub async fn clearAll(action: &State>) -> String { + let r = action.lock().unwrap().send(Action::Clear); + + if r.is_err() || r.ok().is_none() { + return json!(ApiResponse { + code: 500, + message: "Something went wrong".to_string() + }) + .to_string(); + } + + json!(ApiResponse { + code: 200, + message: "Cleared all the animations".to_string() + }) + .to_string() } pub fn get_animation(name: &str, db: &State) -> Result, Box> { - let conn = db.get()?; - let mut stmt = conn.prepare("SELECT id, priority, name, repeat FROM animation WHERE name=?1;")?; + let mut stmt = + conn.prepare("SELECT id, priority, name, repeat FROM animation WHERE name=?1;")?; struct AnimationId { animation: Animation, - id: u8 + id: u8, } - let next = stmt.query_map([name], |row| { - Ok(AnimationId { - animation: Animation { - key_frames: Vec::new(), - priority: Some(row.get(1)?), - name: row.get(2)?, - repeat: row.get(3)? - }, - id: row.get(0)? - }) - })?.next(); + let next = stmt + .query_map([name], |row| { + Ok(AnimationId { + animation: Animation { + key_frames: Vec::new(), + priority: Some(row.get(1)?), + name: row.get(2)?, + repeat: row.get(3)?, + }, + id: row.get(0)?, + }) + })? + .next(); if next.is_none() { return Ok(None); @@ -326,22 +460,24 @@ pub fn get_animation(name: &str, db: &State) -> Result id: u8, } - let mut stmt = conn.prepare("SELECT id, duration from keyframe where animation=?1 order by i asc")?; + let mut stmt = + conn.prepare("SELECT id, duration from keyframe where animation=?1 order by i asc")?; let mut map = stmt.query_map([animation.id], |row| { Ok(KeyFrameId { id: row.get(0)?, keyframe: KeyFrame { duration: row.get(1)?, - settings: Vec::new() - } + settings: Vec::new(), + }, }) })?; while let Some(key_frame_id) = map.next() { let mut key_frame_id = key_frame_id?; - let mut stmt = conn.prepare("SELECT start, end, tags, r, b, g from lightsetting where keyframe=?1")?; + let mut stmt = + conn.prepare("SELECT start, end, tags, r, b, g from lightsetting where keyframe=?1")?; let mut light_setting_map = stmt.query_map([key_frame_id.id], |row| { Ok(LigthSetting { @@ -350,7 +486,7 @@ pub fn get_animation(name: &str, db: &State) -> Result tags: row.get(2)?, r: row.get(3)?, b: row.get(4)?, - g: row.get(5)? + g: row.get(5)?, }) })?; @@ -365,24 +501,30 @@ pub fn get_animation(name: &str, db: &State) -> Result } /** - * + * * Delete animations - * + * */ #[get("/delete/")] pub async fn delete(name: &str, db: &State) -> String { - let animation = remove_animation(name, db); if animation.is_err() || animation.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: format!("Probelms with the db: {:?}", animation.err()).to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: format!("Probelms with the db: {:?}", animation.err()).to_string() + }) + .to_string(); } - json!(ApiResponse {code: 200, message: "Deleted animation".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Deleted animation".to_string() + }) + .to_string() } pub fn remove_animation(name: &str, db: &State) -> Result<(), Box> { - let conn = db.get()?; conn.execute("delete from lightsetting where id in (select l.id from lightsetting as l left join keyframe as k on l.keyframe = k.id left join animation as a on k.animation = a.id where a.name=?1);", [name])?; diff --git a/src/main.rs b/src/main.rs index f414c7e..815e82d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use animations::Animation; use r2d2::PooledConnection; use utils::ApiResponse; -use rocket::{State, serde::json::serde_json::json}; +use rocket::{serde::json::serde_json::json, State}; use r2d2_sqlite::SqliteConnectionManager; @@ -23,7 +23,7 @@ use std::{ sync::{ mpsc::{self, Receiver, Sender}, Mutex, - } + }, }; pub type DBPool = Arc>; @@ -39,7 +39,7 @@ fn index() -> &'static str { pub enum Action { Reload, Stop, - Clear + Clear, } #[get("/quit")] @@ -49,12 +49,20 @@ async fn quit(shutdown: rocket::Shutdown, action: &State>) -> St let send = action.lock().unwrap().send(Action::Stop); if send.is_err() || send.ok().is_none() { - return json!(ApiResponse {code: 500, message: "Failed to stop".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Failed to stop".to_string() + }) + .to_string(); } shutdown.notify(); - json!(ApiResponse {code: 200, message: "Stoped".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Stoped".to_string() + }) + .to_string() } #[get("/reload")] @@ -64,10 +72,18 @@ async fn reload(action: &State>) -> String { let send = action.lock().unwrap().send(Action::Reload); if send.is_err() || send.ok().is_none() { - return json!(ApiResponse {code: 500, message: "Failed to reload".to_string()}).to_string() + return json!(ApiResponse { + code: 500, + message: "Failed to reload".to_string() + }) + .to_string(); } - json!(ApiResponse {code: 200, message: "Reloaded".to_string()}).to_string() + json!(ApiResponse { + code: 200, + message: "Reloaded".to_string() + }) + .to_string() } fn ligth_controll( @@ -97,15 +113,15 @@ fn ligth_controll( if result.is_err() || result.as_ref().ok().is_none() { println!("Ehh failed to load config i guess: {:?}", result.err()); } - }, + } Action::Clear => { render.blank(); - }, + } Action::Stop => { render.blank(); render.im_render(); break 'mainloop; - }, + } } } @@ -125,7 +141,6 @@ fn ligth_controll( } render.render(); - } println!("stoped main loop");