From e173f58da11a2b2e96c316a910876f3283d0f734 Mon Sep 17 00:00:00 2001 From: Jared Burce Date: Wed, 28 Oct 2020 18:01:54 -0700 Subject: [PATCH] Outdoor doorbell chime --- src/main.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index f54f67c..197b7d0 100644 --- a/src/main.rs +++ b/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; }); } +thread_local! { + static AUDIO_CHILD: RefCell> = 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)