added clearall
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-04-14 19:01:49 +01:00
parent f8e2aa91ea
commit df820fa7d8
2 changed files with 256 additions and 99 deletions

View File

@ -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<u32>{
pub fn get_start(&self) -> Option<u32> {
self.start
}
pub fn get_end(&self) -> Option<u32>{
pub fn get_end(&self) -> Option<u32> {
self.end
}
pub fn get_tags(&self) -> Option<String>{
pub fn get_tags(&self) -> Option<String> {
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<LigthSetting>
settings: Vec<LigthSetting>,
}
impl KeyFrame {
@ -71,7 +76,6 @@ pub struct Animation {
}
impl Animation {
pub fn get_frames(&self) -> Vec<KeyFrame> {
self.key_frames.clone()
}
@ -81,7 +85,11 @@ impl Animation {
}
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= "<data>")]
#[post("/animation", data = "<data>")]
pub async fn animation(data: Json<Animation>, db: &State<DBPool>) -> 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<Animation>, db: &State<DBPool>) -> 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<Option<u32>, Box<dyn Error>> {
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<Option<u32>, B
Ok(next.unwrap()?)
}
fn get_keyframe_from_index(conn: &DBConn, animation: u32, index: u32) -> Result<Option<u32>, Box<dyn Error>> {
fn get_keyframe_from_index(
conn: &DBConn,
animation: u32,
index: u32,
) -> Result<Option<u32>, Box<dyn Error>> {
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<Animation>, pool: &State<DBPool>) -> Result<String, Box<dyn Error>>{
let priority = if data.priority.is_some() { data.priority.unwrap() } else { 0 };
fn add_animation_to_db(
data: Json<Animation>,
pool: &State<DBPool>,
) -> Result<String, Box<dyn Error>> {
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<Animation>, pool: &State<DBPool>) -> Result<St
println!("t: {:?}", name);
if name.is_some() {
return Ok(json!(ApiResponse {code: 500, message: "Animation with that name is already exists.".to_string()}).to_string());
return Ok(json!(ApiResponse {
code: 500,
message: "Animation with that name is already exists.".to_string()
})
.to_string());
}
conn.execute("INSERT INTO animation (priority, name, repeat) VALUES (?1, ?2, ?3);", (priority, data.name.to_string(), data.repeat ))?;
conn.execute(
"INSERT INTO animation (priority, name, repeat) VALUES (?1, ?2, ?3);",
(priority, data.name.to_string(), data.repeat),
)?;
let name = get_animation_from_name(&conn, data.name.to_string())?;
if name.is_none() {
return Ok(json!(ApiResponse {code: 500, message: "Failed to create animation.".to_string()}).to_string());
return Ok(json!(ApiResponse {
code: 500,
message: "Failed to create animation.".to_string()
})
.to_string());
}
let animation_id = name.unwrap();
let mut i = 0;
for key_frame in data.key_frames.clone() {
conn.execute("INSERT INTO keyframe (duration, animation, i) VALUES (?1, ?2, ?3);", (key_frame.duration, animation_id, i))?;
conn.execute(
"INSERT INTO keyframe (duration, animation, i) VALUES (?1, ?2, ?3);",
(key_frame.duration, animation_id, i),
)?;
let keyframe = get_keyframe_from_index(&conn, animation_id, i)?;
if keyframe.is_none() {
return Ok(json!(ApiResponse {code: 500, message: "Failed to create animation.".to_string()}).to_string());
return Ok(json!(ApiResponse {
code: 500,
message: "Failed to create animation.".to_string()
})
.to_string());
}
let keyframe = keyframe.unwrap();
@ -215,45 +279,70 @@ fn add_animation_to_db(data: Json<Animation>, pool: &State<DBPool>) -> Result<St
*/
#[get("/stop/<name>")]
pub async fn stop_animation(name: &str, stop_sender: &State<ASender<String>>) -> 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/<name>")]
pub async fn start_animation(name: &str, start_sender: &State<ASender<Animation>>, db: &State<DBPool>) -> String {
pub async fn start_animation(
name: &str,
start_sender: &State<ASender<Animation>>,
db: &State<DBPool>,
) -> 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()
}
/**
@ -262,58 +351,103 @@ pub async fn start_animation(name: &str, start_sender: &State<ASender<Animation>
*
*/
#[get("/clear/<name>")]
pub async fn clear(name: &str, start_sender: &State<ASender<Animation>>, db: &State<DBPool>, action: &State<ASender<Action>>) -> String {
pub async fn clear(
name: &str,
start_sender: &State<ASender<Animation>>,
db: &State<DBPool>,
action: &State<ASender<Action>>,
) -> 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<ASender<Action>>) -> 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<DBPool>) -> Result<Option<Animation>, Box<dyn Error>> {
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<DBPool>) -> Result<Option<Animation>
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<DBPool>) -> Result<Option<Animation>
tags: row.get(2)?,
r: row.get(3)?,
b: row.get(4)?,
g: row.get(5)?
g: row.get(5)?,
})
})?;
@ -371,18 +507,24 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
*/
#[get("/delete/<name>")]
pub async fn delete(name: &str, db: &State<DBPool>) -> 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<DBPool>) -> Result<(), Box<dyn Error>> {
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])?;

View File

@ -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<r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>>;
@ -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<ASender<Action>>) -> 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<ASender<Action>>) -> 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");