From a12c503e812ed6e4eab191411819d13e4935c50e Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 15 Jan 2023 19:35:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=81=9C=E6=AD=A2=E8=B7=9F=E9=9A=8F?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E6=97=B6=EF=BC=8C=E7=A8=8B=E5=BA=8F=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/core/core.rs | 159 +++++++++++++++++-------------------- src-tauri/src/main.rs | 2 +- 2 files changed, 75 insertions(+), 86 deletions(-) diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index b33abcd..931a854 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -1,14 +1,12 @@ use futures::future::join_all; use once_cell::sync::OnceCell; -use paris::info; +use paris::{error, info}; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, sync::Arc, time::Duration}; use tauri::async_runtime::RwLock; use tokio::{ - join, sync::mpsc, - task, time::{sleep, Instant}, }; use tracing::warn; @@ -21,7 +19,7 @@ use crate::{ rpc, }; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub enum AmbientLightMode { None, Follow, @@ -40,25 +38,25 @@ impl CoreManager { ambient_light_mode: Arc::new(RwLock::new(AmbientLightMode::None)), }); - tokio::spawn(async { - loop { - core.play_flowing_light().await; - match core.play_follow().await { - Ok(_) => {} - Err(error) => { - warn!("Can not following displays. {}", error); - sleep(Duration::from_millis(1000)).await; - } - }; - sleep(Duration::from_millis(10)).await; - } - }); core } pub async fn set_ambient_light(&self, target_mode: AmbientLightMode) { let mut mode = self.ambient_light_mode.write().await; *mode = target_mode; + + drop(mode); + + match target_mode { + AmbientLightMode::Flowing => self.play_flowing_light().await, + AmbientLightMode::None => {} + AmbientLightMode::Follow => match self.play_follow().await { + Ok(_) => {} + Err(error) => { + warn!("Can not following displays. {}", error); + } + }, + }; } pub async fn play_flowing_light(&self) { @@ -91,76 +89,67 @@ impl CoreManager { } pub async fn play_follow(&self) -> anyhow::Result<()> { - let lock = self.ambient_light_mode.read().await; let mut futs = vec![]; - if let AmbientLightMode::Follow = *lock { - drop(lock); - let configs = Picker::global().await.display_configs.lock().await; + let configs = Picker::global().await.display_configs.lock().await; + info!("piker display configs: {:?}", configs); - let (tx, mut rx) = mpsc::channel(10); + let (tx, mut rx) = mpsc::channel(10); - for config in configs.to_owned() { - let tx = tx.clone(); - let fut = tokio::spawn(async move { - match Self::follow_display_by_config(config, tx).await { - Ok(_) => {} - Err(error) => { - warn!("following failed. {}", error); - } - } - }); - futs.push(fut); - } - - let configs = configs.clone(); - - tokio::spawn(async move { - let mut global_colors = HashMap::new(); - while let Some(screenshot) = rx.recv().await { - let start_at = Instant::now(); - let colors = screenshot.get_top_colors(); - let start = screenshot - .get_top_of_led_start_at() - .min(screenshot.get_top_of_led_end_at()); - - let colors_len = colors.len(); - for (index, color) in colors.into_iter().enumerate() { - global_colors.insert(index + start, color); - } - - info!( - "led count: {}, spend: {:?}", - global_colors.len(), - start_at.elapsed() - ); - - if global_colors.len() == 60 { - let mut colors = vec![]; - for index in 0..global_colors.len() { - colors.push(*global_colors.get(&index).unwrap()); - } - global_colors = HashMap::new(); - match rpc::manager::Manager::global() - .publish_led_colors(&colors) - .await - { - Ok(_) => { - info!("publish successful",); - } - Err(error) => { - warn!("publish led colors failed. {}", error); - } - } + for config in configs.to_owned() { + let tx = tx.clone(); + let fut = tokio::spawn(async move { + match Self::follow_display_by_config(config, tx).await { + Ok(_) => {} + Err(error) => { + warn!("following failed. {}", error); } } }); - - join_all(futs).await; - } else { - drop(lock); - return Ok(()); + futs.push(fut); } + tokio::spawn(async move { + let mut global_colors = HashMap::new(); + while let Some(screenshot) = rx.recv().await { + let start_at = Instant::now(); + let colors = screenshot.get_top_colors(); + let start = screenshot + .get_top_of_led_start_at() + .min(screenshot.get_top_of_led_end_at()); + + for (index, color) in colors.into_iter().enumerate() { + global_colors.insert(index + start, color); + } + + info!( + "led count: {}, spend: {:?}", + global_colors.len(), + start_at.elapsed() + ); + + if global_colors.len() == 60 { + let mut colors = vec![]; + for index in 0..global_colors.len() { + colors.push(*global_colors.get(&index).unwrap()); + } + global_colors = HashMap::new(); + match rpc::manager::Manager::global() + .publish_led_colors(&colors) + .await + { + Ok(_) => { + info!("publish successful",); + } + Err(error) => { + warn!("publish led colors failed. {}", error); + } + } + } + } + }); + + join_all(futs).await; + Ok(()) } @@ -179,17 +168,17 @@ impl CoreManager { drop(lock); let screenshot = picker.take_screenshot()?; info!("Take Screenshot Spend: {:?}", start.elapsed()); - tx.send(screenshot).await; + match tx.send(screenshot).await { + Ok(_) => {} + Err(err) => { + error!("send screenshot to main thread was failed. {:?}", err); + } + }; } else { break; } tokio::time::sleep_until(next_tick).await; } - - // // Picker::global().take_screenshots_for_all().await?; - // // let colors = Picker::global().get_led_strip_colors().await?; - - // // let colors = colors.into_iter().rev().collect(); Ok(()) } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 165aac2..9b4ad86 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -78,7 +78,7 @@ async fn write_picker_config(config: picker::config::Configuration) -> Result<() async fn play_mode(target_mode: AmbientLightMode) { info!("target mode: {:?}", target_mode); - CoreManager::global().set_ambient_light(target_mode).await; + tokio::spawn(async move { CoreManager::global().set_ambient_light(target_mode).await }); } #[tokio::main]