feat: 命令执行成功后发布相关状态到 MQ. #5.
This commit is contained in:
parent
e09b93432c
commit
eeddff1dc1
@ -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!(
|
||||
|
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 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::*;
|
||||
|
||||
|
@ -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<CmdRespWithRange>),
|
||||
Contrast(ConfigDisplayCmd<CmdRespWithRange>),
|
||||
PresetMode(ConfigDisplayCmd<CmdRespWithRange>),
|
||||
}
|
@ -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<u8>) -> 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<T>(&self, msg: &T) -> anyhow::Result<()>
|
||||
where
|
||||
T: ?Sized + serde::Serialize,
|
||||
{
|
||||
self.client.publish_desktop_cmd(msg).await
|
||||
}
|
||||
|
||||
|
@ -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<display::DisplayBrightness>,
|
||||
message_tx: broadcast::Sender<models::MqMessage>,
|
||||
message_tx: broadcast::Sender<models::CmdMqMessage>,
|
||||
}
|
||||
|
||||
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::<display::DisplayBrightness>(16);
|
||||
let (message_tx, _) = broadcast::channel::<models::MqMessage>(32);
|
||||
let (message_tx, _) = broadcast::channel::<models::CmdMqMessage>(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<models::MqMessage, _> =
|
||||
let message: Result<models::CmdMqMessage, _> =
|
||||
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<display::DisplayBrightness> {
|
||||
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<T>(&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,
|
||||
|
Loading…
Reference in New Issue
Block a user