From 9ec030488aa17b3231e7a75eff358165a299cec0 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 2 Apr 2023 14:05:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=91=E5=B8=83=E7=81=AF=E6=9D=A1?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E5=88=B0=20MQTT=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/ambient_light/publisher.rs | 30 +++++++++++++++++++++--- src-tauri/src/main.rs | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/ambient_light/publisher.rs b/src-tauri/src/ambient_light/publisher.rs index 4f0a87f..406fbcc 100644 --- a/src-tauri/src/ambient_light/publisher.rs +++ b/src-tauri/src/ambient_light/publisher.rs @@ -37,10 +37,10 @@ impl LedColorsPublisher { .await } - pub fn start(&self) -> anyhow::Result<()> { + pub fn start(&self) { let tx = self.tx.clone(); - let handler = tokio::spawn(async move { + tokio::spawn(async move { loop { log::info!("colors update loop AAA"); @@ -103,7 +103,31 @@ impl LedColorsPublisher { } } }); - Ok(()) + + let rx = self.rx.clone(); + tokio::spawn(async move { + let mut rx = rx.read().await.clone(); + loop { + if let Err(err) = rx.changed().await { + warn!("rx changed error: {}", err); + sleep(Duration::from_millis(1000)).await; + continue; + } + + let colors = rx.borrow().clone(); + + let len = colors.len(); + + match Self::send_colors(colors).await { + Ok(_) => { + log::info!("colors sent. len: {}", len); + } + Err(err) => { + warn!("colors send failed: {}", err); + } + } + } + }); } pub async fn send_colors(payload: Vec) -> anyhow::Result<()> { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6d4b2c0..39a9e07 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -183,7 +183,7 @@ async fn main() { screenshot_manager.start().unwrap(); let led_color_publisher = ambient_light::LedColorsPublisher::global().await; - led_color_publisher.start().unwrap(); + led_color_publisher.start(); tauri::Builder::default() .invoke_handler(tauri::generate_handler![