src: main: Do not send a matrix message on duplicate status updates

Keep track of the last status update and post to the matrix room only if
it changes.
This commit is contained in:
2026-09-14 16:54:56 +00:00
parent 0b700cabc4
commit d4b16451b9
+19 -5
View File
@@ -32,9 +32,15 @@ use crate::{
mqtt::{MQTT, StatusMessage},
};
use std::sync::Arc;
use std::sync::{ Arc, Mutex };
use tracing::{ info, debug };
#[derive(PartialEq)]
enum LolOpenerStatus {
Open,
Closed,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
@@ -59,15 +65,23 @@ async fn main() -> anyhow::Result<()> {
matrix_ready.notified().await;
info!("Matrix is ready");
let current_status = Arc::new(Mutex::new(None));
let current_status = Arc::clone(&current_status);
let mut mqtt = MQTT::new(config.mqtt.clone()).await;
mqtt.subscribe(|status: StatusMessage| {
let msg = match status {
StatusMessage { contact: true, .. } => "🔴 Le LOL est fermé :(",
StatusMessage { contact: false, .. } => "🟢 Le LOL est ouvert :)",
mqtt.subscribe(move |status: StatusMessage| {
let (new_status, msg) = match status {
StatusMessage { contact: true, .. } => (Some(LolOpenerStatus::Closed), "🔴 Le LOL est fermé :("),
StatusMessage { contact: false, .. } => (Some(LolOpenerStatus::Open), "🟢 Le LOL est ouvert :)"),
};
let mut current_status = current_status.lock().unwrap();
if new_status != *current_status {
debug!("Sending msg {}", msg);
//XXX: Panic if the matrix module can't get the message
bcast_tx.send((msg.into(), config.rooms.clone())).unwrap();
*current_status = new_status;
}
}).await.unwrap(); //XXX: Panic if the mqtt broker fails
})
};