diff --git a/Cargo.lock b/Cargo.lock index 8c8d2c4..31e6f6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,8 +280,8 @@ dependencies = [ [[package]] name = "mqtt-client" -version = "2.0.1" -source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v2.0.1#e7a46b82a51556ef177c0bf1cb914c4385aefcb9" +version = "2.0.2" +source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v2.0.2#d2d31b2bc16b6d347f49a60a20290aa6880702ae" dependencies = [ "crossbeam", "rumqttc", @@ -289,7 +289,7 @@ dependencies = [ [[package]] name = "mqttAutomation" -version = "1.1.3" +version = "1.1.4" dependencies = [ "crossbeam", "json", diff --git a/Cargo.toml b/Cargo.toml index 0fe339a..7e4e105 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mqttAutomation" -version = "1.1.3" +version = "1.1.4" edition = "2021" [dependencies] @@ -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 = "v2.0.1", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } +mqtt-client = { tag = "v2.0.2", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } diff --git a/src/main.rs b/src/main.rs index ca7c43b..4008753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -use std::{fs, thread::{self, Thread}, time::Duration}; +use std::{fs, thread, time::Duration}; -use mqtt_client::{MqttMessage, Sender, Receiver}; +use mqtt_client::{MqttMessage, Sender, Receiver, QoS}; use serde::Deserialize; use mqtt_client::mqtt_client; @@ -136,40 +136,41 @@ impl mqtt_client::MqttTool for Automation { } fn run(&mut self, rx: Receiver) { + loop { + let message = rx.recv(); + match message { + Err(e) => { + println!("ERROR: mqttAutomation: failed to receve an message ({})", e); + thread::sleep(Duration::from_millis(500)); + }, + Ok(message) => { + println!("INFO : mqtt_automation: {}: {}", message.topic, message.payload); + if message.topic.eq("clock/time/hour") { - let message = rx.recv(); - match message { - Err(e) => { - println!("ERROR: mqttAutomation: failed to receve an message ({})", e); - thread::sleep(Duration::from_millis(500)); - }, - 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); + } + + } 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()) + } - if message.payload.eq("7") && (self.clock_dow < 5) { - self.lamp01_set(true); } + else if message.topic.eq("clock/date/dow") { - } else if message.topic.eq("/cool/devices/KNMITemp/values") { + match message.payload.parse::() { + Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e), + Ok(n) => self.clock_dow = n + } - 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 - } - } } }