From bffcf75eee5a0ab9c8bd1a4b109410e2309e0c7c Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Sat, 25 Jan 2025 13:29:28 +0100 Subject: [PATCH] fix double double client bug --- mqttClock.yml | 1 + src/main.rs | 37 ++++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/mqttClock.yml b/mqttClock.yml index 1a42470..c530e6b 100644 --- a/mqttClock.yml +++ b/mqttClock.yml @@ -1,5 +1,6 @@ mqtt: host: "localhost" port: 1883 + client: "mqttAutomation01" user: "mqttAutomation" pass: "password" diff --git a/src/main.rs b/src/main.rs index 3780da5..b7a8756 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,7 @@ fn mqtt_automation(publish: &Sender, message: MqttMessage) { struct SettingsMQTT { host: String, port: u16, + client: String, user: String, pass: String } @@ -184,18 +185,27 @@ fn main() { }, Err(_) => { conf = Settings { - mqtt: SettingsMQTT {host:String::new(),port:0,user:String::new(),pass:String::new()} + mqtt: SettingsMQTT {host:String::new(),port:0,client:String::new(),user:String::new(),pass:String::new()} }; conf_ok = false; } } if conf_ok { - let mut mqttoptions = MqttOptions::new("rumqtt-sync", conf.mqtt.host, conf.mqtt.port); + let mut mqttoptions = MqttOptions::new(conf.mqtt.client, conf.mqtt.host, conf.mqtt.port); mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_credentials(conf.mqtt.user, conf.mqtt.pass); let (client, mut connection) = Client::new(mqttoptions, 10); + match client.subscribe("clock/hour", QoS::AtMostOnce) { + Err(e) => println!("ERROR: main: faild to subscribe to clock/hour ({})", e), + Ok(_) => {} + } + match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) { + Err(e) => println!("ERROR: main: faild to subscribe to .../KNMITemp/values ({})", e), + Ok(_) => {} + } + // thread publisher let publisher = thread::Builder::new() .name("publisher".to_string()) @@ -222,7 +232,6 @@ fn main() { for (_i, notification) in connection.iter().enumerate() { let mut delay: bool = false; match notification { - // Err(e) => println!("ERROR: mqtt: {}", e), Err(e) => match e { rumqttc::ConnectionError::MqttState(state) => match state { rumqttc::StateError::Io(e) => { @@ -270,17 +279,19 @@ fn main() { Packet::Connect(_) => println!("INFO : mqtt: connected"), Packet::ConnAck(_) => println!("INFO : mqtt: connaction acknolaged"), Packet::Publish(msg) => { - let payload = match String::from_utf8(msg.payload.to_vec()) { - Err(e) => panic!("ERROR: pqtt_recive: faild to decode payload ({})", e), - Ok(v) => v + let mut payload: String = String::from(""); + match String::from_utf8(msg.payload.to_vec()) { + Err(e) => println!("ERROR: pqtt_recive: faild to decode payload ({})", e), + Ok(v) => payload = v }; - - let message: MqttMessage = { MqttMessage { - topic: msg.topic, - payload: payload - }}; - - mqtt_automation(&mqtt_publish, message); + if payload.len() > 0 { + let message: MqttMessage = { MqttMessage { + topic: msg.topic, + payload: payload + }}; + + mqtt_automation(&mqtt_publish, message); + } }, Packet::PubAck(_) => {}, //println!("INFO : mqtt_recive: in PubAck"), Packet::PubRec(_) => {}, //println!("INFO : mqtt_recive: in PubRec"),