Locking for hue control

This commit is contained in:
2020-10-29 09:06:58 -07:00
parent 0ae116acc3
commit 9b147e05b4

View File

@@ -1,6 +1,7 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::convert::Infallible; use std::convert::Infallible;
use std::process; use std::process;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
@@ -20,8 +21,9 @@ const BUTTON_PIN: u8 = 26;
const CHANNEL_VEC_SIZE: usize = 32; const CHANNEL_VEC_SIZE: usize = 32;
thread_local! { thread_local! {
static AUDIO_CHILD: RefCell<Option<process::Child>> = RefCell::new(None) static AUDIO_CHILD: RefCell<Option<process::Child>> = RefCell::new(None);
} }
static HUE_LOCKED: AtomicBool = AtomicBool::new(false);
#[tokio::main(basic_scheduler)] #[tokio::main(basic_scheduler)]
async fn main() { async fn main() {
@@ -39,11 +41,16 @@ async fn main() {
let reqwest = reqwest::Client::new(); let reqwest = reqwest::Client::new();
let (tx, rx) = mpsc::channel(1); let (tx, rx) = mpsc::channel(1);
events_clients.lock().unwrap().push(tx); events_clients.lock().unwrap().push(tx);
let pushes = rx.for_each(|()| async { let pushes = rx.for_each_concurrent(2, |()| async {
if !audio_busy() { play_chime() } if !audio_busy() {
else { debug!("doorbell still ringing, not playing new chime"); } play_chime()
} else { debug!("doorbell still ringing, not playing new chime"); }
flash_patio(&reqwest).await; let locked = HUE_LOCKED.compare_and_swap(false, true, Ordering::Acquire);
if !locked {
flash_patio(&reqwest).await;
HUE_LOCKED.store(false, Ordering::Release);
} else { debug!("hue already in use, not scheduling new flashing"); }
}); });
select! { select! {