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

@ -3,7 +3,6 @@
use log::*; //{trace, debug, info, warn, error}
use anyhow::Result;
use async_channel::{unbounded, Receiver, Sender};
use std::panic;
use crate::dispatch;
use crate::commands::Commands as Dispatch_Commands;
@ -40,32 +39,19 @@ impl MotorDriverDebug {
}
pub async fn run(&self) -> Result<()> {
//TODO: Add error propagation to all loops
loop {
match self.recv_q.recv().await {
Ok(cmd) => {
match self.handle_cmd(cmd).await {
Ok(_) => {}
Err(e) => {
error!("Unable to send commands to motor! {:?}", e);
panic!()
}
}
}
Err(e) => {
error!("Unable to receive commands for motor control! {:?}", e);
panic!();
}
}
let cmd = self.recv_q.recv().await.expect("Unexpected failure in motor driver command queue");
self.handle_cmd(cmd).await.expect("Unexpected failure of motor driver notification queue");
}
}
async fn handle_cmd(&self, cmd: Commands) -> Result<()> {
match cmd {
Commands::StartUp => {self.start_up().await?;Ok(())}
Commands::StartDown => {self.start_down().await?;Ok(())}
Commands::Stop => {self.stop().await?;Ok(())}
Commands::StartUp => {self.start_up().await?;}
Commands::StartDown => {self.start_down().await?;}
Commands::Stop => {self.stop().await?;}
}
Ok(())
}
pub async fn start_up(&self) -> Result<()> {