chore: moved to only one message
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Andre Henriques 2023-07-02 17:56:40 +01:00
parent 390b30d084
commit 99e8a25686
2 changed files with 20 additions and 39 deletions

View File

@ -308,7 +308,7 @@ pub async fn stop_animation(name: &str, stop_sender: &State<ASender<String>>) ->
#[get("/start/<name>")]
pub async fn start_animation(
name: &str,
start_sender: &State<ASender<Animation>>,
action: &State<ASender<Action>>,
db: &State<DBPool>,
) -> 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/<name>")]
pub async fn toggle_animation(
name: &str,
start_sender: &State<ASender<Animation>>,
action: &State<ASender<Action>>,
messages: &State<AReceiver<RenderMessage>>,
stop_sender: &State<ASender<String>>,
db: &State<DBPool>,
) -> 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");
}

View File

@ -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<ASender<Action>>) -> 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<ASender<Action>>) -> String {
fn ligth_controll(
pool: DBPool,
action: Receiver<Action>,
start_ani: Receiver<Animation>,
stop_ani: Receiver<String>,
message_server: Sender<RenderMessage>,
) {
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<Action>, Receiver<Action>) = mpsc::channel();
let (start_animation_sender, start_animation_receiver): (
Sender<Animation>,
Receiver<Animation>,
) = mpsc::channel();
let (stop_animation_sender, stop_animation_receiver): (Sender<String>, Receiver<String>) =
mpsc::channel();
let (messager_sender, messager_revicer): (Sender<RenderMessage>, Receiver<RenderMessage>) =
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?;