feat: 灯条配置。

This commit is contained in:
2023-01-16 21:35:22 +08:00
parent 458cc85db2
commit af03b22d05
4 changed files with 80 additions and 31 deletions

View File

@ -3,7 +3,11 @@ use once_cell::sync::OnceCell;
use paris::{error, info};
use serde::{Deserialize, Serialize};
use std::{collections::{HashMap, HashSet}, sync::Arc, time::Duration};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
time::Duration,
};
use tauri::async_runtime::RwLock;
use tokio::{
sync::mpsc,
@ -13,7 +17,7 @@ use tracing::warn;
use crate::{
picker::{
config::DisplayConfig, display_picker::DisplayPicker, led_color::LedColor, manager::Picker,
self, config::DisplayConfig, display_picker::DisplayPicker, led_color::LedColor,
screenshot::Screenshot,
},
rpc,
@ -90,12 +94,16 @@ impl CoreManager {
pub async fn play_follow(&self) -> anyhow::Result<()> {
let mut futs = vec![];
let configs = Picker::global().await.display_configs.lock().await;
let configs = picker::config::Manager::global().reload_config().await;
let configs = match configs {
Ok(c) => c.display_configs,
Err(err) => anyhow::bail!("can not get display configs. {:?}", err),
};
info!("piker display configs: {:?}", configs);
let (tx, mut rx) = mpsc::channel(10);
for config in configs.to_owned() {
for config in configs.clone() {
let tx = tx.clone();
let fut = tokio::spawn(async move {
match Self::follow_display_by_config(config, tx).await {
@ -108,8 +116,7 @@ impl CoreManager {
futs.push(fut);
}
let total_colors_count = configs
.iter()
let total_colors_count = configs.iter()
.flat_map(|c| {
vec![
c.led_strip_of_borders.top,
@ -125,7 +132,8 @@ impl CoreManager {
None => {
vec![]
}
}).collect::<HashSet<_>>()
})
.collect::<HashSet<_>>()
.len();
tokio::spawn(async move {
let mut global_colors = HashMap::new();

View File

@ -2,7 +2,8 @@ use std::{
env::current_dir,
fs::{self, File},
io::Read,
path::PathBuf, sync::Arc,
path::PathBuf,
sync::Arc,
};
use once_cell::sync::OnceCell;
@ -43,7 +44,9 @@ impl Manager {
}
pub fn new(config: Configuration) -> Self {
Self { config: Arc::new(Mutex::new(config)) }
Self {
config: Arc::new(Mutex::new(config)),
}
}
pub fn get_config_file_path() -> PathBuf {
@ -70,7 +73,10 @@ impl Manager {
.map_err(|error| anyhow::anyhow!("can not parse config file contents. {}", error))
}
pub fn write_config_to_disk(config_file_path: PathBuf, config: &Configuration) -> anyhow::Result<()> {
pub fn write_config_to_disk(
config_file_path: PathBuf,
config: &Configuration,
) -> anyhow::Result<()> {
let contents = serde_json::to_string(config)
.map_err(|error| anyhow::anyhow!("can not serialize config. {}", error))?;
info!("contents: {}", contents);
@ -86,6 +92,13 @@ impl Manager {
let mut config = self.config.lock().await;
*config = new_config.clone();
}
pub async fn reload_config(&self) -> anyhow::Result<Configuration> {
let mut config = self.config.lock().await;
let new_config = Self::read_config_from_disk(Self::get_config_file_path())
.map_err(|err| anyhow::anyhow!("can not reload config. {:?}", err))?;
*config = new_config.clone();
return anyhow::Ok(new_config);
}
}
#[cfg(test)]
@ -103,9 +116,11 @@ mod tests {
let temp = TestDir::temp().create("config_dir", test_dir::FileType::Dir);
let config_file_path = temp.path("config_dir").join("picker.config.json");
let manager = crate::picker::config::manger::Manager::default();
crate::picker::config::manger::Manager
::write_config_to_disk(config_file_path.clone(), &Configuration::default())
.unwrap();
crate::picker::config::manger::Manager::write_config_to_disk(
config_file_path.clone(),
&Configuration::default(),
)
.unwrap();
let contents = fs::read_to_string(config_file_path.clone()).unwrap();
let _config: Configuration = serde_json::from_str(contents.as_str()).unwrap();