MQTT message is received

This commit is contained in:
Ollo 2025-04-25 21:37:56 +02:00
parent ccbb23ebc7
commit 183764f2bb

View File

@ -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<Vec<String>>;
/////////////////////////////////////////////////////////////////////////////
// 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<Result<Forecast, String>>,
straba_res: &NextDeparture,
@ -515,7 +488,7 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
}
let mut gmc = MQTTCLIENT.lock().unwrap();
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
let mqtt_message: Arc<Mutex<Message>> = 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<String>) -> 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<String>) -> 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));
}