more errors refactoring

This commit is contained in:
Marc Planard 2023-06-04 12:02:41 +02:00
parent c18de96e10
commit 49830795e6
2 changed files with 21 additions and 22 deletions

View File

@ -5,7 +5,8 @@ use redis::RedisError;
#[derive(Debug)] #[derive(Debug)]
pub enum LJError { pub enum LJError {
ConfigFileMissing, ConfigFileMissing,
RedisConnect(RedisError) RedisConnect(RedisError),
HeliosDeviceMissing
} }
impl fmt::Display for LJError { impl fmt::Display for LJError {
@ -19,6 +20,9 @@ impl fmt::Display for LJError {
}, },
RedisConnect(err) => { RedisConnect(err) => {
write!(f, "unable to connect to redis server: {err}") write!(f, "unable to connect to redis server: {err}")
},
HeliosDeviceMissing => {
write!(f, "helios device not found")
} }
} }
} }
@ -29,8 +33,8 @@ impl Error for LJError {
use LJError::*; use LJError::*;
match self { match self {
ConfigFileMissing => None, RedisConnect(err) => Some(err),
RedisConnect(err) => Some(err) _ => None
} }
} }
} }

View File

@ -6,8 +6,9 @@ mod redis_ctrl;
mod conf; mod conf;
mod errors; mod errors;
use helios_dac::NativeHeliosDacController;
use helios_dac::{ use helios_dac::{
NativeHeliosDacController,
NativeHeliosDac,
// Coordinate, // Coordinate,
Color, Color,
DeviceStatus, DeviceStatus,
@ -36,31 +37,15 @@ fn run_all() -> Result<(), Box<dyn std::error::Error>> {
return Err(Box::new(LJError::ConfigFileMissing)); return Err(Box::new(LJError::ConfigFileMissing));
}; };
let config = load_config(&filename)?; let config = load_config(&filename)?;
let rs = RedisCtrl::new()?; let mut rs = RedisCtrl::new()?;
run_dac(config, rs)
}
fn run_dac(
config: Conf,
mut rs: RedisCtrl
) -> Result<(), Box<dyn std::error::Error>> {
let running = Arc::new(AtomicBool::new(true)); let running = Arc::new(AtomicBool::new(true));
let r = running.clone(); let r = running.clone();
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst); r.store(false, Ordering::SeqCst);
})?; })?;
let controller = NativeHeliosDacController::new()?; let mut device = get_helios_device()?;
let devices = controller.list_devices()?;
let Some(device) = devices.into_iter().next() else {
println!("Unable to find an helios device");
return Ok(());
};
let mut device = device.open()?;
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {
let order = rs.get_order(config.laser_id)?; let order = rs.get_order(config.laser_id)?;
@ -81,6 +66,16 @@ fn run_dac(
Ok(()) Ok(())
} }
fn get_helios_device() -> Result<NativeHeliosDac, Box<dyn std::error::Error>> {
let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?;
let Some(device) = devices.into_iter().next() else {
return Err(Box::new(LJError::HeliosDeviceMissing));
};
let device = device.open()?;
Ok(device)
}
fn get_next_frame( fn get_next_frame(
config: &Conf, config: &Conf,
speed: u32, speed: u32,