feat: 命令执行成功后发布相关状态到 MQ. #5.

This commit is contained in:
2023-02-19 17:38:09 +08:00
parent e09b93432c
commit eeddff1dc1
6 changed files with 47 additions and 16 deletions

View File

@@ -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
}

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, 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,