mirror of
https://github.com/janishutz/mqtt-remote-gpio.git
synced 2026-10-08 20:56:22 +02:00
fix: timeout can be negative, config path
This commit is contained in:
1 parent
563134b0fe
commit
d1040fc0d1
3 files changed
+16
-11
No files matched your search
+3
-3
@@ -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<Config, Box<dyn std::error::Error>> {
|
||||
// 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<Config, Box<dyn std::error::Error>> {
|
||||
.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))
|
||||
|
||||
+12
-7
@@ -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<String, OutputPin>,
|
||||
topics: Vec<String>,
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user