From 1c08c17fd4137f077dd1cc52d701a80728f7a6b5 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 16 Apr 2023 21:55:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=B0=86=E6=A0=A1?= =?UTF-8?q?=E5=87=86=E7=9A=84=E8=89=B2=E5=BD=A9=E5=8F=91=E9=80=81=E5=88=B0?= =?UTF-8?q?=20MQTT=20=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/ambient_light/config.rs | 10 +++++++ src-tauri/src/main.rs | 7 +++++ src-tauri/src/rpc/mqtt.rs | 40 +++++++++++++++++++++++++++ src/App.tsx | 15 ++++++++++ 4 files changed, 72 insertions(+) diff --git a/src-tauri/src/ambient_light/config.rs b/src-tauri/src/ambient_light/config.rs index c00972d..922c5e9 100644 --- a/src-tauri/src/ambient_light/config.rs +++ b/src-tauri/src/ambient_light/config.rs @@ -33,6 +33,16 @@ pub struct ColorCalibration { b: f32, } +impl ColorCalibration { + pub fn to_bytes(&self) -> [u8; 3] { + [ + (self.r * 255.0) as u8, + (self.g * 255.0) as u8, + (self.b * 255.0) as u8, + ] + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct LedStripConfigGroup { pub strips: Vec, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6841223..2ea43f9 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -181,6 +181,12 @@ async fn set_color_calibration(calibration: ColorCalibration) -> Result<(), Stri }) } +#[tauri::command] +async fn read_config() -> ambient_light::LedStripConfigGroup { + let config_manager = ambient_light::ConfigManager::global().await; + config_manager.configs().await +} + #[tokio::main] async fn main() { env_logger::init(); @@ -204,6 +210,7 @@ async fn main() { move_strip_part, reverse_led_strip_part, set_color_calibration, + read_config, ]) .register_uri_scheme_protocol("ambient-light", move |_app, request| { let response = ResponseBuilder::new().header("Access-Control-Allow-Origin", "*"); diff --git a/src-tauri/src/rpc/mqtt.rs b/src-tauri/src/rpc/mqtt.rs index 819e270..98e57b7 100644 --- a/src-tauri/src/rpc/mqtt.rs +++ b/src-tauri/src/rpc/mqtt.rs @@ -5,10 +5,13 @@ use std::time::Duration; use time::{format_description, OffsetDateTime}; use tokio::{sync::OnceCell, task}; +use crate::ambient_light::{ColorCalibration, ConfigManager}; + const DISPLAY_TOPIC: &'static str = "display-ambient-light/display"; const DESKTOP_TOPIC: &'static str = "display-ambient-light/desktop"; const DISPLAY_BRIGHTNESS_TOPIC: &'static str = "display-ambient-light/board/brightness"; const BOARD_SEND_CMD: &'static str = "display-ambient-light/board/cmd"; +const COLOR_CALIBRATION: &'static str = "display-ambient-light/desktop/color-calibration"; pub struct MqttRpc { client: mqtt::AsyncClient, @@ -165,6 +168,7 @@ impl MqttRpc { // self.subscribe_board()?; // self.subscribe_display()?; self.broadcast_desktop_online(); + Self::publish_color_calibration_worker(); anyhow::Ok(()) } @@ -182,6 +186,31 @@ impl MqttRpc { .map_err(|err| anyhow::anyhow!("subscribe board failed. {:?}", err)) .map(|_| ()) } + fn publish_color_calibration_worker() { + tokio::spawn(async move { + let mqtt = Self::global().await; + let config_manager = ConfigManager::global().await; + let mut config_receiver = config_manager.clone_config_update_receiver(); + + let config = config_manager.configs().await; + if let Err(err) = mqtt + .publish_color_calibration(config.color_calibration) + .await + { + warn!("can not publish color calibration. {}", err); + } + + while config_receiver.changed().await.is_ok() { + let config = config_receiver.borrow().clone(); + if let Err(err) = mqtt + .publish_color_calibration(config.color_calibration) + .await + { + warn!("can not publish color calibration. {}", err); + } + } + }); + } fn broadcast_desktop_online(&self) { let client = self.client.to_owned(); @@ -238,4 +267,15 @@ impl MqttRpc { .await .map_err(|error| anyhow::anyhow!("mqtt publish failed. {}", error)) } + + pub async fn publish_color_calibration(&self, payload: ColorCalibration) -> anyhow::Result<()> { + self.client + .publish(mqtt::Message::new( + COLOR_CALIBRATION, + payload.to_bytes(), + mqtt::QOS_1, + )) + .await + .map_err(|error| anyhow::anyhow!("mqtt publish color calibration failed. {}", error)) + } } diff --git a/src/App.tsx b/src/App.tsx index cd629d7..aff76c9 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,23 @@ import { Routes, Route } from '@solidjs/router'; import { LedStripConfiguration } from './components/led-strip-configuration/led-strip-configuration'; import { WhiteBalance } from './components/white-balance/white-balance'; +import { createEffect } from 'solid-js'; +import { invoke } from '@tauri-apps/api'; +import { setLedStripStore } from './stores/led-strip.store'; +import { LedStripConfigContainer } from './models/led-strip-config'; function App() { + createEffect(() => { + invoke('read_config').then((config) => { + console.log('read config', config); + setLedStripStore({ + strips: config.strips, + mappers: config.mappers, + colorCalibration: config.color_calibration, + }); + }); + }); + return (