Outdoor doorbell chime
This commit is contained in:
28
src/main.rs
28
src/main.rs
@@ -1,4 +1,6 @@
|
||||
use std::cell::RefCell;
|
||||
use std::convert::Infallible;
|
||||
use std::process;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -24,6 +26,10 @@ fn button_pressed(_level: Level, clients: &Arc<Mutex<SmallVec<[mpsc::Sender<()>;
|
||||
});
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static AUDIO_CHILD: RefCell<Option<process::Child>> = RefCell::new(None)
|
||||
}
|
||||
|
||||
#[tokio::main(basic_scheduler)]
|
||||
async fn main() {
|
||||
pretty_env_logger::init_timed();
|
||||
@@ -57,9 +63,27 @@ async fn main() {
|
||||
Ok::<_, Infallible>((sse::event("ping"), sse::data("")).into_a())
|
||||
}),
|
||||
|
||||
rx.map(|()| {
|
||||
rx.filter_map(|()| async {
|
||||
AUDIO_CHILD.with(|cell| {
|
||||
if let Some(ref mut audio_child) = *cell.borrow_mut() {
|
||||
if let Ok(None) = audio_child.try_wait() {
|
||||
debug!("doorbell still ringing, ignoring press");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
})?;
|
||||
debug!("Playing doorbell chime");
|
||||
match process::Command::new("mplayer")
|
||||
.arg("static/outdoor.mp3")
|
||||
.stdout(process::Stdio::null())
|
||||
.stderr(process::Stdio::null())
|
||||
.spawn() {
|
||||
Ok(child) => { AUDIO_CHILD.with(|cell| cell.replace(Some(child))); },
|
||||
Err(err) => error!("Error playing outdoor chime: {}", err)
|
||||
};
|
||||
debug!("sending ring sse");
|
||||
Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b())
|
||||
Some(Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b()))
|
||||
})
|
||||
);
|
||||
sse::reply(stream)
|
||||
|
||||
Reference in New Issue
Block a user