diff --git a/src-tauri/src/display/manager.rs b/src-tauri/src/display/manager.rs index 88b789b..c722107 100644 --- a/src-tauri/src/display/manager.rs +++ b/src-tauri/src/display/manager.rs @@ -6,6 +6,7 @@ use std::{ time::{Duration, SystemTime}, }; +use base64::Config; use ddc_hi::Display; use paris::{error, info}; use tauri::async_runtime::Mutex; @@ -155,13 +156,19 @@ impl Manager { .set_vcp_feature(0x10, target as u16) .map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?; - let rpc = rpc::Manager::global().await; + let rpc = rpc::Manager::global().await; - rpc.publish_desktop_cmd(models::MqMessage::Brightness(models::ConfigDisplayCmd { - display_index: config.id, - value: models::ControlValue::Absolute(config.brightness), - })) - .await; + rpc.publish_desktop_cmd(&models::CmdRespMqMessage::Brightness( + models::ConfigDisplayCmd { + display_index: config.id, + value: models::CmdRespWithRange { + value: config.brightness, + min: config.min_brightness, + max: config.max_brightness, + }, + }, + )) + .await; } Err(err) => { info!( diff --git a/src-tauri/src/models/cmd_resp_with_range.rs b/src-tauri/src/models/cmd_resp_with_range.rs new file mode 100644 index 0000000..b8a8c20 --- /dev/null +++ b/src-tauri/src/models/cmd_resp_with_range.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Serialize, Deserialize, Debug)] +pub struct CmdRespWithRange { + pub value: T, + pub max: T, + pub min: T, +} diff --git a/src-tauri/src/models/mod.rs b/src-tauri/src/models/mod.rs index cf8070d..970290c 100644 --- a/src-tauri/src/models/mod.rs +++ b/src-tauri/src/models/mod.rs @@ -1,8 +1,10 @@ mod control_value; mod mq_message; mod config_display_cmd; +mod cmd_resp_with_range; pub use control_value::*; pub use mq_message::*; pub use config_display_cmd::*; +pub use cmd_resp_with_range::*; diff --git a/src-tauri/src/models/mq_message.rs b/src-tauri/src/models/mq_message.rs index 794ba41..9202b45 100644 --- a/src-tauri/src/models/mq_message.rs +++ b/src-tauri/src/models/mq_message.rs @@ -1,11 +1,17 @@ use serde::{Serialize, Deserialize}; -use super::ConfigDisplayCmd; +use super::{ConfigDisplayCmd, CmdRespWithRange}; #[derive(Clone, Copy, Serialize, Deserialize, Debug)] -pub enum MqMessage { +pub enum CmdMqMessage { Brightness(ConfigDisplayCmd), Contrast(ConfigDisplayCmd), PresetMode(ConfigDisplayCmd), +} +#[derive(Clone, Copy, Serialize, Deserialize, Debug)] +pub enum CmdRespMqMessage { + Brightness(ConfigDisplayCmd), + Contrast(ConfigDisplayCmd), + PresetMode(ConfigDisplayCmd), } \ No newline at end of file diff --git a/src-tauri/src/rpc/manager.rs b/src-tauri/src/rpc/manager.rs index ad2317e..c2f5cfc 100644 --- a/src-tauri/src/rpc/manager.rs +++ b/src-tauri/src/rpc/manager.rs @@ -1,5 +1,6 @@ use paris::error; use tokio::sync::{broadcast, OnceCell}; +use std::fmt::Debug; use crate::{display, models, picker::led_color::LedColor}; @@ -51,7 +52,10 @@ impl Manager { pub async fn publish_led_sub_pixels(&self, payload: Vec) -> anyhow::Result<()> { self.client.publish_led_sub_pixels(payload).await } - pub async fn publish_desktop_cmd(&self, msg: models::MqMessage) -> anyhow::Result<()> { + pub async fn publish_desktop_cmd(&self, msg: &T) -> anyhow::Result<()> + where + T: ?Sized + serde::Serialize, + { self.client.publish_desktop_cmd(msg).await } diff --git a/src-tauri/src/rpc/mqtt.rs b/src-tauri/src/rpc/mqtt.rs index 02bf836..0050ad7 100644 --- a/src-tauri/src/rpc/mqtt.rs +++ b/src-tauri/src/rpc/mqtt.rs @@ -3,7 +3,7 @@ use futures::StreamExt; use paho_mqtt as mqtt; use paris::{error, info, warn}; use serde_json::json; -use std::{borrow::Borrow, rc::Rc, sync::Arc, time::Duration}; +use std::{borrow::Borrow, fmt::Debug, rc::Rc, sync::Arc, time::Duration}; use tauri::async_runtime::{Mutex, TokioJoinHandle}; use time::{format_description, OffsetDateTime}; use tokio::{sync::broadcast, task, time::sleep}; @@ -17,7 +17,7 @@ const DESKTOP_SEND_CMD: &'static str = "display-ambient-light/desktop/cmd"; pub struct MqttRpc { client: mqtt::AsyncClient, change_display_brightness_tx: broadcast::Sender, - message_tx: broadcast::Sender, + message_tx: broadcast::Sender, } impl MqttRpc { @@ -64,6 +64,7 @@ impl MqttRpc { let connect_options = mqtt::ConnectOptionsBuilder::new() .keep_alive_interval(Duration::from_secs(5)) + .will_message(last_will) .automatic_reconnect(Duration::from_secs(1), Duration::from_secs(5)) .finalize(); @@ -78,7 +79,7 @@ impl MqttRpc { let (change_display_brightness_tx, _) = broadcast::channel::(16); - let (message_tx, _) = broadcast::channel::(32); + let (message_tx, _) = broadcast::channel::(32); Ok(Self { client, change_display_brightness_tx, @@ -131,7 +132,7 @@ impl MqttRpc { let payload_text = String::from_utf8(notification.payload().to_vec()); match payload_text { Ok(payload_text) => { - let message: Result = + let message: Result = serde_json::from_str(payload_text.as_str()); match message { Ok(message) => match message_tx_cloned.send(message) { @@ -226,9 +227,12 @@ impl MqttRpc { ) -> broadcast::Receiver { self.change_display_brightness_tx.subscribe() } - pub async fn publish_desktop_cmd(&self, msg: models::MqMessage) -> anyhow::Result<()> { - let str = serde_json::to_string(&msg) - .map_err(|err| anyhow::anyhow!("can not serialize {:?}. {:?}", msg, err))?; + pub async fn publish_desktop_cmd(&self, msg: &T) -> anyhow::Result<()> + where + T: ?Sized + serde::Serialize, + { + let str = serde_json::to_string(msg) + .map_err(|err| anyhow::anyhow!("can not serialize. {:?}", err))?; self.client .publish(mqtt::Message::new( DESKTOP_SEND_CMD,