feat: 支持 MQTT 上报灯条颜色数据
This commit is contained in:
41
src-tauri/src/rpc/manager.rs
Normal file
41
src-tauri/src/rpc/manager.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use crate::picker::led_color::LedColor;
|
||||
|
||||
use super::mqtt::MqttConnection;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
pub struct Manager {
|
||||
mqtt: MqttConnection,
|
||||
}
|
||||
|
||||
impl Manager {
|
||||
pub fn global() -> &'static Self {
|
||||
static RPC_MANAGER: OnceCell<Manager> = OnceCell::new();
|
||||
|
||||
RPC_MANAGER.get_or_init(|| Manager::new())
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut mqtt = MqttConnection::new();
|
||||
mqtt.initialize();
|
||||
Self { mqtt }
|
||||
}
|
||||
|
||||
pub async fn publish_led_colors(&self, colors: &Vec<LedColor>) -> anyhow::Result<()> {
|
||||
let payload = colors
|
||||
.iter()
|
||||
.map(|c| c.get_rgb().clone())
|
||||
.flatten()
|
||||
.collect::<Vec<u8>>();
|
||||
|
||||
self.mqtt
|
||||
.client
|
||||
.publish(
|
||||
"screen-bg-light/desktop/colors",
|
||||
rumqttc::QoS::AtMostOnce,
|
||||
false,
|
||||
payload,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| anyhow::anyhow!("mqtt publish failed. {}", error))
|
||||
}
|
||||
}
|
2
src-tauri/src/rpc/mod.rs
Normal file
2
src-tauri/src/rpc/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod manager;
|
||||
pub mod mqtt;
|
66
src-tauri/src/rpc/mqtt.rs
Normal file
66
src-tauri/src/rpc/mqtt.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use rumqttc::{AsyncClient, MqttOptions, QoS};
|
||||
use std::{time::Duration};
|
||||
use time::{format_description, OffsetDateTime};
|
||||
use tokio::task;
|
||||
use tracing::warn;
|
||||
|
||||
pub struct MqttConnection {
|
||||
pub client: AsyncClient,
|
||||
}
|
||||
|
||||
impl MqttConnection {
|
||||
pub fn new() -> Self {
|
||||
let mut options = MqttOptions::new("rumqtt-async", "192.168.31.11", 1883);
|
||||
options.set_keep_alive(Duration::from_secs(5));
|
||||
|
||||
let (mut client, mut eventloop) = AsyncClient::new(options, 10);
|
||||
task::spawn(async move {
|
||||
while let Ok(notification) = eventloop.poll().await {
|
||||
println!("Received = {:?}", notification);
|
||||
}
|
||||
});
|
||||
Self { client }
|
||||
}
|
||||
|
||||
pub fn initialize(&mut self) {
|
||||
self.subscribe_board();
|
||||
self.broadcast_desktop_online();
|
||||
}
|
||||
|
||||
fn subscribe_board(&self) {
|
||||
self.client
|
||||
.subscribe("screen-bg-light/board/#", QoS::AtMostOnce);
|
||||
}
|
||||
|
||||
fn broadcast_desktop_online(&mut self) {
|
||||
let client = self.client.to_owned();
|
||||
task::spawn(async move {
|
||||
loop {
|
||||
match OffsetDateTime::now_utc()
|
||||
.format(&format_description::well_known::Iso8601::DEFAULT)
|
||||
{
|
||||
Ok(now_str) => {
|
||||
match client
|
||||
.publish(
|
||||
"screen-bg-light/desktop/last-online-at",
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
now_str.as_bytes(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
warn!("can not publish last online time. {}", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
warn!("can not get time for now. {}", error);
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(1000)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user