From ed1d701d94c5976f8277d443fafa52e0b58b434e Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Mon, 5 May 2025 16:19:25 +0200 Subject: [PATCH] add date and time object publishes --- Cargo.lock | 4 +-- Cargo.toml | 2 +- build.sh | 7 ++++ src/main.rs | 95 +++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 build.sh diff --git a/Cargo.lock b/Cargo.lock index 0aafbcf..2c589b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,8 +342,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 = "4.0.0" +source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v4.0.0#49e8adf2eb768fcd147f0d6508a2f32eed86a641" dependencies = [ "crossbeam", "rumqttc", diff --git a/Cargo.toml b/Cargo.toml index d135934..65144ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ crossbeam = "0.8.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 = "v4.0.0", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" } diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..1d5bcee --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +cross build --target aarch64-unknown-linux-gnu --release +cargo build --release + +cp target/aarch64-unknown-linux-gnu/release/mqttClock mqttClock-aarch64 +cp target/release/mqttClock mqttClock-x86_64 diff --git a/src/main.rs b/src/main.rs index e8fb022..867b801 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -use std::{fs, thread, time::Duration}; +use std::{fs, time::Duration}; use serde::Deserialize; use chrono::{Local, Timelike, Datelike}; -use mqtt_client::mqtt_client::{QoS, Sender, Receiver, MqttMessage, MqttTool}; +use mqtt_client::{mqtt_client, MqttMessage, MqttEvent, Sender, Receiver, QoS, Client, MqttTool}; fn get_u16_from_i32(value: i32) -> Option { match u16::try_from(value) { @@ -21,6 +21,9 @@ fn get_u16_from_u32(value: u32) -> Option { struct Clock { tx: Sender, + // client: Client, + connected: bool, + last_year: Option, last_month: Option, last_dom: Option, @@ -49,55 +52,88 @@ impl Clock { } } } + fn tx_time(&self) { + let second = self.last_second; + let minute = self.last_minute; + let hour = self.last_hour; + if let (Some(second), Some(minute), Some(hour)) = (second, minute, hour) { + let payload = format!("{{\"second\":{second},\"minute\":{minute},\"hour\":{hour},\"time\":\"{hour}:{minute}:{second}\"}}"); + + let message = MqttMessage { + topic: String::from("clock/time/time"), + payload, + retain: false, + qos: QoS::AtMostOnce, + }; + match self.tx.send(message) { + Err(n) => println!("ERROR: faild to send publish ({:?})", n), + Ok(_) => {} + } + } + } fn time(&mut self) { let datetime = Local::now(); if self.last_second != get_u16_from_u32(datetime.second()) { self.last_second = get_u16_from_u32(datetime.second()); - self.tx(String::from("clock/time/second"), self.last_second, false); if self.last_minute != get_u16_from_u32(datetime.minute()) { self.last_minute = get_u16_from_u32(datetime.minute()); - self.tx(String::from("clock/time/minute"), self.last_minute, false); if self.last_hour != get_u16_from_u32(datetime.hour()) { self.last_hour = get_u16_from_u32(datetime.hour()); - self.tx(String::from("clock/time/hour"), self.last_hour, false); + self.tx(String::from("clock/time/hour"), self.last_hour, true); self.date(datetime); } + self.tx(String::from("clock/time/minute"), self.last_minute, false); } + self.tx(String::from("clock/time/second"), self.last_second, false); + self.tx_time(); } } fn date(&mut self, datetime: chrono::DateTime) { if self.last_dom != get_u16_from_u32(datetime.day()) { self.last_dom = get_u16_from_u32(datetime.day()); - self.tx(String::from("clock/date/dom"), self.last_dom, true); if self.last_iso_week != get_u16_from_u32(datetime.iso_week().week()) { self.last_iso_week = get_u16_from_u32(datetime.iso_week().week()); - self.tx(String::from("clock/date/isoWeek"), self.last_iso_week, true); if self.last_iso_year != get_u16_from_i32(datetime.iso_week().year()) { self.last_iso_year = get_u16_from_i32(datetime.iso_week().year()); self.tx(String::from("clock/date/isoYear"), self.last_iso_year, true); } + self.tx(String::from("clock/date/isoWeek"), self.last_iso_week, true); } - if self.last_dow != Some(datetime.weekday() as u16) { - self.last_dow = Some(datetime.weekday() as u16); - self.tx(String::from("clock/date/dow"), self.last_dow, true); - } + self.tx(String::from("clock/date/dom"), self.last_dom, true); + if self.last_month != get_u16_from_u32(datetime.month()) { self.last_month = get_u16_from_u32(datetime.month()); - self.tx(String::from("clock/date/month"), self.last_month, true); if self.last_year != get_u16_from_i32(datetime.year()) { self.last_year = get_u16_from_i32(datetime.year()); self.tx(String::from("clock/date/year"), self.last_year, true); } + self.tx(String::from("clock/date/month"), self.last_month, true); } + + self.last_dow = Some(datetime.weekday() as u16); + self.tx(String::from("clock/date/dow"), self.last_dow, true); } } + + fn init(&mut self) { + self.last_year = None; + self.last_month = None; + self.last_dom = None; + self.last_dow = None; + self.last_iso_week = None; + self.last_iso_year = None; + self.last_hour = None; + } + } -impl MqttTool for Clock { - fn new(_client: rumqttc::Client, tx: Sender) -> Clock { +impl MqttTool for Clock { + fn new(tx: Sender, _config: i8, _client: Client) -> Clock { Clock { tx, + // client, + connected: false, last_year: None, last_month: None, last_dom: None, @@ -110,10 +146,26 @@ impl MqttTool for Clock { } } - fn run(&mut self, _rx: Receiver) { + fn run(&mut self, event: Receiver) { loop { - self.time(); - thread::sleep(Duration::from_millis(500)); + match event.recv_timeout(Duration::from_millis(500)) { + Ok(e) => { + match e { + MqttEvent::Connected => { + self.init(); + self.connected = true; + }, + MqttEvent::Disconnected => { + self.connected = false; + }, + MqttEvent::Message(_) => {/* nothing to do, no subscriptions */} + } + }, + Err(_) => {}, + } + if self.connected { + self.time(); + } } } } @@ -174,7 +226,14 @@ fn main() { // get setting match read_config() { Ok(conf) => - mqtt_client::mqtt_client::run::(conf.mqtt.host, conf.mqtt.port, conf.mqtt.client, conf.mqtt.user, conf.mqtt.pass), + mqtt_client::run::( + conf.mqtt.host, + conf.mqtt.port, + conf.mqtt.client, + conf.mqtt.user, + conf.mqtt.pass, + 0 + ), Err(_) => {} }