From 5c2f2f60ad5ffae7223988507785c8219d1c09a8 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 23 Apr 2025 21:16:13 +0200 Subject: [PATCH] lazy_static does not help for MQTT, qwen2.5-coder did not help this time :-( --- client/bin/src/main.rs | 63 ++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 6f2ffd4..321fb59 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -47,6 +47,37 @@ const IMAGE_HEIGHT_BYTE: u32 = 40; const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize; const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) as usize; +/// @struct MqttClient +/// @brief A struct to hold application configuration settings. +/// +/// The `MqttClient` struct contains various configuration parameters that control the behavior of the application. +pub struct MqttClient { + /// MQTT client option + pub mqtt_client: Option, +} + +impl MqttClient { + /// Creates a new instance of MqttClient with mqtt_client initialized to None. + pub fn new() -> Self { + MqttClient { + mqtt_client: None, + } + } + + /// Setter for mqtt_client + pub fn set_mqtt_client(&mut self, client: Option) { + self.mqtt_client = client; + } + + /// Getter for mqtt_client + pub fn get_mqtt_client(&self) -> &Option { + &self.mqtt_client + } +} + +lazy_static! { + static ref MQTTCLIENT: Mutex = Mutex::new(MqttClient::new()); +} struct UdpDisplay { image: [u8; IMAGE_SIZE_BYTE], @@ -423,17 +454,17 @@ lazy_static! { /// @author Gwen2.5 /// /// # Arguments -/// * `mac` - Paho async client /// * `topic` - The MQTT topic to which the message will be published. /// * `message` - The message to be published. /// /// # Returns /// True if the message was successfully published, false otherwise. -async fn publish_message(ref mac: AsyncClient, topic: &str, message: &str) -> bool { +async fn publish_message(topic: &str, message: &str) -> bool { let msg = MqttMessage::new(topic, message, 0); + let mqtt_client = as Clone>::clone(&(MQTTCLIENT.lock().unwrap().get_mqtt_client())).unwrap(); // Publish the message and ensure it completes without error - let token = match mac.publish(msg).await { + let token = match mqtt_client.publish(msg).await { Ok(_token) => { return true; }, @@ -442,7 +473,6 @@ async fn publish_message(ref mac: AsyncClient, topic: &str, message: &str) -> bo return false; } }; - } fn main_function(parameter1: String, parameter2: Option) -> ExitCode { @@ -486,7 +516,7 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } } - let mut mqtt_client: Option = None; + let mut gmc = MQTTCLIENT.lock().unwrap(); let mut mqtt_message: Arc> = Arc::new(Mutex::new(Message{ string: None })); if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0) @@ -551,7 +581,7 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { local_mqtt.connect_with_callbacks(conn_opts, mqtt_on_connect_success, mqtt_on_connect_failure); // move local instance to global scope - mqtt_client = Some(local_mqtt); + gmc.set_mqtt_client(Some(local_mqtt)); } let receiver = openweathermap::init_forecast("Mannheim", @@ -599,14 +629,8 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { if GlobalConfiguration.lock().is_ok() && GlobalConfiguration.lock().unwrap().mqttPrefix.len() > 0 { - if mqtt_client.is_some() - { - fun_publishinfoviamqtt(mqtt_client.clone().unwrap(), &straba_res); - } - else - { - println!("Update MQTT not possible"); - } + //FIXME if mqtt_client.is_some() + fun_publishinfoviamqtt(&straba_res); } // request once a minute new data @@ -632,15 +656,12 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } } -fn fun_publishinfoviamqtt(mqtt_client: AsyncClient, straba_res: &NextDeparture) { +fn fun_publishinfoviamqtt(straba_res: &NextDeparture) { 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()); - println!("MQTT published {:?} = {:?}s", topic_in_station, straba_res.outbound_station); - } else { - println!("MQTT not ready... {:?} = {:?}s", topic_in_station, straba_res.outbound_station); - } + //FIXME if mqtt_client.is_connected() { + publish_message(topic_in_station.as_str(), station_name.as_str()); + println!("MQTT published {:?} = {:?}s", topic_in_station, straba_res.outbound_station); } fn main() -> ExitCode {