From c4c927cc9d03bf8ac743a077af913afa151e40a1 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Thu, 7 Jul 2022 01:25:15 -0400 Subject: [PATCH] Fix inbound handle reading --- src/main.rs | 22 +++++++++++++++++----- src/serial_agent.rs | 1 - 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9e98f48..d02f8ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ async fn main() -> AppResult<()> { info!("Starting ir remote"); let (outbound_serial_tx, outbound_serial_rx) = channel(100); - let (inbound_serial_tx, inbound_serial_rx) = channel(100); + let (inbound_serial_tx, mut inbound_serial_rx) = channel(100); let agent_handle: Option>> = if options.no_serial { warn!("Not starting serial port agent (--no-serial was given)"); @@ -48,6 +48,15 @@ async fn main() -> AppResult<()> { }; // TODO: Consume inbound_serial_rx, otherwise it will fill up and eventually be dropped + let inbound_serial_handle = (!options.no_serial).then(|| { + tokio::task::spawn(async move { + while let Some(m) = inbound_serial_rx.recv().await { + info!("Got message from inbound channel: {m}"); + } + + error!("Inbound serial receiver errored"); + }) + }); // let (agent_shutdown_tx, agent_handle): (Sender<()>, JoinHandle>) = { @@ -68,10 +77,13 @@ async fn main() -> AppResult<()> { .await?; tokio::select!( - // TODO: Handle the case where agent_handle is enabled or disabled - // r = agent_handle.unwrap(), if agent_handle.is_some() => { - // error!("Agent handle ended: {r:?}"); - // } + // TODO: Find a better way to do this. Unwrap is called before if statement, so unwrap_or_else must be used + r = agent_handle.unwrap_or_else(|| tokio::task::spawn(async move {Ok(())})), if agent_handle.is_some() => { + error!("Agent handle ended: {r:?}"); + } + r = inbound_serial_handle.unwrap_or_else(|| tokio::task::spawn(async move {()})), if inbound_serial_handle.is_some() => { + error!("Inbound serial handle ended: {r:?}"); + } r = webui_handle => { // agent_shutdown_tx.send(()).await?; error!("Webserver handle ended: {r:?}"); diff --git a/src/serial_agent.rs b/src/serial_agent.rs index 79cc8d9..04a8af1 100644 --- a/src/serial_agent.rs +++ b/src/serial_agent.rs @@ -58,7 +58,6 @@ fn inbound(port: Box, inbound_serial_tx: Sender) -> AppR return Err(e.into()); } Ok(m) => { - info!("Got message from inbound channel: {m}"); inbound_serial_tx.blocking_send(m)?; } }