2023-04-03 23:19:45 +08:00
|
|
|
use std::{collections::HashMap, sync::Arc, time::Duration};
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
use paris::warn;
|
|
|
|
use tauri::async_runtime::RwLock;
|
|
|
|
use tokio::{
|
|
|
|
sync::{broadcast, watch},
|
|
|
|
time::sleep,
|
|
|
|
};
|
2023-04-01 10:42:46 +08:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
ambient_light::{config, ConfigManager},
|
|
|
|
rpc::MqttRpc,
|
2023-04-05 12:25:14 +08:00
|
|
|
screenshot::LedSamplePoints,
|
|
|
|
screenshot_manager::{self, ScreenshotManager},
|
2023-04-01 10:42:46 +08:00
|
|
|
};
|
|
|
|
|
2023-04-01 15:39:21 +08:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
use super::{LedStripConfigGroup, SamplePointConfig, SamplePointMapper};
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-01 10:42:46 +08:00
|
|
|
pub struct LedColorsPublisher {
|
2023-04-02 14:52:08 +08:00
|
|
|
sorted_colors_rx: Arc<RwLock<watch::Receiver<Vec<u8>>>>,
|
|
|
|
sorted_colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>,
|
2023-04-05 12:25:14 +08:00
|
|
|
colors_rx: Arc<RwLock<watch::Receiver<Vec<u8>>>>,
|
|
|
|
colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>,
|
|
|
|
inner_tasks_version: Arc<RwLock<usize>>,
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LedColorsPublisher {
|
|
|
|
pub async fn global() -> &'static Self {
|
|
|
|
static LED_COLORS_PUBLISHER_GLOBAL: tokio::sync::OnceCell<LedColorsPublisher> =
|
|
|
|
tokio::sync::OnceCell::const_new();
|
|
|
|
|
2023-04-02 14:52:08 +08:00
|
|
|
let (sorted_tx, sorted_rx) = watch::channel(Vec::new());
|
2023-04-01 10:42:46 +08:00
|
|
|
let (tx, rx) = watch::channel(Vec::new());
|
|
|
|
|
|
|
|
LED_COLORS_PUBLISHER_GLOBAL
|
|
|
|
.get_or_init(|| async {
|
|
|
|
LedColorsPublisher {
|
2023-04-02 14:52:08 +08:00
|
|
|
sorted_colors_rx: Arc::new(RwLock::new(sorted_rx)),
|
|
|
|
sorted_colors_tx: Arc::new(RwLock::new(sorted_tx)),
|
|
|
|
colors_rx: Arc::new(RwLock::new(rx)),
|
|
|
|
colors_tx: Arc::new(RwLock::new(tx)),
|
2023-04-05 12:25:14 +08:00
|
|
|
inner_tasks_version: Arc::new(RwLock::new(0)),
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
fn start_one_display_colors_fetcher(
|
|
|
|
&self,
|
|
|
|
display_id: u32,
|
|
|
|
sample_points: Vec<Vec<LedSamplePoints>>,
|
2023-04-15 13:45:30 +08:00
|
|
|
bound_scale_factor: f32,
|
2023-04-14 22:18:59 +08:00
|
|
|
display_colors_tx: broadcast::Sender<(u32, Vec<u8>)>,
|
2023-04-05 12:25:14 +08:00
|
|
|
) {
|
|
|
|
let internal_tasks_version = self.inner_tasks_version.clone();
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-02 14:05:53 +08:00
|
|
|
tokio::spawn(async move {
|
2023-04-15 13:45:30 +08:00
|
|
|
let colors = screenshot_manager::get_display_colors(display_id, &sample_points, bound_scale_factor);
|
2023-04-05 12:25:14 +08:00
|
|
|
|
|
|
|
if let Err(err) = colors {
|
|
|
|
warn!("Failed to get colors: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut start: tokio::time::Instant = tokio::time::Instant::now();
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_millis(66));
|
|
|
|
let init_version = internal_tasks_version.read().await.clone();
|
|
|
|
|
2023-04-01 10:42:46 +08:00
|
|
|
loop {
|
2023-04-05 12:25:14 +08:00
|
|
|
interval.tick().await;
|
|
|
|
tokio::time::sleep(Duration::from_millis(1)).await;
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
let version = internal_tasks_version.read().await.clone();
|
|
|
|
|
|
|
|
if version != init_version {
|
2023-04-05 12:25:14 +08:00
|
|
|
log::info!(
|
|
|
|
"inner task version changed, stop. {} != {}",
|
|
|
|
internal_tasks_version.read().await.clone(),
|
|
|
|
init_version
|
|
|
|
);
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// log::info!("tick: {}ms", start.elapsed().as_millis());
|
|
|
|
start = tokio::time::Instant::now();
|
2023-04-15 13:45:30 +08:00
|
|
|
let colors = screenshot_manager::get_display_colors(display_id, &sample_points, bound_scale_factor);
|
2023-04-05 12:25:14 +08:00
|
|
|
|
|
|
|
if let Err(err) = colors {
|
|
|
|
warn!("Failed to get colors: {}", err);
|
2023-04-03 23:19:45 +08:00
|
|
|
sleep(Duration::from_millis(100)).await;
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let colors = colors.unwrap();
|
|
|
|
|
|
|
|
let color_len = colors.len();
|
|
|
|
|
|
|
|
match display_colors_tx.send((
|
|
|
|
display_id,
|
|
|
|
colors
|
|
|
|
.into_iter()
|
|
|
|
.map(|color| color.get_rgb())
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)) {
|
|
|
|
Ok(_) => {
|
|
|
|
// log::info!("sent colors: {:?}", color_len);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!("Failed to send display_colors: {}", err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
fn start_all_colors_worker(&self, display_ids: Vec<u32>, mappers: Vec<SamplePointMapper>, mut display_colors_rx: broadcast::Receiver<(u32, Vec<u8>)>) {
|
2023-04-05 12:25:14 +08:00
|
|
|
let sorted_colors_tx = self.sorted_colors_tx.clone();
|
|
|
|
let colors_tx = self.colors_tx.clone();
|
2023-04-14 22:18:59 +08:00
|
|
|
log::debug!("start all_colors_worker");
|
2023-04-05 12:25:14 +08:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
for _ in 0..10 {
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let sorted_colors_tx = sorted_colors_tx.write().await;
|
|
|
|
let colors_tx = colors_tx.write().await;
|
2023-04-02 14:52:08 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let mut all_colors: Vec<Option<Vec<u8>>> = vec![None; display_ids.len()];
|
|
|
|
let mut start: tokio::time::Instant = tokio::time::Instant::now();
|
2023-04-02 14:52:08 +08:00
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
log::debug!("start all_colors_worker task");
|
2023-04-03 23:19:45 +08:00
|
|
|
loop {
|
2023-04-14 22:18:59 +08:00
|
|
|
let color_info = display_colors_rx.recv().await;
|
2023-04-03 23:19:45 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
if let Err(err) = color_info {
|
2023-04-03 23:19:45 +08:00
|
|
|
match err {
|
2023-04-05 12:25:14 +08:00
|
|
|
broadcast::error::RecvError::Closed => {
|
2023-04-14 22:18:59 +08:00
|
|
|
return;
|
2023-04-05 12:25:14 +08:00
|
|
|
}
|
|
|
|
broadcast::error::RecvError::Lagged(_) => {
|
|
|
|
warn!("display_colors_rx lagged");
|
2023-04-03 23:19:45 +08:00
|
|
|
continue;
|
2023-04-05 12:25:14 +08:00
|
|
|
}
|
2023-04-02 14:52:08 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 12:25:14 +08:00
|
|
|
let (display_id, colors) = color_info.unwrap();
|
2023-04-02 14:52:08 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let index = display_ids.iter().position(|id| *id == display_id);
|
2023-04-03 23:19:45 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
if index.is_none() {
|
|
|
|
warn!("display id not found");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
all_colors[index.unwrap()] = Some(colors);
|
|
|
|
|
|
|
|
if all_colors.iter().all(|color| color.is_some()) {
|
|
|
|
let flatten_colors = all_colors
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|c| c.unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
match colors_tx.send(flatten_colors.clone()) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
warn!("Failed to send colors: {}", err);
|
2023-04-03 23:19:45 +08:00
|
|
|
}
|
2023-04-05 12:25:14 +08:00
|
|
|
};
|
2023-04-01 10:42:46 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let sorted_colors =
|
|
|
|
ScreenshotManager::get_sorted_colors(&flatten_colors, &mappers);
|
|
|
|
|
|
|
|
match sorted_colors_tx.send(sorted_colors) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
warn!("Failed to send sorted colors: {}", err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
log::info!("tick: {}ms", start.elapsed().as_millis());
|
|
|
|
start = tokio::time::Instant::now();
|
2023-04-02 14:52:08 +08:00
|
|
|
}
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|
2023-04-01 18:39:51 +08:00
|
|
|
}
|
|
|
|
});
|
2023-04-05 12:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(&self) {
|
|
|
|
let inner_tasks_version = self.inner_tasks_version.clone();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let publisher = Self::global().await;
|
|
|
|
|
|
|
|
let config_manager = ConfigManager::global().await;
|
|
|
|
let mut config_receiver = config_manager.clone_config_update_receiver();
|
|
|
|
|
|
|
|
log::info!("waiting for config update...");
|
|
|
|
|
|
|
|
while config_receiver.changed().await.is_ok() {
|
|
|
|
log::info!("config updated, restart inner tasks...");
|
|
|
|
let configs = config_receiver.borrow().clone();
|
|
|
|
let configs = Self::get_colors_configs(&configs).await;
|
|
|
|
|
|
|
|
if let Err(err) = configs {
|
|
|
|
warn!("Failed to get configs: {}", err);
|
|
|
|
sleep(Duration::from_millis(100)).await;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let configs = configs.unwrap();
|
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
|
|
|
|
let mut inner_tasks_version = inner_tasks_version.write().await;
|
|
|
|
*inner_tasks_version = inner_tasks_version.overflowing_add(1).0;
|
|
|
|
drop(inner_tasks_version);
|
|
|
|
|
|
|
|
|
|
|
|
let (display_colors_tx, display_colors_rx) = broadcast::channel::<(u32, Vec<u8>)>(8);
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
for sample_point_group in configs.sample_point_groups.clone() {
|
|
|
|
let display_id = sample_point_group.display_id;
|
|
|
|
let sample_points = sample_point_group.points;
|
2023-04-15 13:45:30 +08:00
|
|
|
let bound_scale_factor = sample_point_group.bound_scale_factor;
|
2023-04-05 12:25:14 +08:00
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
publisher.start_one_display_colors_fetcher(
|
|
|
|
display_id,
|
|
|
|
sample_points,
|
2023-04-15 13:45:30 +08:00
|
|
|
bound_scale_factor,
|
2023-04-14 22:18:59 +08:00
|
|
|
display_colors_tx.clone(),
|
|
|
|
);
|
2023-04-05 12:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let display_ids = configs.sample_point_groups;
|
|
|
|
publisher.start_all_colors_worker(
|
|
|
|
display_ids.iter().map(|c| c.display_id).collect(),
|
|
|
|
configs.mappers,
|
2023-04-14 22:18:59 +08:00
|
|
|
display_colors_rx,
|
2023-04-05 12:25:14 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-02 14:52:08 +08:00
|
|
|
let rx = self.sorted_colors_rx.clone();
|
2023-04-02 14:05:53 +08:00
|
|
|
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(_) => {
|
2023-04-05 12:25:14 +08:00
|
|
|
// log::info!("colors sent. len: {}", len);
|
2023-04-02 14:05:53 +08:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!("colors send failed: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_colors(payload: Vec<u8>) -> anyhow::Result<()> {
|
|
|
|
let mqtt = MqttRpc::global().await;
|
|
|
|
|
|
|
|
mqtt.publish_led_sub_pixels(payload).await
|
|
|
|
}
|
|
|
|
|
2023-04-02 14:52:08 +08:00
|
|
|
pub async fn clone_sorted_colors_receiver(&self) -> watch::Receiver<Vec<u8>> {
|
|
|
|
self.sorted_colors_rx.read().await.clone()
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|
2023-04-03 23:19:45 +08:00
|
|
|
pub async fn get_colors_configs(
|
|
|
|
configs: &LedStripConfigGroup,
|
|
|
|
) -> anyhow::Result<AllColorConfig> {
|
2023-04-01 18:39:51 +08:00
|
|
|
let screenshot_manager = ScreenshotManager::global().await;
|
|
|
|
|
|
|
|
let display_ids = configs
|
|
|
|
.strips
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.display_id)
|
|
|
|
.unique()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mappers = configs.mappers.clone();
|
|
|
|
|
|
|
|
let mut colors_configs = Vec::new();
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let mut merged_screenshot_receiver = screenshot_manager.clone_merged_screenshot_rx().await;
|
2023-04-14 22:18:59 +08:00
|
|
|
merged_screenshot_receiver.resubscribe();
|
2023-04-05 12:25:14 +08:00
|
|
|
|
|
|
|
let mut screenshots = HashMap::new();
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
loop {
|
|
|
|
log::info!("waiting merged screenshot...");
|
|
|
|
let screenshot = merged_screenshot_receiver.recv().await;
|
|
|
|
|
|
|
|
if let Err(err) = screenshot {
|
|
|
|
match err {
|
|
|
|
tokio::sync::broadcast::error::RecvError::Closed => {
|
|
|
|
warn!("closed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tokio::sync::broadcast::error::RecvError::Lagged(_) => {
|
|
|
|
warn!("lagged");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 23:19:45 +08:00
|
|
|
}
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let screenshot = screenshot.unwrap();
|
|
|
|
// log::info!("got screenshot: {:?}", screenshot.display_id);
|
2023-04-03 23:19:45 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
screenshots.insert(screenshot.display_id, screenshot);
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
if screenshots.len() == display_ids.len() {
|
|
|
|
for display_id in display_ids {
|
|
|
|
let led_strip_configs: Vec<_> = configs
|
|
|
|
.strips
|
|
|
|
.iter()
|
|
|
|
.filter(|c| c.display_id == display_id)
|
|
|
|
.collect();
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
if led_strip_configs.len() == 0 {
|
|
|
|
warn!("no led strip config for display_id: {}", display_id);
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let screenshot = screenshots.get(&display_id).unwrap();
|
|
|
|
log::debug!("screenshot updated: {:?}", display_id);
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
let points: Vec<_> = led_strip_configs
|
|
|
|
.iter()
|
|
|
|
.map(|config| screenshot.get_sample_points(&config))
|
|
|
|
.collect();
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-15 13:45:30 +08:00
|
|
|
let bound_scale_factor = screenshot.bound_scale_factor;
|
|
|
|
|
|
|
|
let colors_config = DisplaySamplePointGroup { display_id, points, bound_scale_factor };
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
colors_configs.push(colors_config);
|
|
|
|
}
|
2023-04-01 18:39:51 +08:00
|
|
|
|
2023-04-14 22:18:59 +08:00
|
|
|
log::debug!("got all colors configs: {:?}", colors_configs.len());
|
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
return Ok(AllColorConfig {
|
|
|
|
sample_point_groups: colors_configs,
|
|
|
|
mappers,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 18:39:51 +08:00
|
|
|
}
|
2023-04-02 14:52:08 +08:00
|
|
|
|
2023-04-05 12:25:14 +08:00
|
|
|
pub async fn clone_colors_receiver(&self) -> watch::Receiver<Vec<u8>> {
|
2023-04-02 14:52:08 +08:00
|
|
|
self.colors_rx.read().await.clone()
|
|
|
|
}
|
2023-04-01 18:39:51 +08:00
|
|
|
}
|
|
|
|
|
2023-04-03 23:19:45 +08:00
|
|
|
#[derive(Debug)]
|
2023-04-01 18:39:51 +08:00
|
|
|
pub struct AllColorConfig {
|
2023-04-05 12:25:14 +08:00
|
|
|
pub sample_point_groups: Vec<DisplaySamplePointGroup>,
|
2023-04-01 18:39:51 +08:00
|
|
|
pub mappers: Vec<config::SamplePointMapper>,
|
2023-04-05 12:25:14 +08:00
|
|
|
// pub screenshot_receivers: Vec<watch::Receiver<Screenshot>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DisplaySamplePointGroup {
|
|
|
|
pub display_id: u32,
|
|
|
|
pub points: Vec<Vec<LedSamplePoints>>,
|
2023-04-15 13:45:30 +08:00
|
|
|
pub bound_scale_factor: f32,
|
2023-04-01 10:42:46 +08:00
|
|
|
}
|