load transformers from config file

This commit is contained in:
Marc Planard 2023-06-07 17:26:30 +02:00
parent 80ec78efd3
commit ad3b3803ce
7 changed files with 60 additions and 11 deletions

View File

@ -14,3 +14,4 @@ log = "0.4.18"
redis = "0.23.0" redis = "0.23.0"
ron = "0.8.0" ron = "0.8.0"
serde = { version = "1.0.163", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
toml = "0.7.4"

View File

@ -18,3 +18,11 @@ id = 0
# For Etherdream. IP of the DAC # For Etherdream. IP of the DAC
# [dac.etherdream] # [dac.etherdream]
# url = "192.168.1.68" # url = "192.168.1.68"
[[transformers]]
[transformers.translate]
x = 2000
y = 2000
[[transformers]]
[transformers.replicate]
Until = 48

View File

@ -1,13 +1,16 @@
use config::Config; use config::Config;
use serde::{Serialize,Deserialize}; use serde::{Serialize,Deserialize};
use crate::errors::LJResult; use crate::errors::LJResult;
use crate::transformer;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Conf { pub struct Conf {
pub laser_id: u8, pub laser_id: u8,
pub debug: bool, pub debug: bool,
pub redis_url: String, pub redis_url: String,
pub dac: DacFamily pub dac: DacFamily,
#[serde(default)]
pub transformers: Vec<TransformConf>
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
@ -28,6 +31,15 @@ pub struct EtherDreamConf {
pub url: String 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 { impl Conf {
pub fn new(path: &str) -> LJResult<Conf> { pub fn new(path: &str) -> LJResult<Conf> {
let settings = Config::builder() let settings = Config::builder()
@ -37,4 +49,30 @@ impl Conf {
let conf : Conf = settings.try_deserialize()?; let conf : Conf = settings.try_deserialize()?;
Ok(conf) Ok(conf)
} }
pub fn get_transformers(&self) -> Vec<Box<dyn transformer::Transformers>> {
let mut v = vec![];
for t in &self.transformers {
let t : Box<dyn transformer::Transformers> = 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);
}
} }

View File

@ -1,5 +1,7 @@
pub mod conf; /*
pub mod redis_ctrl; pub mod redis_ctrl;
pub mod conf;
pub mod errors; pub mod errors;
pub mod device; pub mod device;
pub mod point; pub mod point;
*/

View File

@ -14,14 +14,13 @@ 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::Conf; use conf::Conf;
use errors::{LJError, LJResult}; use errors::LJResult;
use point::Point; use point::Point;
use transformer::{Transformers, Translate, Replicate}; use transformer::{Transformers, Translate, Replicate};
use log::{LevelFilter, info, /* warn, */ error}; use log::{LevelFilter, info, /* warn, */ error};
use env_logger::Builder; use env_logger::Builder;
const DEFAULT_CONF_FILE: &str = "settings.toml"; const DEFAULT_CONF_FILE: &str = "settings.toml";
const CENTER: (f32, f32) = (2000.0, 2000.0);
pub fn main() { pub fn main() {
match run_all() { match run_all() {
@ -43,6 +42,8 @@ fn run_all() -> LJResult<()> {
let config = config?; let config = config?;
info!("*** Starting up ***"); info!("*** Starting up ***");
info!("{:?}", config);
// Setup Redis Service // Setup Redis Service
let mut rs = RedisCtrl::new(&config.redis_url)?; let mut rs = RedisCtrl::new(&config.redis_url)?;
@ -60,11 +61,7 @@ fn run_all() -> LJResult<()> {
//dbg!(tracer); //dbg!(tracer);
// Setup geometry transformers on points lists // Setup geometry transformers on points lists
// @todo use the config let transformers = config.get_transformers();
let transformers: Vec<Box<dyn Transformers>> = vec![
Box::new(Translate::new(CENTER.0, CENTER.1)),
Box::new(Replicate::Until(48)),
];
// Dispatch based on redis requests // Dispatch based on redis requests
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {

View File

@ -1,10 +1,11 @@
use crate::Transformers; use crate::Transformers;
use crate::point::Point; use crate::point::Point;
use serde::{Serialize,Deserialize};
/// Replicate /// Replicate
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug,Clone,Copy)] #[derive(Serialize,Deserialize,Debug,Clone,Copy)]
pub enum Replicate { pub enum Replicate {
Until(usize), Until(usize),
Times(usize) Times(usize)

View File

@ -1,8 +1,10 @@
use crate::Transformers; use crate::Transformers;
use crate::point::Point; use crate::point::Point;
use serde::{Serialize,Deserialize};
/// Translate /// Translate
#[derive(Debug,Clone,Copy)] #[derive(Serialize,Deserialize,Debug,Clone,Copy)]
pub struct Translate { pub struct Translate {
x: f32, x: f32,
y: f32 y: f32