refatoring errors

This commit is contained in:
Marc Planard 2023-06-04 11:50:05 +02:00
commit c18de96e10
10 changed files with 143 additions and 87 deletions

View File

@ -1,5 +1,5 @@
[package] [package]
name = "lazer" name = "lj_rust"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View File

@ -1,6 +1,20 @@
# file: Settings.toml
# Rename me !
# The main key of your laser in LJ
laser_id = 0 laser_id = 0
# Activate for more debug
debug = "true" debug = "true"
# Redis URL as IP:port
redis_url = "127.0.0.1" redis_url = "127.0.0.1"
# Either Helios or Etherdream
dac_family = "Helios" dac_family = "Helios"
dac_id = 1
# For Helios. USB Device Id of the DAC
dac_id = 0
# For Etherdream. IP of the DAC
dac_url = "192.168.1.68" dac_url = "192.168.1.68"

View File

@ -1,31 +1,26 @@
use config::Config; use config::Config;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize, Debug)]
#[derive(Deserialize,Debug)]
pub enum DacFamily { pub enum DacFamily {
Helios, Helios,
Etherdream Etherdream,
}
#[derive(Deserialize,Debug)]
pub struct Conf {
laser_id : u8,
debug : bool,
redis_url : String,
dac_family: DacFamily,
dac_id : Option<u8>,
dac_url : Option<String>
} }
#[derive(Deserialize, Debug)]
pub fn load_config( path : &str )-> Result<Conf, Box<dyn std::error::Error>> { pub struct Conf {
pub laser_id: u8,
pub debug: bool,
pub redis_url: String,
pub dac_family: DacFamily,
pub dac_id: Option<u8>,
pub dac_url: Option<String>,
}
pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
let settings = Config::builder() let settings = Config::builder()
.add_source(config::File::with_name(path)) .add_source(config::File::with_name(path))
.build()?; .build()?;
let conf : Conf = settings.try_deserialize()?; let conf : Conf = settings.try_deserialize()?;
Ok(conf) Ok(conf)
} }

View File

@ -1,12 +1,11 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use redis::RedisError;
use config::ConfigError;
#[derive(Debug)] #[derive(Debug)]
pub enum LJError { pub enum LJError {
ConfigFileMissing, ConfigFileMissing,
RedisConnect(RedisError)
} }
impl fmt::Display for LJError { impl fmt::Display for LJError {
@ -18,6 +17,9 @@ impl fmt::Display for LJError {
write!(f, "first argument must be a TOML config file \ write!(f, "first argument must be a TOML config file \
(see copyme.Settings.toml)") (see copyme.Settings.toml)")
}, },
RedisConnect(err) => {
write!(f, "unable to connect to redis server: {err}")
}
} }
} }
} }
@ -28,6 +30,7 @@ impl Error for LJError {
match self { match self {
ConfigFileMissing => None, ConfigFileMissing => None,
RedisConnect(err) => Some(err)
} }
} }
} }

3
src/lib.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod conf;
pub mod redis_ctrl;
pub mod errors;

View File

