feat: 支持调整程序内存中暂存的显示器配置。

This commit is contained in:
2023-05-07 14:32:31 +08:00
parent 3a430716d6
commit 239144a446
7 changed files with 155 additions and 28 deletions

View File

@@ -1,9 +1,11 @@
use std::{sync::Arc, time::Duration};
use paris::{info, warn, error};
use tokio::{net::UdpSocket, sync::RwLock, time::timeout, io};
use paris::{error, info, warn};
use tokio::{io, net::UdpSocket, sync::RwLock, time::timeout, task::yield_now};
use super::{BoardConnectStatus, BoardInfo};
use crate::rpc::DisplaySettingRequest;
use super::{BoardConnectStatus, BoardInfo, UdpRpc};
#[derive(Debug)]
pub struct Board {
@@ -31,16 +33,34 @@ impl Board {
let info = self.info.clone();
let handler=tokio::spawn(async move {
let handler = tokio::spawn(async move {
let mut buf = [0u8; 128];
if let Err(err) = socket.readable().await {
error!("socket read error: {:?}", err);
return;
}
let board_message_channels = crate::rpc::channels::BoardMessageChannels::global().await;
let display_setting_request_sender = board_message_channels.display_setting_request_sender.clone();
loop {
match socket.try_recv(&mut buf) {
Ok(len) => {
log::info!("recv: {:?}", &buf[..len]);
if buf[0] == 3 {
let result = display_setting_request_sender.send(DisplaySettingRequest {
display_index: buf[1] as usize,
setting: crate::rpc::DisplaySetting::Brightness(buf[2]),
});
if let Err(err) = result {
error!("send display setting request to channel failed: {:?}", err);
} else {
info!("send display setting request to channel success");
yield_now().await;
}
}
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue;
@@ -125,7 +145,6 @@ impl Board {
}
}
impl Drop for Board {
fn drop(&mut self) {
if let Some(handler) = self.listen_handler.take() {
@@ -136,4 +155,4 @@ impl Drop for Board {
info!("listen handler aborted");
}
}
}
}

View File

@@ -0,0 +1,26 @@
use std::sync::Arc;
use tokio::sync::{broadcast, OnceCell};
use super::DisplaySettingRequest;
pub struct BoardMessageChannels {
pub display_setting_request_sender: Arc<broadcast::Sender<DisplaySettingRequest>>,
}
impl BoardMessageChannels {
pub async fn global() -> &'static Self {
static BOARD_MESSAGE_CHANNELS: OnceCell<BoardMessageChannels> = OnceCell::const_new();
BOARD_MESSAGE_CHANNELS.get_or_init(|| async {Self::new()}).await
}
pub fn new() -> Self {
let (display_setting_request_sender, _) = broadcast::channel(16);
let display_setting_request_sender = Arc::new(display_setting_request_sender);
Self {
display_setting_request_sender,
}
}
}

View File

@@ -0,0 +1,13 @@
#[derive(Clone, Debug)]
pub enum DisplaySetting {
Brightness(u8),
Contrast(u8),
Mode(u8),
}
#[derive(Clone, Debug)]
pub struct DisplaySettingRequest {
pub display_index: usize,
pub setting: DisplaySetting,
}

View File

@@ -2,8 +2,12 @@ mod board_info;
mod mqtt;
mod udp;
mod board;
mod display_setting_request;
mod channels;
pub use board_info::*;
pub use mqtt::*;
pub use udp::*;
pub use board::*;
pub use display_setting_request::*;
pub use channels::*;

View File

@@ -1,14 +1,11 @@
use std::{collections::HashMap, net::Ipv4Addr, sync::Arc, time::Duration};
use std::{collections::HashMap, sync::Arc, time::Duration};
use futures::future::join_all;
use mdns_sd::{ServiceDaemon, ServiceEvent};
use paris::{error, info, warn};
use tokio::{
net::UdpSocket,
sync::{watch, OnceCell, RwLock},
};
use tokio::sync::{watch, OnceCell, RwLock, broadcast};
use super::{Board, BoardInfo};
use super::{Board, BoardInfo, DisplaySettingRequest};
#[derive(Debug, Clone)]
pub struct UdpRpc {
@@ -212,7 +209,7 @@ impl UdpRpc {
if let Err(err) = board_change_sender.send(tx_boards) {
error!("failed to send board change: {:?}", err);
}
drop(board_change_sender);
}
}