fix double double client bug

This commit is contained in:
Laila van Reenen 2025-01-25 13:29:28 +01:00
parent 8ae77bbeb1
commit bffcf75eee
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
2 changed files with 25 additions and 13 deletions

View File

@ -1,5 +1,6 @@
mqtt:
host: "localhost"
port: 1883
client: "mqttAutomation01"
user: "mqttAutomation"
pass: "password"

View File

@ -125,6 +125,7 @@ fn mqtt_automation(publish: &Sender<MqttMessage>, 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"),