Update to tokio v1, warp v0.3

This commit is contained in:
2021-01-31 03:25:10 -08:00
parent b438d6cd26
commit 39d4c225d1
2 changed files with 15 additions and 13 deletions

View File

@@ -10,8 +10,9 @@ 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 } reqwest = { version = "0.11", 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 = "1", features = ["macros"] }
warp = "0.2.5" tokio-stream = "0.1.2"
warp = "0.3"

View File

@@ -9,8 +9,9 @@ use futures_util::{FutureExt, select, stream, StreamExt};
use rppal::gpio::{Gpio, Trigger}; use rppal::gpio::{Gpio, Trigger};
use smallvec::SmallVec; use smallvec::SmallVec;
use tokio::{ sync::mpsc, use tokio::{ sync::mpsc,
time::{delay_for, interval} }; time::{interval, sleep} };
use warp::{Filter, sse, sse::ServerSentEvent}; use tokio_stream::wrappers::{IntervalStream, ReceiverStream};
use warp::{Filter, sse};
const HUE_ADDRESS: &str = "philips-hue.local"; const HUE_ADDRESS: &str = "philips-hue.local";
const OUTDOOR_CHIME_FILE: &str = "static/outdoor.mp3"; const OUTDOOR_CHIME_FILE: &str = "static/outdoor.mp3";
@@ -19,7 +20,7 @@ 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;
#[tokio::main(basic_scheduler)] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
pretty_env_logger::init_timed(); pretty_env_logger::init_timed();
@@ -38,7 +39,7 @@ async fn main() {
let reqwest = &reqwest::Client::new(); let reqwest = &reqwest::Client::new();
let audio_child = &RefCell::new(None); let audio_child = &RefCell::new(None);
let hue_busy = &Cell::new(false); let hue_busy = &Cell::new(false);
let pushes = rx.for_each_concurrent(2, |()| async move { let pushes = ReceiverStream::new(rx).for_each_concurrent(2, |()| async move {
if !audio_busy(&audio_child) { if !audio_busy(&audio_child) {
play_chime(&audio_child) play_chime(&audio_child)
} else { debug!("doorbell still ringing, not playing new chime"); } } else { debug!("doorbell still ringing, not playing new chime"); }
@@ -82,14 +83,14 @@ async fn warp(clients: Arc<Mutex<SmallVec<[mpsc::Sender<()>; CHANNEL_VEC_SIZE]>>
clients.lock().unwrap().push(tx); clients.lock().unwrap().push(tx);
let stream = stream::select( let stream = stream::select(
interval(Duration::from_secs(3)).map(move |_| { IntervalStream::new(interval(Duration::from_secs(3))).map(move |_| {
trace!("sending sse keepalive ping"); trace!("sending sse keepalive ping");
Ok::<_, Infallible>((sse::event("ping"), sse::data("")).into_a()) Ok::<_, Infallible>(sse::Event::default().event("ping").data(""))
}), }),
rx.map(|()| { ReceiverStream::new(rx).map(|()| {
debug!("sending ring sse"); debug!("sending ring sse");
Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b()) Ok::<_, Infallible>(sse::Event::default().event("ring").data(""))
}) })
); );
sse::reply(stream) sse::reply(stream)
@@ -129,7 +130,7 @@ fn play_chime(audio_child: &RefCell<Option<process::Child>>) {
async fn hue_base(reqwest: &reqwest::Client, body: &'static str, delay_millis: u64) { async fn hue_base(reqwest: &reqwest::Client, body: &'static str, delay_millis: u64) {
let _ = reqwest.put(&format!("http://{}/api/{}/lights/10/state", HUE_ADDRESS, HUE_KEY)) let _ = reqwest.put(&format!("http://{}/api/{}/lights/10/state", HUE_ADDRESS, HUE_KEY))
.body(body).send().await; .body(body).send().await;
delay_for(Duration::from_millis(delay_millis)).await; sleep(Duration::from_millis(delay_millis)).await;
} }
async fn flash_porch(reqwest: &reqwest::Client) { async fn flash_porch(reqwest: &reqwest::Client) {
@@ -147,7 +148,7 @@ async fn flash_porch(reqwest: &reqwest::Client) {
r#"{"transitiontime":0,"hue":56228}"#, // purple r#"{"transitiontime":0,"hue":56228}"#, // purple
250).await; 250).await;
} }
delay_for(Duration::from_millis(250)).await; sleep(Duration::from_millis(250)).await;
hue_base(reqwest, hue_base(reqwest,
r#"{"transitiontime":20,"bri":20}"#, // fade r#"{"transitiontime":20,"bri":20}"#, // fade
2_250).await; 2_250).await;