From 183764f2bb4905aacb5c23f9ca3adaa3019e904e Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 25 Apr 2025 21:37:56 +0200 Subject: [PATCH] MQTT message is received --- client/bin/src/main.rs | 72 ++++++++++++------------------------------ 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 23fc7ec..4f7544e 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -329,35 +329,8 @@ fn render_strab(display: &mut UdpDisplay, straba_res: &NextDeparture) { render_strab_partial(display, &straba_res.outbound_station, straba_res.outbound_diff, 17); render_strab_partial(display, &straba_res.inbound_station, straba_res.inbound_diff, 27); } - -// The type we'll use to keep our dynamic list of topics inside the -// MQTT client. Since we want to update it after creating the client, -// we need to wrap the data in a lock, like a Mutex or RwLock. -type UserTopics = RwLock>; - ///////////////////////////////////////////////////////////////////////////// -// 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::Client, _msgid: u16) { - println!("MQTT | Connection succeeded"); - - // Subscribe to the desired topic(s). - cli.subscribe("room/ledboard", paho_mqtt::QOS_0); -} - -// Callback for a failed attempt to connect to the server. -// We simply sleep and then try again. -// -// Note that normally we don't want to do a blocking operation or sleep -// from within a callback. But in this case, we know that the client is -// *not* conected, and thus not doing anything important. So we don't worry -// too much about stopping its callback thread. -fn mqtt_on_connect_failure(cli: &paho_mqtt::Client, _msgid: u16, rc: i32) { - println!("MQTT | Connection attempt failed with error code {}.\n", rc); - //FIXME exit -} - fn send_package(ipaddress: String, data: &Option>, straba_res: &NextDeparture, @@ -515,7 +488,7 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { } let mut gmc = MQTTCLIENT.lock().unwrap(); - let mut mqtt_message: Arc> = Arc::new(Mutex::new(Message{ string: None })); + let mqtt_message: Arc> = Arc::new(Mutex::new(Message{ string: None })); if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0) { @@ -530,12 +503,30 @@ fn main_function(parameter1: String, parameter2: Option) -> ExitCode { println!("MQTT | Connecting to {:} MQTT server...", GlobalConfiguration.lock().unwrap().mqttIPAddress); - // Define the set of options for the connection. + 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() .keep_alive_interval(Duration::from_secs(20)) - .clean_session(true) + .will_message(lwt) .finalize(); + // Make the connection to the broker + println!("MQTT | Connecting to the MQTT server..."); + let result = local_mqtt.connect(conn_opts); + + match result { + Ok(_) => { + println!("MQTT | Server connected"); + }, + Err(error) => { + println!("MQTT | Server connecting {error:?}"); + }, + } + + // Subscribe to the desired topic(s). + local_mqtt.subscribe("room/ledboard", paho_mqtt::QOS_0); + // Starts the client receiving messages let rx_queue = local_mqtt.start_consuming(); @@ -562,27 +553,6 @@ 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.lock().unwrap().mqttPrefix), "lost connection", 1); - - // The connect options. Defaults to an MQTT v3.x connection. - let conn_opts = paho_mqtt::ConnectOptionsBuilder::new() - .keep_alive_interval(Duration::from_secs(20)) - .will_message(lwt) - .finalize(); - - // Make the connection to the broker - println!("MQTT | Connecting to the MQTT server..."); - let result = local_mqtt.connect(conn_opts); - - match result { - Ok(_) => { - println!("MQTT | Server connected"); - }, - Err(error) => { - println!("MQTT | Server connecting {error:?}"); - }, - } - // move local instance to global scope gmc.set_mqtt_client(Some(local_mqtt)); }