Move audio playback to toplevel, out of sse stream generation

This commit is contained in:
2020-10-29 06:30:20 -07:00
parent f4d2280680
commit ab961382ba

View File

@@ -4,7 +4,7 @@ use std::process;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use futures_util::{stream, StreamExt};
use futures_util::{FutureExt, select, stream, StreamExt};
#[allow(unused_imports)] use log::{debug, error, info, trace};
use rppal::gpio::{Gpio, Level, Trigger};
use smallvec::SmallVec;
@@ -46,8 +46,27 @@ async fn main() {
pin.set_async_interrupt(Trigger::FallingEdge, move |level| button_pressed(level, &clients))
.expect("set interrupt");
let warp = warp(events_clients);
warp.await;
let (tx, rx) = mpsc::channel(1);
events_clients.lock().unwrap().push(tx);
let pushes = rx.for_each(|()| {
if !audio_busy() {
trace!("Playing doorbell chime");
match process::Command::new("mplayer")
.arg(OUTDOOR_CHIME_FILE)
.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)
};
} else { debug!("doorbell still ringing, not playing new chime"); }
futures_util::future::ready(())
});
select! {
_ = warp(events_clients).fuse() => (),
_ = pushes.fuse() => ()
}
}
async fn warp(clients: Arc<Mutex<SmallVec<[mpsc::Sender<()>; CHANNEL_VEC_SIZE]>>>) {
@@ -72,18 +91,6 @@ async fn warp(clients: Arc<Mutex<SmallVec<[mpsc::Sender<()>; CHANNEL_VEC_SIZE]>>
}),
rx.map(|()| {
if !audio_busy() {
trace!("Playing doorbell chime");
match process::Command::new("mplayer")
.arg(OUTDOOR_CHIME_FILE)
.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)
};
} else { debug!("doorbell still ringing, not playing new chime"); }
debug!("sending ring sse");
Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b())
})