Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a5bca9058a | |||
| ed1d701d94 |
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -342,8 +342,8 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mqtt-client"
|
name = "mqtt-client"
|
||||||
version = "2.0.1"
|
version = "4.0.0"
|
||||||
source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v2.0.1#e7a46b82a51556ef177c0bf1cb914c4385aefcb9"
|
source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v4.0.0#49e8adf2eb768fcd147f0d6508a2f32eed86a641"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossbeam",
|
"crossbeam",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
@ -351,7 +351,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mqttClock"
|
name = "mqttClock"
|
||||||
version = "1.2.1"
|
version = "1.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"crossbeam",
|
"crossbeam",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mqttClock"
|
name = "mqttClock"
|
||||||
version = "1.2.1"
|
version = "1.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -9,4 +9,4 @@ crossbeam = "0.8.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 = "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" }
|
||||||
|
|||||||
7
build.sh
Normal file
7
build.sh
Normal 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/mqttClock mqttClock-aarch64
|
||||||
|
cp target/release/mqttClock mqttClock-x86_64
|
||||||
98
src/main.rs
98
src/main.rs
@ -1,9 +1,9 @@
|
|||||||
use std::{fs, thread, time::Duration};
|
use std::{fs, time::Duration};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use chrono::{Local, Timelike, Datelike};
|
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<u16> {
|
fn get_u16_from_i32(value: i32) -> Option<u16> {
|
||||||
match u16::try_from(value) {
|
match u16::try_from(value) {
|
||||||
@ -21,6 +21,9 @@ fn get_u16_from_u32(value: u32) -> Option<u16> {
|
|||||||
|
|
||||||
struct Clock {
|
struct Clock {
|
||||||
tx: Sender<MqttMessage>,
|
tx: Sender<MqttMessage>,
|
||||||
|
// client: Client,
|
||||||
|
connected: bool,
|
||||||
|
|
||||||
last_year: Option<u16>,
|
last_year: Option<u16>,
|
||||||
last_month: Option<u16>,
|
last_month: Option<u16>,
|
||||||
last_dom: Option<u16>,
|
last_dom: Option<u16>,
|
||||||
@ -49,55 +52,91 @@ 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\":{},\"minute\":{},\"hour\":{},\"time\":\"{}:{:02}:{:02}\"}}",
|
||||||
|
second, minute, hour, 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) {
|
fn time(&mut self) {
|
||||||
let datetime = Local::now();
|
let datetime = Local::now();
|
||||||
if self.last_second != get_u16_from_u32(datetime.second()) {
|
if self.last_second != get_u16_from_u32(datetime.second()) {
|
||||||
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()) {
|
if self.last_minute != get_u16_from_u32(datetime.minute()) {
|
||||||
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()) {
|
if self.last_hour != get_u16_from_u32(datetime.hour()) {
|
||||||
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.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<Local>) {
|
fn date(&mut self, datetime: chrono::DateTime<Local>) {
|
||||||
if self.last_dom != get_u16_from_u32(datetime.day()) {
|
if self.last_dom != get_u16_from_u32(datetime.day()) {
|
||||||
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()) {
|
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.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()) {
|
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.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/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.tx(String::from("clock/date/dom"), self.last_dom, true);
|
||||||
self.last_dow = Some(datetime.weekday() as u16);
|
|
||||||
self.tx(String::from("clock/date/dow"), self.last_dow, true);
|
|
||||||
}
|
|
||||||
if self.last_month != get_u16_from_u32(datetime.month()) {
|
if self.last_month != get_u16_from_u32(datetime.month()) {
|
||||||
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()) {
|
if self.last_year != get_u16_from_i32(datetime.year()) {
|
||||||
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/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 {
|
impl MqttTool<i8> for Clock {
|
||||||
fn new(_client: rumqttc::Client, tx: Sender<MqttMessage>) -> Clock {
|
fn new(tx: Sender<MqttMessage>, _config: i8, _client: Client) -> Clock {
|
||||||
Clock {
|
Clock {
|
||||||
tx,
|
tx,
|
||||||
|
// client,
|
||||||
|
connected: false,
|
||||||
last_year: None,
|
last_year: None,
|
||||||
last_month: None,
|
last_month: None,
|
||||||
last_dom: None,
|
last_dom: None,
|
||||||
@ -110,10 +149,26 @@ impl MqttTool for Clock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, _rx: Receiver<MqttMessage>) {
|
fn run(&mut self, event: Receiver<MqttEvent>) {
|
||||||
loop {
|
loop {
|
||||||
self.time();
|
match event.recv_timeout(Duration::from_millis(500)) {
|
||||||
thread::sleep(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 +229,14 @@ fn main() {
|
|||||||
// get setting
|
// get setting
|
||||||
match read_config() {
|
match read_config() {
|
||||||
Ok(conf) =>
|
Ok(conf) =>
|
||||||
mqtt_client::mqtt_client::run::<Clock>(conf.mqtt.host, conf.mqtt.port, conf.mqtt.client, conf.mqtt.user, conf.mqtt.pass),
|
mqtt_client::run::<i8, Clock>(
|
||||||
|
conf.mqtt.host,
|
||||||
|
conf.mqtt.port,
|
||||||
|
conf.mqtt.client,
|
||||||
|
conf.mqtt.user,
|
||||||
|
conf.mqtt.pass,
|
||||||
|
0
|
||||||
|
),
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user