From 49830795e6e9b537f98c0953c5aa0bd930221f9b Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sun, 4 Jun 2023 12:02:41 +0200 Subject: [PATCH] more errors refactoring --- src/errors.rs | 10 +++++++--- src/main.rs | 33 ++++++++++++++------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 9a9c179..3c5446f 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -5,7 +5,8 @@ use redis::RedisError; #[derive(Debug)] pub enum LJError { ConfigFileMissing, - RedisConnect(RedisError) + RedisConnect(RedisError), + HeliosDeviceMissing } impl fmt::Display for LJError { @@ -19,6 +20,9 @@ impl fmt::Display for LJError { }, RedisConnect(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::*; match self { - ConfigFileMissing => None, - RedisConnect(err) => Some(err) + RedisConnect(err) => Some(err), + _ => None } } } diff --git a/src/main.rs b/src/main.rs index 6b5fa5e..ec5c507 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,9 @@ mod redis_ctrl; mod conf; mod errors; -use helios_dac::NativeHeliosDacController; use helios_dac::{ + NativeHeliosDacController, + NativeHeliosDac, // Coordinate, Color, DeviceStatus, @@ -36,31 +37,15 @@ fn run_all() -> Result<(), Box> { return Err(Box::new(LJError::ConfigFileMissing)); }; 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> { let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); - ctrlc::set_handler(move || { r.store(false, Ordering::SeqCst); })?; - let controller = NativeHeliosDacController::new()?; - 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()?; + let mut device = get_helios_device()?; while running.load(Ordering::SeqCst) { let order = rs.get_order(config.laser_id)?; @@ -81,6 +66,16 @@ fn run_dac( Ok(()) } +fn get_helios_device() -> Result> { + 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( config: &Conf, speed: u32,