From ad3b3803cee3df42e531dfa56aa4cd589babac3f Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Wed, 7 Jun 2023 17:26:30 +0200 Subject: [PATCH] load transformers from config file --- Cargo.toml | 1 + copyme.Settings.toml | 8 ++++++++ src/conf.rs | 40 +++++++++++++++++++++++++++++++++++- src/lib.rs | 4 +++- src/main.rs | 11 ++++------ src/transformer/replicate.rs | 3 ++- src/transformer/translate.rs | 4 +++- 7 files changed, 60 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ccbd534..18f311d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ log = "0.4.18" redis = "0.23.0" ron = "0.8.0" serde = { version = "1.0.163", features = ["derive"] } +toml = "0.7.4" diff --git a/copyme.Settings.toml b/copyme.Settings.toml index d0055a3..f440bc0 100644 --- a/copyme.Settings.toml +++ b/copyme.Settings.toml @@ -18,3 +18,11 @@ id = 0 # For Etherdream. IP of the DAC # [dac.etherdream] # url = "192.168.1.68" + +[[transformers]] +[transformers.translate] +x = 2000 +y = 2000 +[[transformers]] +[transformers.replicate] +Until = 48 diff --git a/src/conf.rs b/src/conf.rs index a92dcff..781bcaa 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,13 +1,16 @@ use config::Config; use serde::{Serialize,Deserialize}; use crate::errors::LJResult; +use crate::transformer; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Conf { pub laser_id: u8, pub debug: bool, pub redis_url: String, - pub dac: DacFamily + pub dac: DacFamily, + #[serde(default)] + pub transformers: Vec } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -28,6 +31,15 @@ pub struct EtherDreamConf { pub url: String } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum TransformConf { + #[serde(rename = "translate")] + Translate(transformer::Translate), + #[serde(rename = "replicate")] + Replicate(transformer::Replicate) +} + + impl Conf { pub fn new(path: &str) -> LJResult { let settings = Config::builder() @@ -37,4 +49,30 @@ impl Conf { let conf : Conf = settings.try_deserialize()?; Ok(conf) } + + pub fn get_transformers(&self) -> Vec> { + let mut v = vec![]; + for t in &self.transformers { + let t : Box = match t { + TransformConf::Translate(t) => Box::new(t.clone()), + TransformConf::Replicate(r) => Box::new(r.clone()) + }; + v.push(t); + } + v + } + + pub fn dump() { + let conf = Conf { laser_id: 0, + debug: true, + redis_url: "redis://127.0.0.1:6379/".to_string(), + dac: DacFamily::Helios(HeliosConf { id: 0 }), + transformers: vec![ + TransformConf::Translate(transformer::Translate::new(2000.0,2000.0)), + TransformConf::Replicate(transformer::Replicate::Until(48)) + ] + }; + let s = toml::to_string(&conf).unwrap(); + println!("{}", s); + } } diff --git a/src/lib.rs b/src/lib.rs index 937f09a..ad51a61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ -pub mod conf; +/* pub mod redis_ctrl; +pub mod conf; pub mod errors; pub mod device; pub mod point; +*/ diff --git a/src/main.rs b/src/main.rs index 4b8ba46..7debd47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,14 +14,13 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use redis_ctrl::{RedisCtrl, Order}; use conf::Conf; -use errors::{LJError, LJResult}; +use errors::LJResult; use point::Point; use transformer::{Transformers, Translate, Replicate}; use log::{LevelFilter, info, /* warn, */ error}; use env_logger::Builder; const DEFAULT_CONF_FILE: &str = "settings.toml"; -const CENTER: (f32, f32) = (2000.0, 2000.0); pub fn main() { match run_all() { @@ -43,6 +42,8 @@ fn run_all() -> LJResult<()> { let config = config?; info!("*** Starting up ***"); + info!("{:?}", config); + // Setup Redis Service let mut rs = RedisCtrl::new(&config.redis_url)?; @@ -60,11 +61,7 @@ fn run_all() -> LJResult<()> { //dbg!(tracer); // Setup geometry transformers on points lists - // @todo use the config - let transformers: Vec> = vec![ - Box::new(Translate::new(CENTER.0, CENTER.1)), - Box::new(Replicate::Until(48)), - ]; + let transformers = config.get_transformers(); // Dispatch based on redis requests while running.load(Ordering::SeqCst) { diff --git a/src/transformer/replicate.rs b/src/transformer/replicate.rs index bb56fec..b8a1d71 100644 --- a/src/transformer/replicate.rs +++ b/src/transformer/replicate.rs @@ -1,10 +1,11 @@ use crate::Transformers; use crate::point::Point; +use serde::{Serialize,Deserialize}; /// Replicate #[allow(dead_code)] -#[derive(Debug,Clone,Copy)] +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] pub enum Replicate { Until(usize), Times(usize) diff --git a/src/transformer/translate.rs b/src/transformer/translate.rs index 6b0c029..2e94e2e 100644 --- a/src/transformer/translate.rs +++ b/src/transformer/translate.rs @@ -1,8 +1,10 @@ use crate::Transformers; use crate::point::Point; +use serde::{Serialize,Deserialize}; + /// Translate -#[derive(Debug,Clone,Copy)] +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] pub struct Translate { x: f32, y: f32