Compare commits
1
Commits
main
..
4015cac8de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4015cac8de
|
+14
-35
@@ -57,7 +57,7 @@ enum DoorSensorStatus {
|
||||
#[derive(Debug)]
|
||||
struct LolOpenerState {
|
||||
sensor_status: Option<DoorSensorStatus>,
|
||||
pending: Option<(DoorSensorStatus, String)>,
|
||||
update_pending: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -88,7 +88,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
let current_state = Arc::new(Mutex::new(LolOpenerState {
|
||||
sensor_status: None,
|
||||
pending: None
|
||||
update_pending: false
|
||||
}));
|
||||
let current_state = Arc::clone(¤t_state);
|
||||
|
||||
@@ -100,43 +100,22 @@ async fn main() -> anyhow::Result<()> {
|
||||
while let Some((new_status, msg)) = debounce_rx.recv().await {
|
||||
let should_wait = {
|
||||
//XXX: Panic if the lock is poisoned
|
||||
let mut state = current_state.lock().unwrap();
|
||||
let should_wait = state.pending.is_none();
|
||||
|
||||
state.pending = Some((new_status, msg));
|
||||
should_wait
|
||||
let state = current_state.lock().unwrap();
|
||||
!state.update_pending
|
||||
};
|
||||
|
||||
if should_wait {
|
||||
let tx = bcast_tx.clone();
|
||||
let rooms = rooms.clone();
|
||||
let state = current_state.clone();
|
||||
sleep(Duration::from_secs(debounce_delay)).await;
|
||||
//XXX: Panic if the lock is poisoned
|
||||
let mut state = current_state.lock().unwrap();
|
||||
(*state).update_pending = false;
|
||||
|
||||
tokio::spawn(async move {
|
||||
sleep(Duration::from_secs(debounce_delay)).await;
|
||||
|
||||
let msg = {
|
||||
//XXX: Panic if the lock is poisoned
|
||||
let mut state = state.lock().unwrap();
|
||||
let pending = state.pending.take();
|
||||
|
||||
match pending {
|
||||
Some((status, msg))
|
||||
if state.sensor_status.as_ref() != Some(&status) => {
|
||||
state.sensor_status = Some(status);
|
||||
Some(msg)
|
||||
}
|
||||
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(msg) = msg {
|
||||
debug!("Sending msg {}", msg);
|
||||
//XXX: Panic if the matrix module can't get the message
|
||||
tx.send((msg, rooms)).unwrap();
|
||||
}
|
||||
});
|
||||
if Some(new_status) != (*state).sensor_status {
|
||||
debug!("Sending msg {}", msg);
|
||||
//XXX: Panic if the matrix module can't get the message
|
||||
bcast_tx.send((msg.into(), rooms.clone())).unwrap();
|
||||
(*state).sensor_status = Some(new_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user