fix some bugs

This commit is contained in:
Laila van Reenen 2025-05-05 15:01:22 +02:00
parent dc60244834
commit 642e02949f
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
4 changed files with 119 additions and 46 deletions

6
Cargo.lock generated
View File

@ -280,8 +280,8 @@ dependencies = [
[[package]] [[package]]
name = "mqtt-client" name = "mqtt-client"
version = "3.0.0" version = "4.0.0"
source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v3.0.0#f186e437331b0c8680ac9917d7b88f17bb5f176b" source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v4.0.0#49e8adf2eb768fcd147f0d6508a2f32eed86a641"
dependencies = [ dependencies = [
"crossbeam", "crossbeam",
"rumqttc", "rumqttc",
@ -289,7 +289,7 @@ dependencies = [
[[package]] [[package]]
name = "mqttAutomation" name = "mqttAutomation"
version = "1.3.0" version = "1.3.1"
dependencies = [ dependencies = [
"crossbeam", "crossbeam",
"json", "json",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mqttAutomation" name = "mqttAutomation"
version = "1.3.0" version = "1.3.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
@ -9,4 +9,4 @@ json = "0.12.4"
rumqttc = "0.24.0" rumqttc = "0.24.0"
serde = { version = "1.0.217", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] }
serde_yaml = "0.9.34" serde_yaml = "0.9.34"
mqtt-client = { tag = "v3.0.0", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } mqtt-client = { tag = "v4.0.0", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" }

7
build.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/sh
cross build --target aarch64-unknown-linux-gnu --release
cargo build --release
cp target/aarch64-unknown-linux-gnu/release/mqttAutomation mqttAutomation-aarch64
cp target/release/mqttAutomation mqttAutomation-x86_64

View File

@ -3,7 +3,7 @@ mod json;
use std::{thread, time::Duration}; use std::{thread, time::Duration};
use serde::Deserialize; use serde::Deserialize;
use mqtt_client::{MqttMessage, Sender, Receiver, QoS}; use mqtt_client::{MqttMessage, MqttEvent, Sender, Receiver, QoS};
use mqtt_client::mqtt_client; use mqtt_client::mqtt_client;
use crate::automation::json::json_parser; use crate::automation::json::json_parser;
@ -40,7 +40,8 @@ fn ping_stat_update(stats: &mut PingStats, payload: String) -> Option<f32> {
} }
pub struct Automation { pub struct Automation {
tx: Sender<mqtt_client::MqttMessage>, tx: Sender<MqttMessage>,
client: mqtt_client::Client,
clock_dow: u8, clock_dow: u8,
clock_hour: u8, clock_hour: u8,
@ -57,7 +58,7 @@ pub struct Automation {
impl Automation { impl Automation {
fn tx(&self, message: mqtt_client::MqttMessage) { fn tx(&self, message: MqttMessage) {
match self.tx.send(message) { match self.tx.send(message) {
Err(n) => println!("ERROR: faild to send publish ({:?})", n), Err(n) => println!("ERROR: faild to send publish ({:?})", n),
Ok(_n) => {} Ok(_n) => {}
@ -71,7 +72,7 @@ impl Automation {
} else { } else {
payload = String::from("OFF"); payload = String::from("OFF");
} }
self.tx({ mqtt_client::MqttMessage { self.tx({ MqttMessage {
topic: topic, topic: topic,
payload: payload, payload: payload,
retain: false, retain: false,
@ -86,35 +87,76 @@ impl Automation {
} }
fn get_current_time(&self) -> u32 { fn get_current_time(&self) -> u32 {
u32::from(self.clock_dow) * 24 + u32::from(self.clock_hour) * 60 + u32::from(self.clock_min) self.get_time(None, None, None, None)
}
fn get_time(&self, dow: Option<u8>, hour: Option<u8>, min: Option<u8>, sec: Option<u8>) -> u32 {
let d = match dow {
Some(n) => n,
None => self.clock_dow
};
let h = match hour {
Some(n) => n,
None => self.clock_hour
};
let m = match min {
Some(n) => n,
None => self.clock_min
};
let s = match sec {
Some(n) => n,
None => self.clock_sec
};
((u32::from(d) * 24 + u32::from(h)) * 60 + u32::from(m)) * 60 + u32::from(s)
} }
fn alarm(&self) { fn alarm_trigger(&self) {
self.lamp01_set(true); self.lamp01_set(true);
} }
fn alarm_send_config(&self) {
fn config_message_in(&mut self, message: mqtt_client::MqttMessage, topic: String) { self.tx({ MqttMessage {
if topic.starts_with("alarm/hour") { topic: format!("{}alarm", self.config.base_topic),
payload: format!("{}:{}", self.config.alarm_hour, self.config.alarm_minute),
match message.payload.parse::<u8>() { retain: true,
Err(e) => qos: mqtt_client::QoS::AtMostOnce,
println!("ERROR: config_message_in: alarm/hour has invalid payload ({:?})", e), }});
Ok(n) => self.config.alarm_hour = n
} }
} fn config_message_in(&mut self, message: MqttMessage, topic: String) {
else if topic.eq("alarm/minute") { if topic.starts_with("alarm/set") {
match message.payload.parse::<u8>() { let mut time = message.payload.split(':');
Err(e) => if time.clone().count() != 2 {
println!("ERROR: config_message_in: alarm/minute has invalid payload ({:?})", e), println!("ERROR: config_message_in: alarm/set has invalid payload. incorect number of slices ({})", message.payload)
Ok(n) => self.config.alarm_minute = n
} }
else {
let hour_str = time.next().unwrap();
let min_str = time.next().unwrap();
let mut hour: Option<u8> = None;
let mut min: Option<u8> = None;
match hour_str.parse::<u8>() {
Err(_) =>
println!("ERROR: config_message_in: alarm/set has invalid payload. hour is NaN ({})", message.payload),
Ok(n) => hour = Some(n)
}
match min_str.parse::<u8>() {
Err(_) =>
println!("ERROR: config_message_in: alarm/set has invalid payload. min is NaN ({})", message.payload),
Ok(n) => min = Some(n)
}
if hour != None && min != None {
self.config.alarm_hour = hour.unwrap();
self.config.alarm_minute = min.unwrap();
}
}
self.alarm_send_config();
} }
} }
fn clock_message_in(&mut self, message: mqtt_client::MqttMessage) { fn clock_message_in(&mut self, message: MqttMessage) {
if message.topic.eq("clock/time/second") { if message.topic.eq("clock/time/second") {
match message.payload.parse::<u8>() { match message.payload.parse::<u8>() {
@ -129,7 +171,7 @@ impl Automation {
self.ping_elfdesktop.avg, self.ping_elfdesktop.avg,
1.0 - (f32::from(self.ping_elfdesktop.fails) / f32::from(self.ping_elfdesktop.total)) 1.0 - (f32::from(self.ping_elfdesktop.fails) / f32::from(self.ping_elfdesktop.total))
); );
self.tx({ mqtt_client::MqttMessage { self.tx({ MqttMessage {
topic: String::from("/kees/db/insert/ping"), topic: String::from("/kees/db/insert/ping"),
payload: json, payload: json,
retain: false, retain: false,
@ -152,9 +194,10 @@ impl Automation {
self.clock_min = n; self.clock_min = n;
if n == self.config.alarm_minute if n == self.config.alarm_minute
&& n != 0
&& self.clock_hour == self.config.alarm_hour && self.clock_hour == self.config.alarm_hour
&& self.clock_dow < 5 { && self.clock_dow < 5 {
self.alarm(); self.alarm_trigger();
} }
} }
} }
@ -168,8 +211,10 @@ impl Automation {
Ok(n) => { Ok(n) => {
self.clock_hour = n; self.clock_hour = n;
self.alarm_send_config();
if self.config.alarm_minute == 0 && n == self.config.alarm_hour && self.clock_dow < 5 { if self.config.alarm_minute == 0 && n == self.config.alarm_hour && self.clock_dow < 5 {
self.alarm(); self.alarm_trigger();
} }
} }
} }
@ -187,7 +232,7 @@ impl Automation {
// println!("DEBUG: clock_message_in: current time: {}:{}:{}", self.clock_hour, self.clock_min, self.clock_sec); // println!("DEBUG: clock_message_in: current time: {}:{}:{}", self.clock_hour, self.clock_min, self.clock_sec);
} }
fn message_in(&mut self, message: mqtt_client::MqttMessage) { fn message_in(&mut self, message: MqttMessage) {
// println!("DEBUG : mqtt_automation: {}: {}", message.topic, message.payload); // println!("DEBUG : mqtt_automation: {}: {}", message.topic, message.payload);
if message.topic.starts_with(&self.config.base_topic) { if message.topic.starts_with(&self.config.base_topic) {
let topic = message.topic[self.config.base_topic.len()..].to_string(); let topic = message.topic[self.config.base_topic.len()..].to_string();
@ -203,8 +248,15 @@ impl Automation {
match json_parser::get_u32(payload_json, path) { match json_parser::get_u32(payload_json, path) {
Ok(gr) => { Ok(gr) => {
if gr > 30 { if gr > 30 {
let alarm_time =
self.get_time(None, Some(self.config.alarm_hour), Some(self.config.alarm_minute), Some(0));
let time_30min =
self.get_time(Some(0), Some(0), Some(29), Some(0));
let time_diff: i64 = i64::from(self.get_current_time()) - i64::from(alarm_time);
if self.clock_dow >= 5 || time_diff < 0 || time_diff > i64::from(time_30min) {
self.lamp01_set(false); self.lamp01_set(false);
} }
}
}, },
Err(e) => Err(e) =>
print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string()) print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string())
@ -236,40 +288,46 @@ impl Automation {
} }
} }
}
impl mqtt_client::MqttTool<SettingsConf> for Automation { fn init(&self) {
fn new(client: rumqttc::Client, tx: Sender<mqtt_client::MqttMessage>, config: SettingsConf) -> Automation { println!("DEBUG: init");
match self.client.subscribe("clock/time/#", QoS::AtMostOnce) {
match client.subscribe("clock/time/#", QoS::AtMostOnce) {
Err(e) => Err(e) =>
println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e), println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e),
Ok(_) => {} Ok(_) => {}
} }
match client.subscribe("clock/date/dow", QoS::AtMostOnce) { match self.client.subscribe("clock/date/dow", QoS::AtMostOnce) {
Err(e) => Err(e) =>
println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e), println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e),
Ok(_) => {} Ok(_) => {}
} }
match client.subscribe("/kees/automation/#", QoS::AtMostOnce) { match self.client.subscribe("/kees/automation/#", QoS::AtMostOnce) {
Err(e) => Err(e) =>
println!("ERROR: main: faild to subscribe to automation/alarm/# ({})", e), println!("ERROR: main: faild to subscribe to automation/alarm/# ({})", e),
Ok(_) => {} Ok(_) => {}
} }
match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) { match self.client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) {
Err(e) => Err(e) =>
println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e), println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
Ok(_) => {} Ok(_) => {}
} }
match client.subscribe("/kees/ping/#", QoS::AtMostOnce) { match self.client.subscribe("/kees/ping/#", QoS::AtMostOnce) {
Err(e) => Err(e) =>
println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e), println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
Ok(_) => {} Ok(_) => {}
} }
self.alarm_send_config();
}
}
impl mqtt_client::MqttTool<SettingsConf> for Automation {
fn new(tx: Sender<MqttMessage>, config: SettingsConf, client: mqtt_client::Client) -> Automation {
Automation { Automation {
tx, tx,
client,
clock_dow: u8::MAX, clock_dow: u8::MAX,
clock_hour: u8::MAX, clock_hour: u8::MAX,
@ -285,7 +343,7 @@ impl mqtt_client::MqttTool<SettingsConf> for Automation {
} }
} }
fn run(&mut self, rx: Receiver<MqttMessage>) { fn run(&mut self, rx: Receiver<MqttEvent>) {
loop { loop {
let message = rx.recv(); let message = rx.recv();
match message { match message {
@ -293,8 +351,16 @@ impl mqtt_client::MqttTool<SettingsConf> for Automation {
println!("ERROR: mqttAutomation: failed to receve an message ({})", e); println!("ERROR: mqttAutomation: failed to receve an message ({})", e);
thread::sleep(Duration::from_millis(500)); thread::sleep(Duration::from_millis(500));
}, },
Ok(message) => { Ok(event) => {
match event {
MqttEvent::Connected => {
self.init();
},
MqttEvent::Disconnected => {},
MqttEvent::Message(message) => {
self.message_in(message); self.message_in(message);
},
}
} }
} }
} }