feat: 开放 Rust 补丁程序源代码和编译说明

This commit is contained in:
Del Wang
2025-04-08 23:07:26 +08:00
parent 0e43eefd38
commit d170fadbcc
26 changed files with 2510 additions and 0 deletions
@@ -0,0 +1,70 @@
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::sync::LazyLock;
use tokio::time::{sleep, Duration};
use crate::base::AppError;
use crate::utils::shell::run_shell;
use crate::utils::task::TaskManager;
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum PlayingMonitorEvent {
Playing,
Paused,
Idle,
}
pub struct PlayingMonitor;
static INSTANCE: LazyLock<PlayingMonitor> = LazyLock::new(PlayingMonitor::new);
impl PlayingMonitor {
fn new() -> Self {
Self {}
}
pub fn instance() -> &'static Self {
&INSTANCE
}
pub async fn start<F, Fut>(&self, on_update: F)
where
F: Fn(PlayingMonitorEvent) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), AppError>> + Send + 'static,
{
let monitor = tokio::spawn(async move {
let _ = PlayingMonitor::start_monitor(on_update).await;
});
TaskManager::instance().add("PlayingMonitor", monitor).await;
}
pub async fn stop(&self) {
TaskManager::instance().dispose("PlayingMonitor").await;
}
async fn start_monitor<F, Fut>(on_update: F) -> Result<(), AppError>
where
F: Fn(PlayingMonitorEvent) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), AppError>> + Send + 'static,
{
let mut last_status = PlayingMonitorEvent::Idle;
loop {
let res = run_shell("mphelper mute_stat").await?;
let status = if res.stdout.contains("1") {
PlayingMonitorEvent::Playing
} else if res.stdout.contains("2") {
PlayingMonitorEvent::Paused
} else {
PlayingMonitorEvent::Idle
};
if last_status != status {
last_status = status.clone();
let _ = on_update(status).await;
}
sleep(Duration::from_millis(100)).await;
}
}
}