diff --git a/Cargo.lock b/Cargo.lock index 75c7c9d..3312b8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -264,6 +264,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "libc" version = "0.2.169" @@ -318,6 +324,7 @@ version = "0.1.0" dependencies = [ "chrono", "crossbeam", + "json", "rumqttc", ] diff --git a/Cargo.toml b/Cargo.toml index 0b3b5cb..68c3ccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] chrono = "0.4.39" crossbeam = "0.8.4" +json = "0.12.4" rumqttc = "0.24.0" diff --git a/src/main.rs b/src/main.rs index b288735..f9a289e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ -use std::{time::Duration}; +use std::time::Duration; use std::thread; use rumqttc::{Client, Event, MqttOptions, Outgoing, Packet, QoS}; -use chrono::{Local, Timelike, Datelike}; use crossbeam::channel::{unbounded, Sender}; struct MqttMessage { @@ -18,106 +17,59 @@ fn send_publish(publish: &Sender, message: MqttMessage) { } } -fn lamp_on(publish: &Sender) { +fn lamp01_set(publish: &Sender, state: bool) { + let payload: String; + if state { + payload = String::from("ON"); + } else { + payload = String::from("OFF"); + } send_publish(publish, { MqttMessage { topic: String::from("/cool/devices/lamp-01/set"), - payload: String::from("ON") + payload: payload }}); } fn mqtt_automation(publish: &Sender, message: MqttMessage) { - if message.topic.eq("clock/hour") && message.payload.eq("16") { - lamp_on(publish); - } -} + println!("DEBUG: mqtt_automation: {}: {}", message.topic, message.payload); + if message.topic.eq("clock/hour") && message.payload.eq("7") { -fn mqtt_clock(publish: Sender) { - // let clock_topic: String = String::from("clock"); + lamp01_set(publish, true); - // init last values with invalid values - let mut last_year: i32 = 65535; - let mut last_month: u32 = 65535; - let mut last_dom: u32 = 65535; - let mut last_dow: u32 = 65535; - let mut last_iso_week: u32 = 65535; - let mut last_iso_year: i32 = 65535; - let mut last_hour: u32 = 65535; - // let mut last_hour12: u32 = 65535; - let mut last_minute: u32 = 65535; - let mut last_second: u32 = 65535; + } else if message.topic.eq("/cool/devices/KNMITemp/values") { - loop { - let datetime = Local::now(); - if last_second != datetime.second() { - last_second = datetime.second(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/second"), - payload: last_second.to_string() - }}); - if last_minute != datetime.minute() { - last_minute = datetime.minute(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/minute"), - payload: last_minute.to_string() - }}); - if last_hour != datetime.hour() { - last_hour = datetime.hour(); - // last_hour12 = datetime.hour12(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/hour"), - payload: last_hour.to_string() - }}); - // send_publish(&publish, { MqttMessage { - // topic: String::from("clock/hour12"), - // payload: last_hour12.to_string() - // }}); - if last_dom != datetime.day() { - last_dom = datetime.day(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/dom"), - payload: last_dom.to_string() - }}); - if last_iso_week != datetime.iso_week().week() { - last_iso_week = datetime.iso_week().week(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/isoWeek"), - payload: last_iso_week.to_string() - }}); - if last_iso_year != datetime.iso_week().year() { - last_iso_year = datetime.iso_week().year(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/isoYear"), - payload: last_iso_year.to_string() - }}); - } - } - if last_dow != datetime.weekday() as u32 { - last_dow = datetime.weekday() as u32; - send_publish(&publish, { MqttMessage { - topic: String::from("clock/dow"), - payload: last_dow.to_string() - }}); - } - if last_month != datetime.month() { - last_month = datetime.month(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/month"), - payload: last_month.to_string() - }}); - if last_year != datetime.year() { - last_year = datetime.year(); - send_publish(&publish, { MqttMessage { - topic: String::from("clock/year"), - payload: last_year.to_string() - }}); + match json::parse(&message.payload) { + Err(e) => println!("ERROR: mqtt_automation: KNMITemp: invalid json ({})", e), + Ok(payload) => { + match payload { + json::JsonValue::Object(obj) => { + match obj["gr"] { + json::JsonValue::Number(number) => { + let gr = match u16::try_from(number) { + Err(_) => 0, + Ok(n) => n + }; + println!("DEBUG: mqtt_automation: KNMITemp: gr = {}", gr); + if gr > 30 { + lamp01_set(publish, false); + } } + json::JsonValue::Null => {},json::JsonValue::Short(_) => {},json::JsonValue::String(_) => {},json::JsonValue::Boolean(_) => {},json::JsonValue::Object(_) => {},json::JsonValue::Array(_) => {}, } } + json::JsonValue::Null => {},json::JsonValue::Short(_) => {},json::JsonValue::String(_) => {},json::JsonValue::Number(_) => {},json::JsonValue::Boolean(_) => {},json::JsonValue::Array(_) => {}, } + } } - thread::sleep(Duration::from_millis(500)); + } + // else if message.topic.eq("clock/dow") { + // match u8::try_from(message.payload) { + // Err(e) => println!("ERROR: mqtt_automation: clock/dow has invalid payload ({:?})", e), + // Ok(n) => automation_dow = n + // } + // } } fn mqtt_recive(publish: &Sender, e: Event) { @@ -140,8 +92,6 @@ fn mqtt_recive(publish: &Sender, e: Event) { Packet::Connect(_connect) => {}, //println!("INFO : mqtt_recive: in Connect"), Packet::ConnAck(_connack) => {}, //println!("INFO : mqtt_recive: in ConnAck"), Packet::Publish(msg) => { - // println!("INFO : mqtt_recive: in msg {:?}", msg); - let payload = match String::from_utf8(msg.payload.to_vec()) { Err(e) => panic!("ERROR: pqtt_recive: faild to decode payload ({})", e), Ok(v) => v @@ -177,10 +127,10 @@ fn main() { mqttoptions.set_credentials("rustClient", "test"); let (client, mut connection) = Client::new(mqttoptions, 10); - client.subscribe("/cool/devices/#", QoS::AtMostOnce).unwrap(); - client.subscribe("clock/#", QoS::AtMostOnce).unwrap(); + client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce).unwrap(); + client.subscribe("clock/hour", QoS::AtMostOnce).unwrap(); - // treath publisher + // thread publisher let publisher = thread::Builder::new() .name("publisher".to_string()) .spawn(move || { @@ -203,18 +153,6 @@ fn main() { Ok(_n) => {} } - // treath mqtt clock - let mqtt_publish_clock = mqtt_publish.clone(); - let mqtt_clock_stat = thread::Builder::new() - .name("mqtt_clock".to_string()) - .spawn(move || { - mqtt_clock(mqtt_publish_clock); - }); - match mqtt_clock_stat { - Err(_n) => println!("ERROR: mqtt clock: faild to start thread"), - Ok(_n) => {} - } - for (_i, notification) in connection.iter().enumerate() { // println!("Notification = {:?}", notification); match notification {