feat: 命令执行成功后发布相关状态到 MQ. #5.
This commit is contained in:
parent
e09b93432c
commit
eeddff1dc1
@ -6,6 +6,7 @@ use std::{
|
|||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use base64::Config;
|
||||||
use ddc_hi::Display;
|
use ddc_hi::Display;
|
||||||
use paris::{error, info};
|
use paris::{error, info};
|
||||||
use tauri::async_runtime::Mutex;
|
use tauri::async_runtime::Mutex;
|
||||||
@ -157,10 +158,16 @@ impl Manager {
|
|||||||
|
|
||||||
let rpc = rpc::Manager::global().await;
|
let rpc = rpc::Manager::global().await;
|
||||||
|
|
||||||
rpc.publish_desktop_cmd(models::MqMessage::Brightness(models::ConfigDisplayCmd {
|
rpc.publish_desktop_cmd(&models::CmdRespMqMessage::Brightness(
|
||||||
|
models::ConfigDisplayCmd {
|
||||||
display_index: config.id,
|
display_index: config.id,
|
||||||
value: models::ControlValue::Absolute(config.brightness),
|
value: models::CmdRespWithRange {
|
||||||
}))
|
value: config.brightness,
|
||||||
|
min: config.min_brightness,
|
||||||
|
max: config.max_brightness,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
8
src-tauri/src/models/cmd_resp_with_range.rs
Normal file
8
src-tauri/src/models/cmd_resp_with_range.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
|
||||||
|
pub struct CmdRespWithRange<T = u16> {
|
||||||
|
pub value: T,
|
||||||
|
pub max: T,
|
||||||
|
pub min: T,
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
mod control_value;
|
mod control_value;
|
||||||
mod mq_message;
|
mod mq_message;
|
||||||
mod config_display_cmd;
|
mod config_display_cmd;
|
||||||
|
mod cmd_resp_with_range;
|
||||||
|
|
||||||
pub use control_value::*;
|
pub use control_value::*;
|
||||||
pub use mq_message::*;
|
pub use mq_message::*;
|
||||||
pub use config_display_cmd::*;
|
pub use config_display_cmd::*;
|
||||||
|
pub use cmd_resp_with_range::*;
|
||||||
|
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
use super::ConfigDisplayCmd;
|
use super::{ConfigDisplayCmd, CmdRespWithRange};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
|
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
|
||||||
pub enum MqMessage {
|
pub enum CmdMqMessage {
|
||||||
Brightness(ConfigDisplayCmd),
|
Brightness(ConfigDisplayCmd),
|
||||||
Contrast(ConfigDisplayCmd),
|
Contrast(ConfigDisplayCmd),
|
||||||
PresetMode(ConfigDisplayCmd),
|
PresetMode(ConfigDisplayCmd),
|
||||||
}
|
}
|
||||||
|
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
|
||||||
|
pub enum CmdRespMqMessage {
|
||||||
|
Brightness(ConfigDisplayCmd<CmdRespWithRange>),
|
||||||
|
Contrast(ConfigDisplayCmd<CmdRespWithRange>),
|
||||||
|
PresetMode(ConfigDisplayCmd<CmdRespWithRange>),
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
use paris::error;
|
use paris::error;
|
||||||
use tokio::sync::{broadcast, OnceCell};
|
use tokio::sync::{broadcast, OnceCell};
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use crate::{display, models, picker::led_color::LedColor};
|
use crate::{display, models, picker::led_color::LedColor};
|
||||||
|
|
||||||
@ -51,7 +52,10 @@ impl Manager {
|
|||||||
pub async fn publish_led_sub_pixels(&self, payload: Vec<u8>) -> anyhow::Result<()> {
|
pub async fn publish_led_sub_pixels(&self, payload: Vec<u8>) -> anyhow::Result<()> {
|
||||||
self.client.publish_led_sub_pixels(payload).await
|
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<T>(&self, msg: &T) -> anyhow::Result<()>
|
||||||
|
where
|
||||||
|
T: ?Sized + serde::Serialize,
|
||||||
|
{
|
||||||
self.client.publish_desktop_cmd(msg).await
|
self.client.publish_desktop_cmd(msg).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use futures::StreamExt;
|
|||||||
use paho_mqtt as mqtt;
|
use paho_mqtt as mqtt;
|
||||||
use paris::{error, info, warn};
|
use paris::{error, info, warn};
|
||||||
use serde_json::json;
|
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 tauri::async_runtime::{Mutex, TokioJoinHandle};
|
||||||
use time::{format_description, OffsetDateTime};
|
use time::{format_description, OffsetDateTime};
|
||||||
use tokio::{sync::broadcast, task, time::sleep};
|
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 {
|
pub struct MqttRpc {
|
||||||
client: mqtt::AsyncClient,
|
client: mqtt::AsyncClient,
|
||||||
change_display_brightness_tx: broadcast::Sender<display::DisplayBrightness>,
|
change_display_brightness_tx: broadcast::Sender<display::DisplayBrightness>,
|
||||||
message_tx: broadcast::Sender<models::MqMessage>,
|
message_tx: broadcast::Sender<models::CmdMqMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MqttRpc {
|
impl MqttRpc {
|
||||||
@ -64,6 +64,7 @@ impl MqttRpc {
|
|||||||
|
|
||||||
let connect_options = mqtt::ConnectOptionsBuilder::new()
|
let connect_options = mqtt::ConnectOptionsBuilder::new()
|
||||||
.keep_alive_interval(Duration::from_secs(5))
|
.keep_alive_interval(Duration::from_secs(5))
|
||||||
|
.will_message(last_will)
|
||||||
.automatic_reconnect(Duration::from_secs(1), Duration::from_secs(5))
|
.automatic_reconnect(Duration::from_secs(1), Duration::from_secs(5))
|
||||||
.finalize();
|
.finalize();
|
||||||
|
|
||||||
@ -78,7 +79,7 @@ impl MqttRpc {
|
|||||||
|
|
||||||
let (change_display_brightness_tx, _) =
|
let (change_display_brightness_tx, _) =
|
||||||
broadcast::channel::<display::DisplayBrightness>(16);
|
broadcast::channel::<display::DisplayBrightness>(16);
|
||||||
let (message_tx, _) = broadcast::channel::<models::MqMessage>(32);
|
let (message_tx, _) = broadcast::channel::<models::CmdMqMessage>(32);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
client,
|
client,
|
||||||
change_display_brightness_tx,
|
change_display_brightness_tx,
|
||||||
@ -131,7 +132,7 @@ impl MqttRpc {
|
|||||||
let payload_text = String::from_utf8(notification.payload().to_vec());
|
let payload_text = String::from_utf8(notification.payload().to_vec());
|
||||||
match payload_text {
|
match payload_text {
|
||||||
Ok(payload_text) => {
|
Ok(payload_text) => {
|
||||||
let message: Result<models::MqMessage, _> =
|
let message: Result<models::CmdMqMessage, _> =
|
||||||
serde_json::from_str(payload_text.as_str());
|
serde_json::from_str(payload_text.as_str());
|
||||||
match message {
|
match message {
|
||||||
Ok(message) => match message_tx_cloned.send(message) {
|
Ok(message) => match message_tx_cloned.send(message) {
|
||||||
@ -226,9 +227,12 @@ impl MqttRpc {
|
|||||||
) -> broadcast::Receiver<display::DisplayBrightness> {
|
) -> broadcast::Receiver<display::DisplayBrightness> {
|
||||||
self.change_display_brightness_tx.subscribe()
|
self.change_display_brightness_tx.subscribe()
|
||||||
}
|
}
|
||||||
pub async fn publish_desktop_cmd(&self, msg: models::MqMessage) -> anyhow::Result<()> {
|
pub async fn publish_desktop_cmd<T>(&self, msg: &T) -> anyhow::Result<()>
|
||||||
let str = serde_json::to_string(&msg)
|
where
|
||||||
.map_err(|err| anyhow::anyhow!("can not serialize {:?}. {:?}", msg, err))?;
|
T: ?Sized + serde::Serialize,
|
||||||
|
{
|
||||||
|
let str = serde_json::to_string(msg)
|
||||||
|
.map_err(|err| anyhow::anyhow!("can not serialize. {:?}", err))?;
|
||||||
self.client
|
self.client
|
||||||
.publish(mqtt::Message::new(
|
.publish(mqtt::Message::new(
|
||||||
DESKTOP_SEND_CMD,
|
DESKTOP_SEND_CMD,
|
||||||
|
Loading…
Reference in New Issue
Block a user