Refactored configuration into new module

This commit is contained in:
Ollo
2025-04-23 20:30:12 +02:00
parent d4726a2b9a
commit cef76ad9aa
2 changed files with 90 additions and 70 deletions

82
client/bin/src/config.rs Normal file
View 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;
}