From 922c023216838931436fdc51373e3bb0681bd3ab Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 6 Apr 2025 17:13:47 +0200 Subject: [PATCH] Refactored function: always use only two parameter --- client/bin/src/main.rs | 96 +++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 6ca115f..d3d99cc 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -26,6 +26,7 @@ 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; @@ -303,6 +304,7 @@ type UserTopics = RwLock>; struct Config { mqttPrefix: String, mqttIPAddress: String, + panelIPAddress: String, } @@ -310,7 +312,8 @@ struct Config { fn read_ini_file(filename: String) -> Result { let mut config = Config { mqttPrefix: "room/ledboard".to_string(), - mqttIPAddress: String::new() }; + mqttIPAddress: String::new(), + panelIPAddress: String::new() }; let i = Ini::load_from_file(filename).unwrap(); for (sec, prop) in i.iter() { @@ -328,6 +331,13 @@ fn read_ini_file(filename: String) -> Result { config.mqttIPAddress = v.trim().to_string(); } } + else if (sec.is_some()) && (sec.unwrap() == "panel") + { + if k == "ip" + { + config.panelIPAddress = v.trim().to_string(); + } + } } } Ok(config) @@ -402,7 +412,7 @@ LEDboardClient " println!(""); println!(""); println!("Config mode"); - println!("--mode config "); + println!("--config "); } fn check_connection(ipaddress: String) -> bool { @@ -439,37 +449,61 @@ struct Message { // Lazy static to load the config file content lazy_static! { - static ref GlobalConfiguration: Config = Config { mqttIPAddress : "".to_string(), mqttPrefix : "test/ledboard".to_string() }; + static ref GlobalConfiguration: Config = Config { + mqttIPAddress : "".to_string(), + panelIPAddress : "".to_string(), + mqttPrefix : "test/ledboard".to_string() }; } -fn main_function(ipaddress: String, mqtt: Option, inifile: Option) -> ExitCode { +fn main_function(parameter1: String, parameter2: Option) -> ExitCode { // Read configuration file - if (inifile.is_some()) && (mqtt.is_some()) && (ipaddress == "--mode") + if (parameter1 == "--config") && (parameter2.is_some()) { - read_ini_file(inifile.unwrap()).expect("Failed to load config file"); - println!("Config parsed"); + let configOrMqttAddress: String = parameter2.unwrap(); + if Path::new(&configOrMqttAddress).exists() + { + read_ini_file(configOrMqttAddress).expect("Failed to load config file"); + println!("Config parsed"); + } + else + { + /* Panel and MQTT Configured*/ + let mut config = Config { mqttPrefix: "room/ledboard".to_string(), + mqttIPAddress: configOrMqttAddress.clone(), + panelIPAddress: parameter1.clone() }; + //FIXME how is th global config updated ? + } + } + else + { + } - let mut device_online = check_connection(ipaddress.clone()); - if !device_online { - println!("{:} not online", &ipaddress); - return ExitCode::FAILURE; + let mut device_online = false; + if GlobalConfiguration.panelIPAddress.len() > 0 + { + device_online = check_connection(GlobalConfiguration.panelIPAddress.clone()); + if !device_online { + println!("{:} not online", &GlobalConfiguration.panelIPAddress); + return ExitCode::FAILURE; + } } + let mut mqtt_client: Option = None; let mut mqtt_message: Arc> = Arc::new(Mutex::new(Message{ string: None })); - if mqtt.is_some() { - let mqtt_ip: String = mqtt.clone().unwrap(); + if GlobalConfiguration.mqttIPAddress.len() > 0 + { // Define the set of options for the create. // Use an ID for a persistent session. let create_opts = paho_mqtt::CreateOptionsBuilder::new() - .server_uri(mqtt_ip.clone()) + .server_uri(GlobalConfiguration.mqttIPAddress.clone()) .client_id("ledboard") .finalize(); // Create a client. let local_mqtt = paho_mqtt::AsyncClient::new(create_opts).unwrap(); - println!("MQTT | Connecting to {:} MQTT server...", mqtt_ip); + println!("MQTT | Connecting to {:} MQTT server...", GlobalConfiguration.mqttIPAddress); // Define the set of options for the connection. let conn_opts = paho_mqtt::ConnectOptionsBuilder::new() @@ -536,8 +570,11 @@ fn main_function(ipaddress: String, mqtt: Option, inifile: Option 0 + { + // Render start + send_package(GlobalConfiguration.panelIPAddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string())); + } loop { let st_now = SystemTime::now(); @@ -558,7 +595,11 @@ fn main_function(ipaddress: String, mqtt: Option, inifile: Option 0 + { + device_online = check_connection(GlobalConfiguration.mqttIPAddress.clone()); + } + // request once a minute new data if device_online == true { straba_res = straba::fetch_data(None); @@ -575,9 +616,9 @@ fn main_function(ipaddress: String, mqtt: Option, inifile: Option 0) && (device_online == true) { // Render new image - send_package(ipaddress.clone(), &last_data, &straba_res, mqtt_message); + send_package(GlobalConfiguration.mqttIPAddress.clone(), &last_data, &straba_res, mqtt_message); } // Handle MQTT messages @@ -596,25 +637,14 @@ fn main() -> ExitCode { // one argument passed 2 => { let ip = &args[1]; - return main_function(ip.to_string(), None, None); + return main_function(ip.to_string(), None); } // two argument passed 3 => { let ip = &args[1]; let mqtt = &args[2]; return main_function( ip.to_string(), - Some(mqtt.to_string()), - None - ); - } - // three argument passed - 4 => { - let ip = &args[1]; - let mqtt = &args[2]; - let config = &args[3]; - return main_function( ip.to_string(), - Some(mqtt.to_string()), - Some(config.to_string()) + Some(mqtt.to_string()) ); } // all the other cases