Compare commits

..

1 Commits

Author SHA1 Message Date
2f1453dd63 feat: MQTT client 改为 paho-mqtt. #5. 2023-02-19 16:14:47 +08:00
8 changed files with 144 additions and 48 deletions

128
src-tauri/Cargo.lock generated
View File

@ -867,6 +867,7 @@ dependencies = [
"paho-mqtt",
"paris",
"redb",
"rumqttc",
"scrap",
"serde",
"serde_json",
@ -1027,7 +1028,7 @@ dependencies = [
"futures-sink",
"nanorand",
"pin-project",
"spin",
"spin 0.9.4",
]
[[package]]
@ -2706,6 +2707,12 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "pollster"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5da3b0203fd7ee5720aa0b5e790b591aa5d3f41c3ed2c34a3a393382198af2f7"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@ -3012,6 +3019,40 @@ dependencies = [
"windows 0.37.0",
]
[[package]]
name = "ring"
version = "0.16.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin 0.5.2",
"untrusted",
"web-sys",
"winapi 0.3.9",
]
[[package]]
name = "rumqttc"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "499b7ab08ffa5a722958b6ce1b7c0270bea30909f589d12c5ec3a051afe423fc"
dependencies = [
"bytes",
"flume",
"futures",
"http",
"log",
"pollster",
"rustls-native-certs",
"rustls-pemfile 0.3.0",
"thiserror",
"tokio",
"tokio-rustls",
]
[[package]]
name = "rustc_version"
version = "0.3.3"
@ -3030,6 +3071,48 @@ dependencies = [
"semver 1.0.16",
]
[[package]]
name = "rustls"
version = "0.20.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "539a2bfe908f471bfa933876bd1eb6a19cf2176d375f82ef7f99530a40e48c2c"
dependencies = [
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls-native-certs"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50"
dependencies = [
"openssl-probe",
"rustls-pemfile 1.0.1",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pemfile"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ee86d63972a7c661d1536fefe8c3c8407321c3df668891286de28abcd087360"
dependencies = [
"base64",
]
[[package]]
name = "rustls-pemfile"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
dependencies = [
"base64",
]
[[package]]
name = "rustversion"
version = "1.0.11"
@ -3097,6 +3180,16 @@ dependencies = [
"winapi 0.2.8",
]
[[package]]
name = "sct"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "security-framework"
version = "2.7.0"
@ -3395,6 +3488,12 @@ dependencies = [
"system-deps 5.0.0",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.9.4"
@ -3934,6 +4033,17 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-rustls"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59"
dependencies = [
"rustls",
"tokio",
"webpki",
]
[[package]]
name = "toml"
version = "0.5.10"
@ -4075,6 +4185,12 @@ version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74c1aa4511c38276c548406f0b1f5f8b793f000cfb51e18f278a102abd057e81"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "url"
version = "2.3.1"
@ -4316,6 +4432,16 @@ dependencies = [
"libwebp-sys",
]
[[package]]
name = "webpki"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "webview2-com"
version = "0.19.1"

View File

@ -28,6 +28,7 @@ tokio = { version = "1.22.0", features = ["full"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
hex = "0.4.3"
rumqttc = "0.17.0"
time = { version = "0.3.17", features = ["formatting"] }
color_space = "0.5.3"
futures = "0.3.25"

View File

@ -6,7 +6,6 @@ use std::{
time::{Duration, SystemTime},
};
use base64::Config;
use ddc_hi::Display;
use paris::{error, info};
use tauri::async_runtime::Mutex;
@ -158,16 +157,10 @@ impl Manager {
let rpc = rpc::Manager::global().await;
rpc.publish_desktop_cmd(&models::CmdRespMqMessage::Brightness(
models::ConfigDisplayCmd {
rpc.publish_desktop_cmd(models::MqMessage::Brightness(models::ConfigDisplayCmd {
display_index: config.id,
value: models::CmdRespWithRange {
value: config.brightness,
min: config.min_brightness,
max: config.max_brightness,
},
},
))
value: models::ControlValue::Absolute(config.brightness),
}))
.await;
}
Err(err) => {

View File

@ -1,8 +0,0 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct CmdRespWithRange<T = u16> {
pub value: T,
pub max: T,
pub min: T,
}

View File

@ -1,10 +1,8 @@
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::*;

View File

@ -1,17 +1,11 @@
use serde::{Serialize, Deserialize};
use super::{ConfigDisplayCmd, CmdRespWithRange};
use super::ConfigDisplayCmd;
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub enum CmdMqMessage {
pub enum MqMessage {
Brightness(ConfigDisplayCmd),
Contrast(ConfigDisplayCmd),
PresetMode(ConfigDisplayCmd),
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub enum CmdRespMqMessage {
Brightness(ConfigDisplayCmd<CmdRespWithRange>),
Contrast(ConfigDisplayCmd<CmdRespWithRange>),
PresetMode(ConfigDisplayCmd<CmdRespWithRange>),
}

View File

@ -1,6 +1,5 @@
use paris::error;
use tokio::sync::{broadcast, OnceCell};
use std::fmt::Debug;
use crate::{display, models, picker::led_color::LedColor};
@ -52,10 +51,7 @@ 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<T>(&self, msg: &T) -> anyhow::Result<()>
where
T: ?Sized + serde::Serialize,
{
pub async fn publish_desktop_cmd(&self, msg: models::MqMessage) -> anyhow::Result<()> {
self.client.publish_desktop_cmd(msg).await
}

View File

@ -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, fmt::Debug, rc::Rc, sync::Arc, time::Duration};
use std::{borrow::Borrow, 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::CmdMqMessage>,
message_tx: broadcast::Sender<models::MqMessage>,
}
impl MqttRpc {
@ -64,7 +64,6 @@ 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();
@ -79,7 +78,7 @@ impl MqttRpc {
let (change_display_brightness_tx, _) =
broadcast::channel::<display::DisplayBrightness>(16);
let (message_tx, _) = broadcast::channel::<models::CmdMqMessage>(32);
let (message_tx, _) = broadcast::channel::<models::MqMessage>(32);
Ok(Self {
client,
change_display_brightness_tx,
@ -132,7 +131,7 @@ impl MqttRpc {
let payload_text = String::from_utf8(notification.payload().to_vec());
match payload_text {
Ok(payload_text) => {
let message: Result<models::CmdMqMessage, _> =
let message: Result<models::MqMessage, _> =
serde_json::from_str(payload_text.as_str());
match message {
Ok(message) => match message_tx_cloned.send(message) {
@ -227,12 +226,9 @@ impl MqttRpc {
) -> broadcast::Receiver<display::DisplayBrightness> {
self.change_display_brightness_tx.subscribe()
}
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))?;
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))?;
self.client
.publish(mqtt::Message::new(
DESKTOP_SEND_CMD,