From d1040fc0d15b4c45aaa244293ef14a1e2a14e20c Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Wed, 26 Aug 2026 10:10:39 +0200 Subject: [PATCH] fix: timeout can be negative, config path --- src/conf.rs | 6 +++--- src/gpio_utils.rs | 19 ++++++++++++------- src/main.rs | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index eeb3fe5..499b737 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -6,7 +6,7 @@ pub struct Topic { pub topic: String, pub pin: u8, pub mode: PinMode, - pub off_timeout: u64, + pub off_timeout: i64, } #[derive(Debug, PartialEq, Clone, Copy)] @@ -33,7 +33,7 @@ pub struct Config { pub fn load_config(path: &str) -> Result> { // Read config file - let conf = fs::read_to_string(path).unwrap_or_else(|_| String::from("test")); + let conf = fs::read_to_string(path).unwrap_or("config".to_string()); let yaml = &YamlLoader::load_from_str(&conf).unwrap()[0]; let conf = yaml .as_hash() @@ -74,7 +74,7 @@ pub fn load_config(path: &str) -> Result> { .get(&Yaml::String(String::from("pin"))) .unwrap_or(&Yaml::Integer(-1)) .as_i64() - .expect(&format!("Invalid topic configuration found at index {}. The pin should be a 16 bit integer (0-65535)", i)) as u64, + .expect(&format!("Invalid topic configuration found at index {}. The pin should be a 16 bit integer (0-65535)", i)), mode: if topic .get(&Yaml::String(String::from("mode"))) .expect(&format!("Invalid topic configuration found at index {}. Mode is unset", i)) diff --git a/src/gpio_utils.rs b/src/gpio_utils.rs index c384a90..0119053 100644 --- a/src/gpio_utils.rs +++ b/src/gpio_utils.rs @@ -1,6 +1,6 @@ use rppal::gpio; use rumqttc::Publish; -use std::{collections::HashMap, time::SystemTime}; +use std::{collections::HashMap, thread, time::{Duration, SystemTime}}; use crate::conf::{PinMode, Topic}; @@ -11,7 +11,7 @@ pub struct InputPin { pub struct OutputPin { gpio: gpio::OutputPin, - off_timeout: u64, + off_timeout: i64, pub id: u8, pub topic: String, } @@ -83,7 +83,7 @@ pub fn read_pin(pin: &mut gpio::InputPin) -> u8 { pub struct GPIOController { out_pins: HashMap, topics: Vec, - timeout_topics: Vec<(String, SystemTime, u64)>, + timeout_topics: Vec<(String, SystemTime, i64)>, } impl GPIOController { @@ -139,13 +139,18 @@ impl GPIOController { } else if instr == "OFF" { println!("Turned OFF pin {}", pin.id); pin.gpio.set_low(); + } else if instr == "PRESS" || instr == "TOGGLE" { + pin.gpio.set_high(); + if pin.off_timeout > 0 { + self.timeout_topics + .push((pin.topic.clone(), SystemTime::now(), pin.off_timeout)); + } else { + thread::sleep(Duration::from_millis(10)); + pin.gpio.set_low(); + } } else { println!("Received unknown instruction {}", instr); } - if pin.off_timeout > 0 { - self.timeout_topics - .push((pin.topic.clone(), SystemTime::now(), pin.off_timeout)); - } } } } diff --git a/src/main.rs b/src/main.rs index ea2613a..4ce4299 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn main() { "); let mut conf_path = std::env::args().nth(1).unwrap_or("config.yml".to_string()); - if fs::exists(conf_path).unwrap_or(false) { + if !fs::exists(&conf_path).unwrap_or(false) { conf_path = "/etc/mqtt-remote-gpio.config.yml".to_string(); }