fix: 亮度调节指令频繁时通道被关闭。

This commit is contained in:
Ivan Li 2023-05-10 21:18:55 +08:00
parent 5ddd704c9d
commit 878180ed5b

View File

@ -5,7 +5,10 @@ use std::{
use ddc_hi::Display; use ddc_hi::Display;
use paris::{error, info, warn}; use paris::{error, info, warn};
use tokio::{sync::{watch, OnceCell, RwLock}, task::yield_now}; use tokio::{
sync::{broadcast, watch, OnceCell, RwLock},
task::yield_now,
};
use crate::rpc::{BoardMessageChannels, DisplaySetting}; use crate::rpc::{BoardMessageChannels, DisplaySetting};
@ -75,7 +78,22 @@ impl DisplayManager {
let channels = BoardMessageChannels::global().await; let channels = BoardMessageChannels::global().await;
let mut request_rx = channels.display_setting_request_sender.subscribe(); let mut request_rx = channels.display_setting_request_sender.subscribe();
while let Ok(message) = request_rx.recv().await { loop {
if let Err(err) = request_rx.recv().await {
match err {
broadcast::error::RecvError::Closed => {
info!("display setting request channel closed");
break;
}
broadcast::error::RecvError::Lagged(_) => {
warn!("display setting request channel lagged");
continue;
}
}
}
let message = request_rx.recv().await.unwrap();
let displays = displays.write().await; let displays = displays.write().await;
let display = displays.get(message.display_index); let display = displays.get(message.display_index);
@ -84,7 +102,6 @@ impl DisplayManager {
continue; continue;
} }
let display = display.unwrap().write().await; let display = display.unwrap().write().await;
let result = match message.setting { let result = match message.setting {
DisplaySetting::Brightness(value) => display.set_brightness(value as u16).await, DisplaySetting::Brightness(value) => display.set_brightness(value as u16).await,
@ -122,8 +139,8 @@ impl DisplayManager {
impl Drop for DisplayManager { impl Drop for DisplayManager {
fn drop(&mut self) { fn drop(&mut self) {
log::info!("dropping display manager=============");
if let Some(handler) = self.setting_request_handler.take() { if let Some(handler) = self.setting_request_handler.take() {
info!("abort display setting request handler");
handler.abort(); handler.abort();
} }
} }