put runner in a loop

This commit is contained in:
Laila van Reenen 2025-02-01 14:09:26 +01:00
parent 34805734c2
commit 3e01c8c47f
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
3 changed files with 36 additions and 35 deletions

6
Cargo.lock generated
View File

@ -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",

View File

@ -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" }

View File

@ -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<MqttMessage>) {
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::<u8>() {
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::<u8>() {
Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e),
Ok(n) => self.clock_dow = n
}
}
}
}