This commit is contained in:
		
							parent
							
								
									5c84875dd9
								
							
						
					
					
						commit
						a4c913eedc
					
				@ -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<DBPool>) -> Result<Option<Animation>
 | 
			
		||||
        return Ok(None);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    println!("got here 2");
 | 
			
		||||
 | 
			
		||||
    let mut animation = next.unwrap()?;
 | 
			
		||||
@ -482,7 +484,6 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
 | 
			
		||||
        })
 | 
			
		||||
    })?;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    println!("got here 4");
 | 
			
		||||
 | 
			
		||||
    while let Some(key_frame_id) = map.next() {
 | 
			
		||||
@ -511,7 +512,6 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
 | 
			
		||||
        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<DBPool>) -> Result<(), Box<dyn Er
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/active")]
 | 
			
		||||
pub async fn get_active(
 | 
			
		||||
    action: &State<ASender<Action>>,
 | 
			
		||||
    messages: &State<AReceiver<RenderMessage>>,
 | 
			
		||||
) -> 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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										55
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								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<String>),
 | 
			
		||||
    // TODO remove none where other are added
 | 
			
		||||
    None
 | 
			
		||||
    None,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/quit")]
 | 
			
		||||
@ -97,42 +97,6 @@ async fn reload(action: &State<ASender<Action>>) -> String {
 | 
			
		||||
    .to_string()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/active")]
 | 
			
		||||
async fn get_active(action: &State<ASender<Action>>, messages: &State<AReceiver<RenderMessage>>) -> 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<Action>,
 | 
			
		||||
@ -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<RenderMessage>, Receiver<RenderMessage>) =
 | 
			
		||||
        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,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user