@ -2,20 +2,20 @@
/// Configure udev: /// Configure udev:
/// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md /// https://github.com/Grix/helios_dac/blob/master/docs/udev_rules_for_linux.md
/// ///
mod redis_ctrl; mod redis_ctrl;
mod conf; mod conf;
mod errors; mod errors;
use helios_dac::{Frame,
Point,
DeviceStatus,
// Coordinate,
Color};
use helios_dac::NativeHeliosDacController; use helios_dac::NativeHeliosDacController;
use helios_dac::{
// Coordinate,
Color,
DeviceStatus,
Frame,
Point,
};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order}; use redis_ctrl::{RedisCtrl,Order};
use conf::{load_config,Conf}; use conf::{load_config,Conf};
use errors::LJError; use errors::LJError;
@ -35,9 +35,7 @@ fn run_all() -> Result<(), Box<dyn std::error::Error>> {
let Some(filename) = std::env::args().nth(1) else { let Some(filename) = std::env::args().nth(1) else {
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 rs = RedisCtrl::new()?;
run_dac(config, rs) run_dac(config, rs)
@ -65,12 +63,13 @@ fn run_dac(
let mut device = device.open()?; let mut device = device.open()?;
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {
let order = rs.get_order(0)?; let order = rs.get_order(config.laser_id)?;
if order != Order::Draw { if order != Order::Draw {
println!("{:?}", order); println!("{:?}", order);
} }
let frame = get_next_frame(1000, &mut rs, order == Order::Black)?; let frame = get_next_frame(&config, 1000,
&mut rs, order == Order::Black)?;
while let Ok(DeviceStatus::NotReady) = device.status() { while let Ok(DeviceStatus::NotReady) = device.status() {
} }
@ -83,15 +82,14 @@ fn run_dac(
} }
fn get_next_frame( fn get_next_frame(
config: &Conf,
speed: u32, speed: u32,
rs: &mut RedisCtrl, rs: &mut RedisCtrl,
black: bool _black: bool
) -> Result<Frame, Box<dyn std::error::Error>> { ) -> Result<Frame, Box<dyn std::error::Error>> {
let line = rs.get("/pl/0/0")?; let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let line : Vec<Point> = line.iter() let line: Vec<Point> = line.iter().map(tuple_to_point).collect();
.map(tuple_to_point)
.collect();
let mut line2 = vec![]; let mut line2 = vec![];
while line2.len() < 48 { while line2.len() < 48 {
@ -99,11 +97,13 @@ fn get_next_frame(
line2.push(*p); line2.push(*p);
} }
} }
println!("{:?}", line2); println!("{:?}", line2);
Ok(Frame::new(speed, line2)) Ok(Frame::new(speed, line2))
} }
fn tuple_to_point(tpl: &(f32,f32,u32)) -> Point { fn tuple_to_point(tpl: &(f32, f32, u32)) -> Point {
let (x, y, col) = tpl; let (x, y, col) = tpl;
let r = (col >> 16) as u8 ; let r = (col >> 16) as u8 ;
@ -122,6 +122,6 @@ fn tuple_to_point(tpl: &(f32,f32,u32)) -> Point {
Point { Point {
coordinate: (x, y).into(), coordinate: (x, y).into(),
color: Color::new(r, g, b), color: Color::new(r, g, b),
intensity: 0xFF intensity: 0xFF,
} }
} }

View File

@ -1,12 +1,9 @@
use redis::{ use redis::{Client, Commands, Connection};
Client,
Connection,
Commands
};
use ron::de::from_str; use ron::de::from_str;
use crate::errors::LJError;
#[repr(u8)] #[repr(u8)]
#[derive(Debug,PartialEq)] #[derive(Debug, PartialEq)]
pub enum Order { pub enum Order {
Draw = 0, Draw = 0,
Edh, //homography Edh, //homography
@ -16,7 +13,7 @@ pub enum Order {
ClientKey, ClientKey,
Intensity, Intensity,
Kpps, Kpps,
ColorBalance ColorBalance,
} }
impl TryFrom<u8> for Order { impl TryFrom<u8> for Order {
@ -44,36 +41,31 @@ impl TryFrom<u8> for Order {
} }
} }
pub type Line = Vec<(f32,f32,u32)>; pub type Line = Vec<(f32, f32, u32)>;
pub struct RedisCtrl { pub struct RedisCtrl {
pub client: Client, pub client: Client,
pub connection: Connection pub connection: Connection,
} }
impl RedisCtrl { impl RedisCtrl {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> { pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let client = Client::open("redis://127.0.0.1/")?; let client = Client::open("redis://127.0.0.1/")
let connection = client.get_connection()?; .map_err(| err | LJError::RedisConnect(err))?;
let connection = client.get_connection()
.map_err(| err | LJError::RedisConnect(err))?;
Ok(RedisCtrl { client, connection }) Ok(RedisCtrl { client, connection })
} }
pub fn get( pub fn get(&mut self, key: &str) -> Result<Line, Box<dyn std::error::Error>> {
&mut self, let val: String = self.connection.get(key)?;
key: &str let line: Line = from_str(&val)?;
) -> Result<Line, Box<dyn std::error::Error>> {
let val : String = self.connection.get(key)?;
let line : Line = from_str(&val)?;
Ok(line) Ok(line)
} }
pub fn get_order( pub fn get_order(&mut self, id: u8) -> Result<Order, Box<dyn std::error::Error>> {
&mut self,
id: u8
) -> Result<Order, Box<dyn std::error::Error>> {
let path = format!("/order/{id}"); let path = format!("/order/{id}");
let val : u8 = self.connection.get(path.clone())?; let val: u8 = self.connection.get(path.clone())?;
if val == 1 || val >= 4 { if val == 1 || val >= 4 {
self.connection.set(path, 0)?; self.connection.set(path, 0)?;
@ -81,6 +73,4 @@ impl RedisCtrl {
Ok(val.try_into()?) Ok(val.try_into()?)
} }
} }

View File

19
tests/settings/valid.toml Normal file
View File

@ -0,0 +1,19 @@
# file: valid.toml
# The main key of your laser in LJ
laser_id = 0
# Activate for more debug
debug = "true"
# Redis URL as IP:port
redis_url = "127.0.0.1"
# Either Helios or Etherdream
dac_family = "Helios"
# For Helios. USB Device Id of the DAC
dac_id = 0
# For Etherdream. IP of the DAC
dac_url = "192.168.1.68"

32
tests/test_conf.rs Normal file
View File

@ -0,0 +1,32 @@
use lj_rust::conf::{load_config, DacFamily};
#[test]
fn it_loads_a_valid_conf() {
let result = load_config("tests/settings/valid");
assert!(result.is_ok());
}
#[test]
fn it_fails_invalid_conf() {
let result = load_config("tests/settings/empty");
assert!(result.is_err());
}
#[test]
fn it_finds_struct_fields() {
let config = match load_config("tests/settings/valid") {
Ok(c) => c,
Err(err) => {
panic!("Unable to load config file: {:?}", err)
}
};
assert_eq!(config.laser_id, u8::from(0));
assert_eq!(config.debug, true);
assert_eq!(config.redis_url, String::from("127.0.0.1"));
assert!(match config.dac_family {
DacFamily::Helios => true,
_ => false,
});
assert_eq!(config.dac_id, Some(0));
assert_eq!(config.dac_url, Some(String::from("192.168.1.68")));
}