diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 2c4dd7d..4f78323 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -1,5 +1,5 @@ use std::{time::Duration, fmt::format}; -use std::sync::{RwLock, Mutex, Arc}; +use std::sync::{RwLock, Arc}; use async_trait::async_trait; use paho_mqtt::{AsyncClient,Message as MqttMessage}; use str; @@ -30,6 +30,8 @@ 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; const IMAGE_SIZE_BYTE: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; /* one byte contains 8 LEDs, one in each bit */ @@ -303,13 +305,25 @@ const DEFAULT_REFRESH_INTERVAL: u32 = 50; // Define a struct to hold the INI configuration #[derive(Debug)] -struct Config { +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 { @@ -454,12 +468,9 @@ struct Message { // Lazy static to load the config file content lazy_static! { - static ref GlobalConfiguration: Config = Config { - mqttIPAddress : "".to_string(), - panelIPAddress : "".to_string(), - mqttPrefix : "test/ledboard".to_string(), - refreshInterval: DEFAULT_REFRESH_INTERVAL - }; + pub static ref GlobalConfiguration: Mutex = Mutex::new({ + Config::new("", "", "test/ledboard", DEFAULT_REFRESH_INTERVAL) + }); } @@ -502,8 +513,9 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { let c:Config = read_ini_file(configOrMqttAddress); //FIMXE update configuration - //GlobalConfiguration.panelIPAddress = c.panelIPAddress; - //println!("Global: {:} ", &GlobalConfiguration.panelIPAddress); + let mut gc = GlobalConfiguration.lock().unwrap(); + println!("Global: {:?} ", &c.panelIPAddress); + gc.panelIPAddress = c.panelIPAddress; } else { @@ -522,29 +534,30 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } let mut device_online = false; - if GlobalConfiguration.panelIPAddress.len() > 0 + if GlobalConfiguration.lock().unwrap().panelIPAddress.len() > 0 { - device_online = check_connection(GlobalConfiguration.panelIPAddress.clone()); + device_online = check_connection(GlobalConfiguration.lock().unwrap().panelIPAddress.clone()); if !device_online { - println!("{:} not online", &GlobalConfiguration.panelIPAddress); + println!("{:} not online", &GlobalConfiguration.lock().unwrap().panelIPAddress); return ExitCode::FAILURE; } } let mut mqtt_client: Option = None; let mut mqtt_message: Arc> = Arc::new(Mutex::new(Message{ string: None })); - if GlobalConfiguration.mqttIPAddress.len() > 0 + + if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().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(GlobalConfiguration.mqttIPAddress.clone()) + .server_uri(GlobalConfiguration.lock().unwrap().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...", GlobalConfiguration.mqttIPAddress); + println!("MQTT | Connecting to {:} MQTT server...", GlobalConfiguration.lock().unwrap().mqttIPAddress); // Define the set of options for the connection. let conn_opts = paho_mqtt::ConnectOptionsBuilder::new() @@ -582,7 +595,7 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { // Define the set of options for the connection - let lwt = paho_mqtt::Message::new(&format!("{}/lwt", GlobalConfiguration.mqttPrefix), "lost connection", 1); + let lwt = paho_mqtt::Message::new(&format!("{}/lwt", GlobalConfiguration.lock().unwrap().mqttPrefix), "lost connection", 1); // The connect options. Defaults to an MQTT v3.x connection. let conn_opts = paho_mqtt::ConnectOptionsBuilder::new() @@ -611,10 +624,10 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { println!("{:?} {:?}s", straba_res.outbound_station, straba_res.outbound_diff); println!("{:?} {:?}s", straba_res.inbound_station , straba_res.inbound_diff); - if GlobalConfiguration.panelIPAddress.len() > 0 + if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().panelIPAddress.len() > 0) { // Render start - send_package(GlobalConfiguration.panelIPAddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string())); + send_package(GlobalConfiguration.lock().unwrap().panelIPAddress.clone(), &last_data, &straba_res, Some("MQTT: room/ledboard".to_string())); } loop { @@ -635,13 +648,13 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } } - if (straba_res.request_time + (GlobalConfiguration.refreshInterval as i64)) < seconds as i64 { - if GlobalConfiguration.mqttIPAddress.len() > 0 + if (straba_res.request_time + (GlobalConfiguration.lock().unwrap().refreshInterval as i64)) < seconds as i64 { + if GlobalConfiguration.lock().is_ok() && GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0 { - device_online = check_connection(GlobalConfiguration.mqttIPAddress.clone()); + device_online = check_connection(GlobalConfiguration.lock().unwrap().mqttIPAddress.clone()); } - if GlobalConfiguration.mqttPrefix.len() > 0 + if GlobalConfiguration.lock().is_ok() && GlobalConfiguration.lock().unwrap().mqttPrefix.len() > 0 { if mqtt_client.is_some() { @@ -667,9 +680,9 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { mqtt_message = None; } - if (GlobalConfiguration.mqttIPAddress.len() > 0) && (device_online == true) { + if (GlobalConfiguration.lock().is_ok()) && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0) && (device_online == true) { // Render new image - send_package(GlobalConfiguration.mqttIPAddress.clone(), &last_data, &straba_res, mqtt_message); + send_package(GlobalConfiguration.lock().unwrap().mqttIPAddress.clone(), &last_data, &straba_res, mqtt_message); } // Handle MQTT messages @@ -677,7 +690,7 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } fn fun_publishinfoviamqtt(mqtt_client: AsyncClient, straba_res: &NextDeparture) { - let topic_in_station: String = format!("{}{}", GlobalConfiguration.mqttPrefix, "/inbound/station"); + let topic_in_station: String = format!("{}{}", GlobalConfiguration.lock().unwrap().mqttPrefix, "/inbound/station"); let station_name: String = format!("{}", straba_res.inbound_station); if mqtt_client.is_connected() { publish_message(mqtt_client, topic_in_station.as_str(), station_name.as_str());