From 563134b0fe40abe281bec27c37a9c2cfce548056 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Tue, 25 Aug 2026 10:57:49 +0200 Subject: [PATCH] feat: systemd unit, simple install script, clean up todos --- config.yml | 2 -- install.sh | 5 +++++ mqtt-remote-gpio.service | 7 +++++++ src/main.rs | 20 +++++++++++++------- src/mqtt.rs | 3 --- 5 files changed, 25 insertions(+), 12 deletions(-) create mode 100755 install.sh create mode 100644 mqtt-remote-gpio.service diff --git a/config.yml b/config.yml index 5a86ec8..8d1e438 100644 --- a/config.yml +++ b/config.yml @@ -6,8 +6,6 @@ mqtt: password: "234" user: "mqtt" -# TODO: Consider adding an auto-off setting for out mode -# Add default setting (also only for out) topics: - topic: "" # The pin id is appended as / to the topic. pin: 0 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..f95377b --- /dev/null +++ b/install.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +cargo install +sudo cp ./config.yml /etc/mqtt-remote-gpio.config.yml +sudo cp ./mqtt-remote-gpio.service /etc/systemd/system/ diff --git a/mqtt-remote-gpio.service b/mqtt-remote-gpio.service new file mode 100644 index 0000000..47fb26b --- /dev/null +++ b/mqtt-remote-gpio.service @@ -0,0 +1,7 @@ +[Service] +Type=simple +ExecStart=/usr/bin/mqtt-remote-gpio + +[Unit] +Restart=always +RestartSec=10 diff --git a/src/main.rs b/src/main.rs index a2a4530..ea2613a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,23 @@ +use std::fs; + mod conf; mod gpio_utils; mod mqtt; fn main() { - // TODO: Proper cli interface and start screen - // CLI interface should allow setting the config file - println!("mqtt-remote-gpio"); + println!(r" + __ _ _ _ _ __ _ _ __ _ + _ __ / _` || |_ | |_ _ _ ___ _ __ ___ | |_ ___ / _` || '_ \(_) ___ +| ' \ \__. || _|| _| | '_|/ -_)| ' \ / _ \| _|/ -_) \__. || .__/| |/ _ \ +|_|_|_| |_| \__| \__| |_| \___||_|_|_|\___/ \__|\___| |___/ |_| |_|\___/ + "); - // let config = conf::load_config("config.yml").unwrap(); - let config = conf::load_config("config.secret.yml").unwrap(); + let mut conf_path = std::env::args().nth(1).unwrap_or("config.yml".to_string()); + if fs::exists(conf_path).unwrap_or(false) { + conf_path = "/etc/mqtt-remote-gpio.config.yml".to_string(); + } - // TODO: Remove this when done - println!("{:#?}", config); + let config = conf::load_config(&conf_path).unwrap(); mqtt::handler(config); } diff --git a/src/mqtt.rs b/src/mqtt.rs index 29e4c12..64e9029 100644 --- a/src/mqtt.rs +++ b/src/mqtt.rs @@ -36,7 +36,6 @@ pub fn handler(config: Config) { loop { let mut i = 0; for topic in input_pins.iter_mut() { - // TODO: handle fails let val = read_pin(&mut topic.gpio); if val != pin_state[i] { pin_state[i] = val; @@ -65,8 +64,6 @@ pub fn handler(config: Config) { controller.handle_event(&content); } } - // println!("Notification {:?}", msg); thread::sleep(Duration::from_millis(config.poll_interval / 2)); - // TODO: Handle pin value change instructions } }