use paris::error; use tokio::sync::{broadcast, OnceCell}; use crate::{display, picker::led_color::LedColor}; use super::mqtt::MqttRpc; pub struct Manager { client: MqttRpc, initialized: bool, } impl Manager { pub async fn global() -> &'static Self { static RPC_MANAGER: OnceCell = 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 publish_led_colors(&self, colors: &Vec) -> anyhow::Result<()> { let payload = colors .iter() .map(|c| c.get_rgb().clone()) .flatten() .collect::>(); self.publish_led_sub_pixels(payload).await } pub async fn publish_led_sub_pixels(&self, payload: Vec) -> anyhow::Result<()> { self.client.publish_led_sub_pixels(payload).await } pub fn client(&self) -> &MqttRpc { &self.client } }