Hacky hue flashing-- no locking multiple presses

This commit is contained in:
2020-10-29 07:43:00 -07:00
parent 1730f55d21
commit 6e7e74205e
3 changed files with 27 additions and 3 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/target /target
.emacs.desktop* .emacs.desktop*
hue.key

View File

@@ -10,6 +10,7 @@ edition = "2018"
futures-util = "0.3" futures-util = "0.3"
log = "0.4.11" log = "0.4.11"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
reqwest = { version = "0.10", default-features = false }
rppal = { git = "https://github.com/golemparts/rppal/", rev = "2e980caf76756c97bb0b18fb3ab08fb51ed1f90e" } rppal = { git = "https://github.com/golemparts/rppal/", rev = "2e980caf76756c97bb0b18fb3ab08fb51ed1f90e" }
smallvec = "1.4.2" smallvec = "1.4.2"
tokio = { version = "0.2", features = ["macros"] } tokio = { version = "0.2", features = ["macros"] }

View File

@@ -9,11 +9,13 @@ use futures_util::{FutureExt, select, stream, StreamExt};
use rppal::gpio::{Gpio, Level, Trigger}; use rppal::gpio::{Gpio, Level, Trigger};
use smallvec::SmallVec; use smallvec::SmallVec;
use tokio::{ sync::mpsc, use tokio::{ sync::mpsc,
time::interval }; time::{delay_for, interval} };
use warp::{Filter, sse, sse::ServerSentEvent}; use warp::{Filter, sse, sse::ServerSentEvent};
const HUE_ADDRESS: &str = "philips-hue.local";
const OUTDOOR_CHIME_FILE: &str = "static/outdoor.mp3"; const OUTDOOR_CHIME_FILE: &str = "static/outdoor.mp3";
const HUE_KEY: &str = include_str!("../hue.key");
const BUTTON_PIN: u8 = 26; const BUTTON_PIN: u8 = 26;
const CHANNEL_VEC_SIZE: usize = 32; const CHANNEL_VEC_SIZE: usize = 32;
@@ -34,12 +36,14 @@ async fn main() {
pin.set_async_interrupt(Trigger::FallingEdge, move |level| button_pressed(level, &clients)) pin.set_async_interrupt(Trigger::FallingEdge, move |level| button_pressed(level, &clients))
.expect("set interrupt"); .expect("set interrupt");
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(|()| { let pushes = rx.for_each(|()| async {
if !audio_busy() { play_chime() } if !audio_busy() { play_chime() }
else { debug!("doorbell still ringing, not playing new chime"); } else { debug!("doorbell still ringing, not playing new chime"); }
futures_util::future::ready(())
flash_patio(&reqwest).await;
}); });
select! { select! {
@@ -121,3 +125,21 @@ fn play_chime() {
Err(err) => error!("Error playing outdoor chime: {}", err) Err(err) => error!("Error playing outdoor chime: {}", err)
}; };
} }
fn hue_base(reqwest: &reqwest::Client) -> reqwest::RequestBuilder {
reqwest.put(&format!("http://{}/api/{}/lights/10/state", HUE_ADDRESS, HUE_KEY))
}
async fn flash_patio(reqwest: &reqwest::Client) {
for _ in 0..5 {
let _ = hue_base(reqwest).body(r#"{"transitiontime":0,"sat":254, "bri":254,"hue":2125}"#) // orange
.send().await;
delay_for(Duration::from_millis(250)).await;
let _ = hue_base(reqwest).body(r#"{"transitiontime":0,"hue":25500}"#) // green
.send().await;
delay_for(Duration::from_millis(250)).await;
let _ = hue_base(reqwest).body(r#"{"transitiontime":0,"hue":56228}"#) // purple
.send().await;
delay_for(Duration::from_millis(250)).await;
}
}