lazy_static does not help for MQTT, qwen2.5-coder did not help this time :-(
This commit is contained in:
parent
2fcf37bfdc
commit
5c2f2f60ad
@ -47,6 +47,37 @@ const IMAGE_HEIGHT_BYTE: u32 = 40;
|
|||||||
const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize;
|
const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize;
|
||||||
const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) 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<paho_mqtt::AsyncClient>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<paho_mqtt::AsyncClient>) {
|
||||||
|
self.mqtt_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Getter for mqtt_client
|
||||||
|
pub fn get_mqtt_client(&self) -> &Option<paho_mqtt::AsyncClient> {
|
||||||
|
&self.mqtt_client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref MQTTCLIENT: Mutex<MqttClient> = Mutex::new(MqttClient::new());
|
||||||
|
}
|
||||||
|
|
||||||
struct UdpDisplay {
|
struct UdpDisplay {
|
||||||
image: [u8; IMAGE_SIZE_BYTE],
|
image: [u8; IMAGE_SIZE_BYTE],
|
||||||
@ -423,17 +454,17 @@ lazy_static! {
|
|||||||
/// @author Gwen2.5
|
/// @author Gwen2.5
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `mac` - Paho async client
|
|
||||||
/// * `topic` - The MQTT topic to which the message will be published.
|
/// * `topic` - The MQTT topic to which the message will be published.
|
||||||
/// * `message` - The message to be published.
|
/// * `message` - The message to be published.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// True if the message was successfully published, false otherwise.
|
/// 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 msg = MqttMessage::new(topic, message, 0);
|
||||||
|
|
||||||
|
let mqtt_client = <std::option::Option<AsyncClient> as Clone>::clone(&(MQTTCLIENT.lock().unwrap().get_mqtt_client())).unwrap();
|
||||||
// Publish the message and ensure it completes without error
|
// 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) => {
|
Ok(_token) => {
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
@ -442,7 +473,6 @@ async fn publish_message(ref mac: AsyncClient, topic: &str, message: &str) -> bo
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
||||||
@ -486,7 +516,7 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut mqtt_client: Option<paho_mqtt::AsyncClient> = None;
|
let mut gmc = MQTTCLIENT.lock().unwrap();
|
||||||
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
|
let mut mqtt_message: Arc<Mutex<Message>> = Arc::new(Mutex::new(Message{ string: None }));
|
||||||
|
|
||||||
if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0)
|
if GlobalConfiguration.lock().is_ok() && (GlobalConfiguration.lock().unwrap().mqttIPAddress.len() > 0)
|
||||||
@ -551,7 +581,7 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
local_mqtt.connect_with_callbacks(conn_opts, mqtt_on_connect_success, mqtt_on_connect_failure);
|
local_mqtt.connect_with_callbacks(conn_opts, mqtt_on_connect_success, mqtt_on_connect_failure);
|
||||||
|
|
||||||
// move local instance to global scope
|
// move local instance to global scope
|
||||||
mqtt_client = Some(local_mqtt);
|
gmc.set_mqtt_client(Some(local_mqtt));
|
||||||
}
|
}
|
||||||
|
|
||||||
let receiver = openweathermap::init_forecast("Mannheim",
|
let receiver = openweathermap::init_forecast("Mannheim",
|
||||||
@ -599,14 +629,8 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> ExitCode {
|
|||||||
|
|
||||||
if GlobalConfiguration.lock().is_ok() && GlobalConfiguration.lock().unwrap().mqttPrefix.len() > 0
|
if GlobalConfiguration.lock().is_ok() && GlobalConfiguration.lock().unwrap().mqttPrefix.len() > 0
|
||||||
{
|
{
|
||||||
if mqtt_client.is_some()
|
//FIXME if mqtt_client.is_some()
|
||||||
{
|
fun_publishinfoviamqtt(&straba_res);
|
||||||
fun_publishinfoviamqtt(mqtt_client.clone().unwrap(), &straba_res);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
println!("Update MQTT not possible");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// request once a minute new data
|
// request once a minute new data
|
||||||
@ -632,15 +656,12 @@ fn main_function(parameter1: String, parameter2: Option<String>) -> 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 topic_in_station: String = format!("{}{}", GlobalConfiguration.lock().unwrap().mqttPrefix, "/inbound/station");
|
||||||
let station_name: String = format!("{}", straba_res.inbound_station);
|
let station_name: String = format!("{}", straba_res.inbound_station);
|
||||||
if mqtt_client.is_connected() {
|
//FIXME if mqtt_client.is_connected() {
|
||||||
publish_message(mqtt_client, topic_in_station.as_str(), station_name.as_str());
|
publish_message(topic_in_station.as_str(), station_name.as_str());
|
||||||
println!("MQTT published {:?} = {:?}s", topic_in_station, straba_res.outbound_station);
|
println!("MQTT published {:?} = {:?}s", topic_in_station, straba_res.outbound_station);
|
||||||
} else {
|
|
||||||
println!("MQTT not ready... {:?} = {:?}s", topic_in_station, straba_res.outbound_station);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user