Moved all stuff into thread
This commit is contained in:
@@ -516,6 +516,9 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
|
|
||||||
if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0)
|
if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0)
|
||||||
{
|
{
|
||||||
|
// Create a thread that stays pending over incoming messages.
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
|
|
||||||
// Define the set of options for the create.
|
// Define the set of options for the create.
|
||||||
// Use an ID for a persistent session.
|
// Use an ID for a persistent session.
|
||||||
let create_opts = paho_mqtt::CreateOptionsBuilder::new()
|
let create_opts = paho_mqtt::CreateOptionsBuilder::new()
|
||||||
@@ -548,18 +551,27 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe to the desired topic(s).
|
let thread_mqtt = local_mqtt.clone();
|
||||||
local_mqtt.subscribe("room/ledboard", paho_mqtt::QOS_0);
|
// move local instance to global scope
|
||||||
|
gmc.set_mqtt_client(Some(local_mqtt));
|
||||||
// Starts the client receiving messages
|
|
||||||
let rx_queue = local_mqtt.start_consuming();
|
|
||||||
|
|
||||||
// Attach a closure to the client to receive callback
|
// Attach a closure to the client to receive callback
|
||||||
// on incoming messages.
|
// on incoming messages.
|
||||||
let mqtt_message_for_callback = mqtt_message.clone();
|
let mqtt_message_for_callback = mqtt_message.clone();
|
||||||
|
|
||||||
// Create a thread that stays pending over incoming messages.
|
// Subscribe to the desired topic(s).
|
||||||
let handle = thread::spawn(move || {
|
let resultSubscribe = thread_mqtt.subscribe("room/ledboard", paho_mqtt::QOS_0);
|
||||||
|
match resultSubscribe {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("MQTT | Subscribe ");
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
println!("MQTT | Subscribe failed {error:?}");
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Starts the client receiving messages
|
||||||
|
let rx_queue = thread_mqtt.start_consuming();
|
||||||
for mqttmsg in rx_queue.iter() {
|
for mqttmsg in rx_queue.iter() {
|
||||||
if let Some(mqttmsg) = mqttmsg {
|
if let Some(mqttmsg) = mqttmsg {
|
||||||
let topic = mqttmsg.topic();
|
let topic = mqttmsg.topic();
|
||||||
@@ -568,17 +580,13 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
//println!("MQTT | {} - {}", topic, payload_str);
|
//println!("MQTT | {} - {}", topic, payload_str);
|
||||||
let mut lock = mqtt_message_for_callback.lock().unwrap();
|
let mut lock = mqtt_message_for_callback.lock().unwrap();
|
||||||
lock.string = Some(payload_str.to_string());
|
lock.string = Some(payload_str.to_string());
|
||||||
println!("Received: -> {}", mqttmsg.payload_str());
|
println!("MQTT | Received: -> {}", mqttmsg.payload_str());
|
||||||
} else {
|
} else {
|
||||||
println!("Unsubscribe: connection closed");
|
println!("MQTT | Unsubscribe: connection closed");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Define the set of options for the connection
|
// Define the set of options for the connection
|
||||||
|
|
||||||
// move local instance to global scope
|
|
||||||
gmc.set_mqtt_client(Some(local_mqtt));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let receiver = openweathermap::init_forecast("Mannheim",
|
let receiver = openweathermap::init_forecast("Mannheim",
|
||||||
|
|||||||
Reference in New Issue
Block a user