feat: 发布灯条颜色到 MQTT。

This commit is contained in:
Ivan Li 2023-04-02 14:05:53 +08:00
parent 0d47911355
commit 9ec030488a
2 changed files with 28 additions and 4 deletions

View File

@ -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<u8>) -> anyhow::Result<()> {

View File

@ -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![