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:
+19
-5
@@ -32,9 +32,15 @@ use crate::{
|
|||||||
mqtt::{MQTT, StatusMessage},
|
mqtt::{MQTT, StatusMessage},
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::{ Arc, Mutex };
|
||||||
use tracing::{ info, debug };
|
use tracing::{ info, debug };
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum LolOpenerStatus {
|
||||||
|
Open,
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
@@ -59,15 +65,23 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
matrix_ready.notified().await;
|
matrix_ready.notified().await;
|
||||||
info!("Matrix is ready");
|
info!("Matrix is ready");
|
||||||
|
|
||||||
|
let current_status = Arc::new(Mutex::new(None));
|
||||||
|
let current_status = Arc::clone(¤t_status);
|
||||||
|
|
||||||
let mut mqtt = MQTT::new(config.mqtt.clone()).await;
|
let mut mqtt = MQTT::new(config.mqtt.clone()).await;
|
||||||
mqtt.subscribe(|status: StatusMessage| {
|
mqtt.subscribe(move |status: StatusMessage| {
|
||||||
let msg = match status {
|
let (new_status, msg) = match status {
|
||||||
StatusMessage { contact: true, .. } => "🔴 Le LOL est fermé :(",
|
StatusMessage { contact: true, .. } => (Some(LolOpenerStatus::Closed), "🔴 Le LOL est fermé :("),
|
||||||
StatusMessage { contact: false, .. } => "🟢 Le LOL est ouvert :)",
|
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);
|
debug!("Sending msg {}", msg);
|
||||||
//XXX: Panic if the matrix module can't get the message
|
//XXX: Panic if the matrix module can't get the message
|
||||||
bcast_tx.send((msg.into(), config.rooms.clone())).unwrap();
|
bcast_tx.send((msg.into(), config.rooms.clone())).unwrap();
|
||||||
|
*current_status = new_status;
|
||||||
|
}
|
||||||
}).await.unwrap(); //XXX: Panic if the mqtt broker fails
|
}).await.unwrap(); //XXX: Panic if the mqtt broker fails
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user