From 39d4c225d129eebeb32d963cc3dd0a53bee2deed Mon Sep 17 00:00:00 2001 From: Jared Burce Date: Sun, 31 Jan 2021 03:25:10 -0800 Subject: [PATCH] Update to tokio v1, warp v0.3 --- Cargo.toml | 7 ++++--- src/main.rs | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1434ec3..332a798 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,9 @@ edition = "2018" futures-util = "0.3" log = "0.4.11" 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" } smallvec = "1.4.2" -tokio = { version = "0.2", features = ["macros"] } -warp = "0.2.5" +tokio = { version = "1", features = ["macros"] } +tokio-stream = "0.1.2" +warp = "0.3" diff --git a/src/main.rs b/src/main.rs index 47e293c..e2cc652 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,9 @@ use futures_util::{FutureExt, select, stream, StreamExt}; use rppal::gpio::{Gpio, Trigger}; use smallvec::SmallVec; use tokio::{ sync::mpsc, - time::{delay_for, interval} }; -use warp::{Filter, sse, sse::ServerSentEvent}; + time::{interval, sleep} }; +use tokio_stream::wrappers::{IntervalStream, ReceiverStream}; +use warp::{Filter, sse}; const HUE_ADDRESS: &str = "philips-hue.local"; 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 CHANNEL_VEC_SIZE: usize = 32; -#[tokio::main(basic_scheduler)] +#[tokio::main(flavor = "current_thread")] async fn main() { pretty_env_logger::init_timed(); @@ -38,7 +39,7 @@ async fn main() { let reqwest = &reqwest::Client::new(); let audio_child = &RefCell::new(None); 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) { play_chime(&audio_child) } else { debug!("doorbell still ringing, not playing new chime"); } @@ -82,14 +83,14 @@ async fn warp(clients: Arc; CHANNEL_VEC_SIZE]>> clients.lock().unwrap().push(tx); 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"); - 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"); - Ok::<_, Infallible>((sse::event("ring"), sse::data("")).into_b()) + Ok::<_, Infallible>(sse::Event::default().event("ring").data("")) }) ); sse::reply(stream) @@ -129,7 +130,7 @@ fn play_chime(audio_child: &RefCell>) { 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)) .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) { @@ -147,7 +148,7 @@ async fn flash_porch(reqwest: &reqwest::Client) { r#"{"transitiontime":0,"hue":56228}"#, // purple 250).await; } - delay_for(Duration::from_millis(250)).await; + sleep(Duration::from_millis(250)).await; hue_base(reqwest, r#"{"transitiontime":20,"bri":20}"#, // fade 2_250).await;