desktop/src-tauri/src/rpc/manager.rs

43 lines
1.0 KiB
Rust
Raw Normal View History

use crate::picker::led_color::LedColor;
use super::mqtt::MqttConnection;
use once_cell::sync::OnceCell;
2022-11-23 00:36:28 +08:00
use paris::info;
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",
2022-11-23 21:42:04 +08:00
rumqttc::QoS::AtLeastOnce,
false,
payload,
)
.await
.map_err(|error| anyhow::anyhow!("mqtt publish failed. {}", error))
}
}