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

60 lines
1.5 KiB
Rust
Raw Normal View History

use paris::error;
use tokio::sync::{broadcast, OnceCell};
use crate::{display, picker::led_color::LedColor, models};
use super::mqtt::MqttRpc;
pub struct Manager {
client: MqttRpc,
initialized: bool,
}
impl Manager {
pub async fn global() -> &'static Self {
static RPC_MANAGER: OnceCell<Manager> = OnceCell::const_new();
RPC_MANAGER.get_or_init(|| Manager::new()).await
}
pub async fn new() -> Self {
let mut mqtt = MqttRpc::new();
let initialized = match mqtt.initialize().await {
Ok(_) => true,
Err(err) => {
error!("initialize for mqtt was failed. {:?}", err);
false
}
};
Self {
client: mqtt,
initialized,
}
}
pub async fn listen(&self) {
self.client.listen().await
}
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.publish_led_sub_pixels(payload).await
}
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<()> {
self.client.publish_desktop_cmd(msg).await
}
pub fn client(&self) -> &MqttRpc {
&self.client
}
}