Refactored configuration into new module
This commit is contained in:
		
							
								
								
									
										82
									
								
								client/bin/src/config.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								client/bin/src/config.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
			
		||||
}
 | 
			
		||||
@@ -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<Vec<String>>;
 | 
			
		||||
 | 
			
		||||
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<Config> = Mutex::new({
 | 
			
		||||
        Config::new("", "", "test/ledboard", DEFAULT_REFRESH_INTERVAL)
 | 
			
		||||
        Config::newDefault()
 | 
			
		||||
     });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -519,13 +462,8 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> 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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user