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 topic: String,
|
||||||
pub pin: u8,
|
pub pin: u8,
|
||||||
pub mode: PinMode,
|
pub mode: PinMode,
|
||||||
pub off_timeout: u64,
|
pub off_timeout: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
#[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>> {
|
pub fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
|
||||||
// Read config file
|
// 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 yaml = &YamlLoader::load_from_str(&conf).unwrap()[0];
|
||||||
let conf = yaml
|
let conf = yaml
|
||||||
.as_hash()
|
.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")))
|
.get(&Yaml::String(String::from("pin")))
|
||||||
.unwrap_or(&Yaml::Integer(-1))
|
.unwrap_or(&Yaml::Integer(-1))
|
||||||
.as_i64()
|
.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
|
mode: if topic
|
||||||
.get(&Yaml::String(String::from("mode")))
|
.get(&Yaml::String(String::from("mode")))
|
||||||
.expect(&format!("Invalid topic configuration found at index {}. Mode is unset", i))
|
.expect(&format!("Invalid topic configuration found at index {}. Mode is unset", i))
|
||||||
|
|||||||
+12
-7
@@ -1,6 +1,6 @@
|
|||||||
use rppal::gpio;
|
use rppal::gpio;
|
||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use std::{collections::HashMap, time::SystemTime};
|
use std::{collections::HashMap, thread, time::{Duration, SystemTime}};
|
||||||
|
|
||||||
use crate::conf::{PinMode, Topic};
|
use crate::conf::{PinMode, Topic};
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ pub struct InputPin {
|
|||||||
|
|
||||||
pub struct OutputPin {
|
pub struct OutputPin {
|
||||||
gpio: gpio::OutputPin,
|
gpio: gpio::OutputPin,
|
||||||
off_timeout: u64,
|
off_timeout: i64,
|
||||||
pub id: u8,
|
pub id: u8,
|
||||||
pub topic: String,
|
pub topic: String,
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,7 @@ pub fn read_pin(pin: &mut gpio::InputPin) -> u8 {
|
|||||||
pub struct GPIOController {
|
pub struct GPIOController {
|
||||||
out_pins: HashMap<String, OutputPin>,
|
out_pins: HashMap<String, OutputPin>,
|
||||||
topics: Vec<String>,
|
topics: Vec<String>,
|
||||||
timeout_topics: Vec<(String, SystemTime, u64)>,
|
timeout_topics: Vec<(String, SystemTime, i64)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GPIOController {
|
impl GPIOController {
|
||||||
@@ -139,13 +139,18 @@ impl GPIOController {
|
|||||||
} else if instr == "OFF" {
|
} else if instr == "OFF" {
|
||||||
println!("Turned OFF pin {}", pin.id);
|
println!("Turned OFF pin {}", pin.id);
|
||||||
pin.gpio.set_low();
|
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 {
|
} else {
|
||||||
println!("Received unknown instruction {}", instr);
|
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());
|
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();
|
conf_path = "/etc/mqtt-remote-gpio.config.yml".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user