Compare commits
2 Commits
3e01c8c47f
...
7106e9fbe2
| Author | SHA1 | Date | |
|---|---|---|---|
| 7106e9fbe2 | |||
| 672a46aed0 |
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -280,8 +280,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mqtt-client"
|
||||
version = "2.0.2"
|
||||
source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v2.0.2#d2d31b2bc16b6d347f49a60a20290aa6880702ae"
|
||||
version = "3.0.0"
|
||||
source = "git+https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git?tag=v3.0.0#f186e437331b0c8680ac9917d7b88f17bb5f176b"
|
||||
dependencies = [
|
||||
"crossbeam",
|
||||
"rumqttc",
|
||||
@ -289,7 +289,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mqttAutomation"
|
||||
version = "1.1.4"
|
||||
version = "1.2.0"
|
||||
dependencies = [
|
||||
"crossbeam",
|
||||
"json",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mqttAutomation"
|
||||
version = "1.1.4"
|
||||
version = "1.2.0"
|
||||
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.2", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" }
|
||||
mqtt-client = { tag = "v3.0.0", git = "https://gitea.finnvanreenen.nl/LailaTheElf/mqttClient.git" }
|
||||
|
||||
@ -4,3 +4,7 @@ mqtt:
|
||||
client: "mqttAutomation01"
|
||||
user: "mqttAutomation"
|
||||
pass: "password"
|
||||
automation:
|
||||
base_topic: "/kees/automation/"
|
||||
alarm_hour: 7
|
||||
alarm_minute: 0
|
||||
|
||||
80
src/automation/json.rs
Normal file
80
src/automation/json.rs
Normal file
@ -0,0 +1,80 @@
|
||||
pub mod json_parser {
|
||||
pub enum Error {
|
||||
Null,
|
||||
InvalidType,
|
||||
ConvertionFaild,
|
||||
JsonParseError(String)
|
||||
}
|
||||
impl Error {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Error::Null => String::from("path not found"),
|
||||
Error::InvalidType => String::from("invalid type"),
|
||||
Error::ConvertionFaild => String::from("type convertion faild"),
|
||||
Error::JsonParseError(s) => s.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Json {
|
||||
Value(json::JsonValue),
|
||||
Text(String)
|
||||
}
|
||||
|
||||
pub fn get_value(value: json::JsonValue, mut path: Vec<String>) -> json::JsonValue {
|
||||
if path.len() == 0 {
|
||||
return value
|
||||
}
|
||||
match value {
|
||||
json::JsonValue::Object(obj) => {
|
||||
let key = path[0].clone();
|
||||
path.remove(0);
|
||||
get_value(obj[key].clone(), path)
|
||||
},
|
||||
json::JsonValue::Array(a) => {
|
||||
let key = path[0].clone();
|
||||
match key.parse::<usize>() {
|
||||
Ok(i) => {
|
||||
if i < a.len() {
|
||||
get_value(a[i].clone(), path)
|
||||
} else {
|
||||
json::JsonValue::Null
|
||||
}
|
||||
},
|
||||
Err(_) => json::JsonValue::Null
|
||||
}
|
||||
},
|
||||
json::JsonValue::String(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Short(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Number(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Boolean(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Null => json::JsonValue::Null,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_u32(data: Json, path: Vec<String>) -> Result<u32,Error> {
|
||||
match data {
|
||||
Json::Value(value) => match get_value(value, path) {
|
||||
json::JsonValue::Object(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Array(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::String(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Short(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Number(num) => {
|
||||
match u32::try_from(num) {
|
||||
Err(_) => Err(Error::ConvertionFaild),
|
||||
Ok(n) => Ok(n)
|
||||
}
|
||||
},
|
||||
json::JsonValue::Boolean(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Null => Err(Error::Null),
|
||||
}
|
||||
Json::Text(data) => match json::parse(&data) {
|
||||
Err(e) => {
|
||||
Err(Error::JsonParseError(e.to_string()))
|
||||
},
|
||||
Ok(value) => get_u32(Json::Value(value), path)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
199
src/automation/mod.rs
Normal file
199
src/automation/mod.rs
Normal file
@ -0,0 +1,199 @@
|
||||
mod json;
|
||||
|
||||
use std::{thread, time::Duration};
|
||||
use serde::Deserialize;
|
||||
|
||||
use mqtt_client::{MqttMessage, Sender, Receiver, QoS};
|
||||
use mqtt_client::mqtt_client;
|
||||
|
||||
use crate::automation::json::json_parser;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SettingsConf {
|
||||
base_topic: String,
|
||||
alarm_hour: u8,
|
||||
alarm_minute: u8
|
||||
}
|
||||
|
||||
pub struct Automation {
|
||||
tx: Sender<mqtt_client::MqttMessage>,
|
||||
clock_dow: u8,
|
||||
clock_hour: u8,
|
||||
clock_min: u8,
|
||||
|
||||
config: SettingsConf
|
||||
}
|
||||
|
||||
|
||||
impl Automation {
|
||||
fn tx(&self, message: mqtt_client::MqttMessage) {
|
||||
match self.tx.send(message) {
|
||||
Err(n) => println!("ERROR: faild to send publish ({:?})", n),
|
||||
Ok(_n) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn lamp01_set(&self, state: bool) {
|
||||
let payload: String;
|
||||
if state {
|
||||
payload = String::from("ON");
|
||||
} else {
|
||||
payload = String::from("OFF");
|
||||
}
|
||||
self.tx({ mqtt_client::MqttMessage {
|
||||
topic: String::from("/cool/devices/lamp-01/set"),
|
||||
payload: payload,
|
||||
retain: false,
|
||||
qos: mqtt_client::QoS::AtMostOnce,
|
||||
}});
|
||||
}
|
||||
|
||||
fn alarm(&self) {
|
||||
self.lamp01_set(true);
|
||||
}
|
||||
|
||||
fn config_message_in(&mut self, message: mqtt_client::MqttMessage, topic: String) {
|
||||
if topic.starts_with("alarm/hour") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: config_message_in: alarm/hour has invalid payload ({:?})", e),
|
||||
Ok(n) => self.config.alarm_hour = n
|
||||
}
|
||||
|
||||
}
|
||||
else if topic.eq("alarm/minute") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: config_message_in: alarm/minute has invalid payload ({:?})", e),
|
||||
Ok(n) => self.config.alarm_minute = n
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn clock_message_in(&mut self, message: mqtt_client::MqttMessage) {
|
||||
if message.topic.eq("clock/time/hour") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: clock_message_in: clock/time/hour has invalid payload ({:?})", e),
|
||||
Ok(n) => {
|
||||
self.clock_hour = n;
|
||||
|
||||
if self.config.alarm_minute == 0 && n == self.config.alarm_hour && self.clock_dow < 5 {
|
||||
self.alarm();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if message.topic.eq("clock/time/minute") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: clock_message_in: clock/time/minute has invalid payload ({:?})", e),
|
||||
Ok(n) => {
|
||||
self.clock_min = n;
|
||||
|
||||
if n == self.config.alarm_minute && self.clock_hour == self.config.alarm_hour && self.clock_dow < 5 {
|
||||
self.alarm();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if message.topic.eq("clock/date/dow") {
|
||||
|
||||
match message.payload.parse::<u8>() {
|
||||
Err(e) =>
|
||||
println!("ERROR: clock_message_in: clock/date/dow has invalid payload ({:?})", e),
|
||||
Ok(n) => self.clock_dow = n
|
||||
}
|
||||
|
||||
}
|
||||
// println!("DEBUG: clock_message_in: current time: {}:{}", self.clock_hour, self.clock_min);
|
||||
}
|
||||
|
||||
fn message_in(&mut self, message: mqtt_client::MqttMessage) {
|
||||
// println!("DEBUG : mqtt_automation: {}: {}", message.topic, message.payload);
|
||||
if message.topic.starts_with(&self.config.base_topic) {
|
||||
let topic = message.topic[self.config.base_topic.len()..].to_string();
|
||||
self.config_message_in(message, topic);
|
||||
}
|
||||
else if message.topic.starts_with("clock/") {
|
||||
self.clock_message_in(message);
|
||||
}
|
||||
else if message.topic.eq("/cool/devices/KNMITemp/values") {
|
||||
|
||||
let payload_json = json_parser::Json::Text(message.payload);
|
||||
let path = Vec::from([String::from("gr")]);
|
||||
match json_parser::get_u32(payload_json, path) {
|
||||
Ok(gr) => {
|
||||
if gr > 30 {
|
||||
self.lamp01_set(false);
|
||||
}
|
||||
},
|
||||
Err(e) =>
|
||||
print!("ERROR: mqtt_automation: KNMITemp: {}", e.to_string())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl mqtt_client::MqttTool<SettingsConf> for Automation {
|
||||
fn new(client: rumqttc::Client, tx: Sender<mqtt_client::MqttMessage>, config: SettingsConf) -> Automation {
|
||||
|
||||
match client.subscribe("clock/time/hour", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("clock/time/minute", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to clock/time/minute ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("clock/date/dow", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("/kees/automation/#", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to automation/alarm/# ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) {
|
||||
Err(e) =>
|
||||
println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
|
||||
Automation {
|
||||
tx,
|
||||
clock_dow: u8::MAX,
|
||||
clock_hour: u8::MAX,
|
||||
clock_min: u8::MAX,
|
||||
config: config
|
||||
}
|
||||
}
|
||||
|
||||
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) => {
|
||||
self.message_in(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
195
src/main.rs
195
src/main.rs
@ -1,182 +1,10 @@
|
||||
use std::{fs, thread, time::Duration};
|
||||
|
||||
use mqtt_client::{MqttMessage, Sender, Receiver, QoS};
|
||||
use std::fs;
|
||||
use serde::Deserialize;
|
||||
|
||||
use mqtt_client::mqtt_client;
|
||||
|
||||
mod json_parser {
|
||||
pub enum Error {
|
||||
Null,
|
||||
InvalidType,
|
||||
ConvertionFaild,
|
||||
JsonParseError(String)
|
||||
}
|
||||
impl Error {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Error::Null => String::from("path not found"),
|
||||
Error::InvalidType => String::from("invalid type"),
|
||||
Error::ConvertionFaild => String::from("type convertion faild"),
|
||||
Error::JsonParseError(s) => s.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Json {
|
||||
Value(json::JsonValue),
|
||||
Text(String)
|
||||
}
|
||||
|
||||
pub fn get_value(value: json::JsonValue, mut path: Vec<String>) -> json::JsonValue {
|
||||
if path.len() == 0 {
|
||||
return value
|
||||
}
|
||||
match value {
|
||||
json::JsonValue::Object(obj) => {
|
||||
let key = path[0].clone();
|
||||
path.remove(0);
|
||||
get_value(obj[key].clone(), path)
|
||||
},
|
||||
json::JsonValue::Array(a) => {
|
||||
let key = path[0].clone();
|
||||
match key.parse::<usize>() {
|
||||
Ok(i) => {
|
||||
if i < a.len() {
|
||||
get_value(a[i].clone(), path)
|
||||
} else {
|
||||
json::JsonValue::Null
|
||||
}
|
||||
},
|
||||
Err(_) => json::JsonValue::Null
|
||||
}
|
||||
},
|
||||
json::JsonValue::String(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Short(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Number(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Boolean(_) => json::JsonValue::Null,
|
||||
json::JsonValue::Null => json::JsonValue::Null,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_u32(data: Json, path: Vec<String>) -> Result<u32,Error> {
|
||||
match data {
|
||||
Json::Value(value) => match get_value(value, path) {
|
||||
json::JsonValue::Object(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Array(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::String(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Short(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Number(num) => {
|
||||
match u32::try_from(num) {
|
||||
Err(_) => Err(Error::ConvertionFaild),
|
||||
Ok(n) => Ok(n)
|
||||
}
|
||||
},
|
||||
json::JsonValue::Boolean(_) => Err(Error::InvalidType),
|
||||
json::JsonValue::Null => Err(Error::Null),
|
||||
}
|
||||
Json::Text(data) => match json::parse(&data) {
|
||||
Err(e) => {
|
||||
Err(Error::JsonParseError(e.to_string()))
|
||||
},
|
||||
Ok(value) => get_u32(Json::Value(value), path)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
struct Automation {
|
||||
tx: Sender<mqtt_client::MqttMessage>,
|
||||
clock_dow: u8
|
||||
}
|
||||
impl Automation {
|
||||
fn tx(&self, message: mqtt_client::MqttMessage) {
|
||||
match self.tx.send(message) {
|
||||
Err(n) => println!("ERROR: faild to send publish ({:?})", n),
|
||||
Ok(_n) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn lamp01_set(&self, state: bool) {
|
||||
let payload: String;
|
||||
if state {
|
||||
payload = String::from("ON");
|
||||
} else {
|
||||
payload = String::from("OFF");
|
||||
}
|
||||
self.tx({ mqtt_client::MqttMessage {
|
||||
topic: String::from("/cool/devices/lamp-01/set"),
|
||||
payload: payload,
|
||||
retain: false,
|
||||
qos: mqtt_client::QoS::AtMostOnce,
|
||||
}});
|
||||
}
|
||||
}
|
||||
impl mqtt_client::MqttTool for Automation {
|
||||
fn new(client: rumqttc::Client, tx: Sender<mqtt_client::MqttMessage>) -> Automation {
|
||||
|
||||
match client.subscribe("clock/time/hour", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("clock/date/dow", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
match client.subscribe("/cool/devices/KNMITemp/values", rumqttc::QoS::AtMostOnce) {
|
||||
Err(e) => println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e),
|
||||
Ok(_) => {}
|
||||
}
|
||||
|
||||
Automation {
|
||||
tx,
|
||||
clock_dow: u8::MAX
|
||||
}
|
||||
}
|
||||
|
||||
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") {
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod automation;
|
||||
use automation::{Automation, SettingsConf};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SettingsMQTT {
|
||||
@ -186,12 +14,11 @@ struct SettingsMQTT {
|
||||
user: String,
|
||||
pass: String
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Settings {
|
||||
mqtt: SettingsMQTT
|
||||
mqtt: SettingsMQTT,
|
||||
automation: SettingsConf
|
||||
}
|
||||
|
||||
enum SettingsError {
|
||||
ReadError,
|
||||
SyntaxError
|
||||
@ -206,7 +33,8 @@ fn read_config() -> Result<Settings,SettingsError> {
|
||||
Err(_) => {
|
||||
println!("INFO : read_config: could not find '~/.config/mqttAutomation.yml'. try '/etc/mqttAutomation.yml");
|
||||
match fs::read_to_string("/etc/mqttAutomation.yml") {
|
||||
Err(_) => println!("ERROR: read_config: could not find any config file"),
|
||||
Err(_) =>
|
||||
println!("ERROR: read_config: could not find any config file"),
|
||||
Ok(str) => config_str = str
|
||||
}
|
||||
},
|
||||
@ -234,7 +62,14 @@ fn main() {
|
||||
// get setting
|
||||
match read_config() {
|
||||
Ok(conf) =>
|
||||
mqtt_client::run::<Automation>(conf.mqtt.host, conf.mqtt.port, conf.mqtt.client, conf.mqtt.user, conf.mqtt.pass),
|
||||
mqtt_client::run::<SettingsConf, Automation>(
|
||||
conf.mqtt.host,
|
||||
conf.mqtt.port,
|
||||
conf.mqtt.client,
|
||||
conf.mqtt.user,
|
||||
conf.mqtt.pass,
|
||||
conf.automation
|
||||
),
|
||||
Err(_) => {}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user