More consistent error handling in loops

This commit is contained in:
2024-08-24 10:56:05 -04:00
parent 70947d5fa7
commit ee334db157
7 changed files with 23 additions and 40 deletions

View File

@ -70,7 +70,7 @@ impl<Q: PartialEq, S: Copy> MessageTimer<Q, S> {
}
async fn wait_for_cmd(&self) -> Result<Q> {
Ok(self.recv_q.recv().await.unwrap())
Ok(self.recv_q.recv().await.expect("Timer command queue unexpectedly failed"))
}
async fn wait_with_timeout(&self, timer: &mut EspAsyncTimer) -> Result<Q> {
@ -101,18 +101,17 @@ impl<Q: PartialEq, S: Copy> MessageTimer<Q, S> {
}
pub async fn run(&mut self) -> Result<()> {
let mut cmd;
let timer_service = EspTaskTimerService::new()?;
let mut async_timer = timer_service.timer_async()?;
loop {
match self.state {
let cmd = match self.state {
State::Running => {
cmd = self.wait_with_timeout(&mut async_timer).await;
self.wait_with_timeout(&mut async_timer).await
}
State::Stopped => {
cmd = self.wait_for_cmd().await;
self.wait_for_cmd().await
}
}
};
self.handle_cmd(cmd).await;
}
}