diff --git a/Cargo.lock b/Cargo.lock index a7da4ee..31386cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,8 +280,8 @@ dependencies = [ [[package]] name = "mqtt-client" -version = "1.0.0" -source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v1.0.0#f0b34311457b26a0a3b3a7027845b407a3ca47ee" +version = "2.0.1" +source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v2.0.1#e7a46b82a51556ef177c0bf1cb914c4385aefcb9" dependencies = [ "crossbeam", "rumqttc", diff --git a/Cargo.toml b/Cargo.toml index a0d986e..ce43733 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ json = "0.12.4" rumqttc = "0.24.0" serde = { version = "1.0.217", features = ["derive"] } serde_yaml = "0.9.34" -mqtt-client = { tag = "v1.0.0", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } +mqtt-client = { tag = "v2.0.1", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } diff --git a/src/main.rs b/src/main.rs index cbee677..c71e701 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::fs; -use crossbeam::channel::Sender; +use mqtt_client::{MqttMessage, Sender, Receiver}; use serde::Deserialize; use mqtt_client::mqtt_client; @@ -107,7 +107,9 @@ impl Automation { } self.tx({ mqtt_client::MqttMessage { topic: String::from("/cool/devices/lamp-01/set"), - payload: payload + payload: payload, + retain: false, + qos: mqtt_client::QoS::AtMostOnce, }}); } } @@ -133,33 +135,40 @@ impl mqtt_client::MqttTool for Automation { } } - fn rx(&mut self, message: mqtt_client::MqttMessage) { - println!("INFO : mqtt_automation: {}: {}", message.topic, message.payload); - if message.topic.eq("clock/time/hour") { - - if message.payload.eq("7") && (self.clock_dow >= 1 && self.clock_dow <= 7) { - self.lamp01_set(true); - } - - } else if message.topic.eq("/cool/devices/KNMITemp/values") { - - match json_parser::get_u32(json_parser::Json::Text(message.payload), Vec::from([String::from("gr")])) { - Ok(gr) => { - if gr > 30 { - self.lamp01_set(false); + fn run(&mut self, rx: Receiver) { + + let message = rx.recv(); + match message { + Err(e) => println!("ERROR: publisher: failed to receve an message ({})", e), + Ok(message) => { + println!("INFO : mqtt_automation: {}: {}", message.topic, message.payload); + if message.topic.eq("clock/time/hour") { + + if message.payload.eq("7") && (self.clock_dow < 5) { + self.lamp01_set(true); } - }, - Err(e) => print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string()) - } - - } - else if message.topic.eq("clock/date/dow") { - match message.payload.parse::() { - Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e), - Ok(n) => self.clock_dow = n - } + } else if message.topic.eq("/cool/devices/KNMITemp/values") { + match json_parser::get_u32(json_parser::Json::Text(message.payload), Vec::from([String::from("gr")])) { + Ok(gr) => { + if gr > 30 { + self.lamp01_set(false); + } + }, + Err(e) => print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string()) + } + + } + else if message.topic.eq("clock/date/dow") { + + match message.payload.parse::() { + Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e), + Ok(n) => self.clock_dow = n + } + + } + } } } }