From 672a46aed0cef458b23484b35b74d45d7198967c Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Fri, 11 Apr 2025 15:13:34 +0200 Subject: [PATCH] make it more readable --- src/main.rs | 87 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4008753..f8c2e11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,20 +112,56 @@ impl Automation { qos: mqtt_client::QoS::AtMostOnce, }}); } + + fn message_in(&mut self, message: mqtt_client::MqttMessage) { + // 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") { + + let payload = json_parser::Json::Text(message.payload); + match json_parser::get_u32(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::() { + Err(e) => + println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e), + Ok(n) => self.clock_dow = n + } + + } + } } impl mqtt_client::MqttTool for Automation { fn new(client: rumqttc::Client, tx: Sender) -> Automation { - match client.subscribe("clock/time/hour", rumqttc::QoS::AtMostOnce) { - Err(e) => println!("ERROR: main: faild to subscribe to clock/time/hour ({})", e), + 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/date/dow", rumqttc::QoS::AtMostOnce) { - Err(e) => println!("ERROR: main: faild to subscribe to clock/date/dow ({})", e), + 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("/cool/devices/KNMITemp/values", rumqttc::QoS::AtMostOnce) { - Err(e) => println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e), + match client.subscribe("/cool/devices/KNMITemp/values", QoS::AtMostOnce) { + Err(e) => + println!("ERROR: main: faild to subscribe to KNMITemp/values ({})", e), Ok(_) => {} } @@ -144,33 +180,7 @@ impl mqtt_client::MqttTool for Automation { 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::() { - Err(e) => println!("ERROR: mqtt_automation: clock/date/dow has invalid payload ({:?})", e), - Ok(n) => self.clock_dow = n - } - - } + self.message_in(message); } } } @@ -206,7 +216,8 @@ fn read_config() -> Result { 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 +245,13 @@ fn main() { // get setting match read_config() { Ok(conf) => - 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 + ), Err(_) => {} }