From cef76ad9aaab8d8f98499d2f60feb65397be1594 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 23 Apr 2025 20:30:12 +0200 Subject: [PATCH] Refactored configuration into new module --- client/bin/src/config.rs | 82 ++++++++++++++++++++++++++++++++++++++++ client/bin/src/main.rs | 78 ++++---------------------------------- 2 files changed, 90 insertions(+), 70 deletions(-) create mode 100644 client/bin/src/config.rs diff --git a/client/bin/src/config.rs b/client/bin/src/config.rs new file mode 100644 index 0000000..4f0b6bf --- /dev/null +++ b/client/bin/src/config.rs @@ -0,0 +1,82 @@ +// config.rs + +/// @file +/// @brief Configuration settings for the application. +/// +/// This file defines the configuration settings that are used throughout the application. + +use lazy_static::lazy_static; +use std::sync::Mutex; + +use ini::Ini; +use std::path::Path; + +const DEFAULT_REFRESH_INTERVAL: u32 = 50; + + +// Define a struct to hold the INI configuration +#[derive(Debug)] +pub struct Config { + pub mqttPrefix: String, + pub mqttIPAddress: String, + pub panelIPAddress: String, + pub refreshInterval: u32 +} + +impl Config { + // Constructor for Config + pub fn new(mqtt_prefix: &str, mqtt_ip_address: &str, panel_ip_address: &str, interval: u32) -> Self { + Config { + mqttPrefix: mqtt_prefix.to_string(), + mqttIPAddress: mqtt_ip_address.to_string(), + panelIPAddress: panel_ip_address.to_string(), + refreshInterval: interval + } + } + + pub fn newDefault() -> Self { + Config { + mqttPrefix: "".to_string(), + mqttIPAddress: "".to_string(), + panelIPAddress: "".to_string(), + refreshInterval: DEFAULT_REFRESH_INTERVAL + } + } +} + +// Function to read the INI file +pub fn read_ini_file(filename: String) -> Config { + + let mut config = Config { mqttPrefix: "room/ledboard".to_string(), + mqttIPAddress: String::new(), + panelIPAddress: String::new(), + refreshInterval: DEFAULT_REFRESH_INTERVAL + }; + let i = Ini::load_from_file(filename).unwrap(); + for (sec, prop) in i.iter() { + + for (k, v) in prop.iter() + { + println!("{:?} {}:{}", sec, k, v); + if (sec.is_some()) && (sec.unwrap() == "mqtt") + { + if k == "path" + { + config.mqttPrefix = v.trim().to_string(); + } + else if k == "server" + { + config.mqttIPAddress = v.trim().to_string(); + } + } + else if (sec.is_some()) && (sec.unwrap() == "panel") + { + if k == "ip" + { + config.panelIPAddress = v.trim().to_string(); + } + } + } + } + return config; +} \ No newline at end of file diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 4f78323..8c31118 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -25,14 +25,18 @@ use std::io; use std::process::ExitCode; use openweathermap::forecast::Forecast; use straba::NextDeparture; -use ini::Ini; use std::path::Path; + // This declaration will look for a file named `straba.rs` and will // insert its contents inside a module named `straba` under this scope mod straba; use std::sync::Mutex; use lazy_static::lazy_static; +// Load INI-File handling module +mod config; +use config::Config; +use crate::config::read_ini_file; const IMAGE_SIZE_BYTE: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; /* one byte contains 8 LEDs, one in each bit */ const IMAGE_WIDTH: u32 = 5 * 32; @@ -299,69 +303,8 @@ fn render_strab(display: &mut UdpDisplay, straba_res: &NextDeparture) { // we need to wrap the data in a lock, like a Mutex or RwLock. type UserTopics = RwLock>; -const DEFAULT_REFRESH_INTERVAL: u32 = 50; - ///////////////////////////////////////////////////////////////////////////// -// Define a struct to hold the INI configuration -#[derive(Debug)] -pub struct Config { - mqttPrefix: String, - mqttIPAddress: String, - panelIPAddress: String, - refreshInterval: u32 -} - -impl Config { - // Constructor for Config - fn new(mqtt_prefix: &str, mqtt_ip_address: &str, panel_ip_address: &str, interval: u32) -> Self { - Config { - mqttPrefix: mqtt_prefix.to_string(), - mqttIPAddress: mqtt_ip_address.to_string(), - panelIPAddress: panel_ip_address.to_string(), - refreshInterval: interval - } - } -} - - -// Function to read the INI file -fn read_ini_file(filename: String) -> Config { - - let mut config = Config { mqttPrefix: "room/ledboard".to_string(), - mqttIPAddress: String::new(), - panelIPAddress: String::new(), - refreshInterval: DEFAULT_REFRESH_INTERVAL - }; - let i = Ini::load_from_file(filename).unwrap(); - for (sec, prop) in i.iter() { - - for (k, v) in prop.iter() - { - println!("{:?} {}:{}", sec, k, v); - if (sec.is_some()) && (sec.unwrap() == "mqtt") - { - if k == "path" - { - config.mqttPrefix = v.trim().to_string(); - } - else if k == "server" - { - config.mqttIPAddress = v.trim().to_string(); - } - } - else if (sec.is_some()) && (sec.unwrap() == "panel") - { - if k == "ip" - { - config.panelIPAddress = v.trim().to_string(); - } - } - } - } - return config; -} - // Callback for a successful connection to the broker. // We subscribe to the topic(s) we want here. fn mqtt_on_connect_success(cli: &paho_mqtt::AsyncClient, _msgid: u16) { @@ -469,7 +412,7 @@ struct Message { // Lazy static to load the config file content lazy_static! { pub static ref GlobalConfiguration: Mutex = Mutex::new({ - Config::new("", "", "test/ledboard", DEFAULT_REFRESH_INTERVAL) + Config::newDefault() }); } @@ -519,13 +462,8 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } else { - /* Panel and MQTT Configured*/ - let mut config = Config { mqttPrefix: "room/ledboard".to_string(), - mqttIPAddress: configOrMqttAddress.clone(), - panelIPAddress: parameter1.clone(), - refreshInterval: DEFAULT_REFRESH_INTERVAL - }; - //FIXME how is th global config updated ? + /* Panel and MQTT Configured*/ + } } else