2022-11-20 20:09:25 +08:00
|
|
|
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;
|
2022-11-20 20:09:25 +08:00
|
|
|
|
|
|
|
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,
|
2022-11-20 20:09:25 +08:00
|
|
|
false,
|
|
|
|
payload,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|error| anyhow::anyhow!("mqtt publish failed. {}", error))
|
|
|
|
}
|
|
|
|
}
|