From 475143e6a7fd771cd588045ad1d1be2cb424f0c2 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Sat, 30 Jan 2021 20:07:48 +0100 Subject: trocken-fertig --- .gitignore | 6 ++- Makefile | 62 ++++++++++++++++++++++ configure-gpio.service | 25 --------- httpdocs/index.php | 76 --------------------------- index.php.in | 87 +++++++++++++++++++++++++++++++ lights-out-init.service.in | 10 ++++ lights-out.conf | 8 +++ lights-out.in | 126 +++++++++++++++++++++++++++++++++++++++++++++ lights-out@.service.in | 8 +++ 9 files changed, 305 insertions(+), 103 deletions(-) create mode 100644 Makefile delete mode 100644 configure-gpio.service delete mode 100644 httpdocs/index.php create mode 100644 index.php.in create mode 100644 lights-out-init.service.in create mode 100644 lights-out.conf create mode 100644 lights-out.in create mode 100644 lights-out@.service.in diff --git a/.gitignore b/.gitignore index 0191024..b9bf5ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -.ssh -password +index.php +lights-out-init.service +lights-out +lights-out@.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e71cf0c --- /dev/null +++ b/Makefile @@ -0,0 +1,62 @@ +# +# lights-out - simple skripts for lights-out like management from a raspi +# +# Copyright (c) 2021 Erich Eckner +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, +# USA. +# + +SHELL = /bin/bash +DESTDIR = +ETCDIR = /etc +BINDIR = /usr/bin +LIBDIR = /usr/lib +HTTPDIR = /srv/http/lights-out + +VERSION = 0.0 + +all: index.php lights-out lights-out-init.service lights-out@.service + +%: %.in + sed " \ + s/#VERSION#/$(VERSION)/; \ + s@#BINDIR#@$(BINDIR)@; \ + s@#ETCDIR#@$(ETCDIR)@; \ + s@#HELPTEXT#\(\s\+\)#@ --help \1display this help and exit\n --version\1display version and exit@; \ + " $< > $@ && \ + ( [[ "$@" = *.* ]] || chmod +x "$@" ) + +.PHONY: install dist clean + +install: all + install -D -m0755 -t $(DESTDIR)$(BINDIR) lights-out + install -D -m0644 -t $(DESTDIR)$(LIBDIR)/systemd/system lights-out@.service lights-out-init.service + install -D -m0644 -t $(DESTDIR)$(ETCDIR) lights-out.conf + install -D -m0644 -t $(DESTDIR)$(HTTPDIR)/httpdocs index.php + install -D -m0600 -o http -g http /dev/null $(DESTDIR)$(HTTPDIR)/password + +clean: + git clean -x -d -f + +dist: clean + git status --porcelain 2> /dev/null | grep -q "\S" && (git add .; git commit -m"neue Version: $(VERSION)") || true + ! git tag -d v$(VERSION) 2> /dev/null + git tag v$(VERSION) + knock-knock || true + git push + git push --tags + +# End of file diff --git a/configure-gpio.service b/configure-gpio.service deleted file mode 100644 index 39fe311..0000000 --- a/configure-gpio.service +++ /dev/null @@ -1,25 +0,0 @@ -[Unit] -Description=configure the GPIO pins - -[Service] -Type=oneshot -RemainAfterExit=yes - -# power led -ExecStart=/usr/bin/gpio mode 7 in -ExecStart=/usr/bin/gpio mode 7 up - -# hdd led -ExecStart=/usr/bin/gpio mode 15 in -ExecStart=/usr/bin/gpio mode 15 up - -# power switch -ExecStart=/usr/bin/gpio mode 9 out -ExecStart=/usr/bin/gpio write 9 0 - -# reset switch -ExecStart=/usr/bin/gpio mode 8 out -ExecStart=/usr/bin/gpio write 8 0 - -[Install] -WantedBy=multi-user.target diff --git a/httpdocs/index.php b/httpdocs/index.php deleted file mode 100644 index a697be3..0000000 --- a/httpdocs/index.php +++ /dev/null @@ -1,76 +0,0 @@ -Cannot read password file
' . "\n"; - return; - } - $saved_hash = trim(fgets($f)); - fclose($f); - $entered_hash = hash('sha512', $_POST['password']); - if ($saved_hash != $entered_hash) { - print 'Wrong password: ' . $entered_hash . '
' . "\n"; - return; - } - foreach ($_POST as $key => $dummy) - if (preg_match('/^push_([0-9]+)$/', $key, $duration)) - break; - if (count($duration) != 2) - return; - $duration = $duration[1]; - if (($duration > 0) - && ($duration < 300)) { - print 'pushing for ' . ($duration * 0.1) . ' seconds ...
' . "\n"; - shell_exec('/usr/bin/gpio write 9 1'); - flush(); - usleep(100000 * $duration); - shell_exec('/usr/bin/gpio write 9 0'); - print '... done
' . "\n"; - } -} - -print '' . "\n"; -print '' . "\n"; -print 'fenster lights-out' . "\n"; -if (array_key_exists('password', $_POST)) - print '' . "\n"; -print '' . "\n"; -print '' . "\n"; -print 'Fenster is currently '; -if ($l == '1') - print 'off'; -else - print 'on'; -print '
' . "\n"; -if ($https) { - push_it(); - print '
' . "\n"; - print 'Passwort:
' . "\n"; - print '
' . "\n"; - print '
' . "\n"; - print '
' . "\n"; -} -print '' . "\n"; -print '' . "\n"; diff --git a/index.php.in b/index.php.in new file mode 100644 index 0000000..c3dc987 --- /dev/null +++ b/index.php.in @@ -0,0 +1,87 @@ +Cannot read password file
' . "\n"; + return; + } + $saved_hash = trim(fgets($f)); + fclose($f); + $entered_hash = hash('sha512', $_POST['password']); + if ($saved_hash != $entered_hash) { + print 'Wrong password: ' . $entered_hash . '
' . "\n"; + return; + } + foreach ($_POST as $key => $dummy) + if (preg_match('/^push_([0-9]+)$/', $key, $duration)) + break; + if (count($duration) != 2) + return; + $duration = $duration[1]; + if (($duration > 0) + && ($duration < 300)) { + print 'pushing power button for ' . ($duration * 0.1) . ' seconds ...
' . "\n"; + flush(); + shell_exec('#BINDIR#/lights-out push power ' . ($duration * 0.1)); + print '... done
' . "\n"; + } +} + +print '' . "\n"; +print '' . "\n"; +print 'fenster lights-out' . "\n"; +if (array_key_exists('password', $_POST)) + print '' . "\n"; +print '' . "\n"; +print '' . "\n"; +print 'Fenster is currently '; +if ($power == '0') + print 'off'; +else + print 'on'; +print '
' . "\n"; +if ($https) { + push_it(); + print '
' . "\n"; + print 'Passwort:
' . "\n"; + print '
' . "\n"; + print '
' . "\n"; + print '
' . "\n"; +} +print '' . "\n"; +print '' . "\n"; diff --git a/lights-out-init.service.in b/lights-out-init.service.in new file mode 100644 index 0000000..a7e0f55 --- /dev/null +++ b/lights-out-init.service.in @@ -0,0 +1,10 @@ +[Unit] +Description=configure the GPIO pins for lights out + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=#BINDIR#/lights-out init + +[Install] +WantedBy=multi-user.target diff --git a/lights-out.conf b/lights-out.conf new file mode 100644 index 0000000..7f6dc9e --- /dev/null +++ b/lights-out.conf @@ -0,0 +1,8 @@ +#!/hint/bash + +server='fenster' + +inputs['power']=7 +inputs['hdd']=15 +outputs['power']=9 +outputs['reset']=8 diff --git a/lights-out.in b/lights-out.in new file mode 100644 index 0000000..144d7bd --- /dev/null +++ b/lights-out.in @@ -0,0 +1,126 @@ +#!/bin/bash + +display_help() { + >&2 printf 'lights-out version #VERSION#\n' + >&2 printf '\n' + >&2 printf 'usage:\n' + >&2 printf ' lights-out get $input: display inverted pin state of $input\n' + >&2 printf ' lights-out help: display this help and exit\n' + >&2 printf ' lights-out init: init gpio\n' + >&2 printf ' lights-out name: print the name of the server\n' + >&2 printf ' lights-out push $output $time: simulate push on $output for $time seconds\n' + >&2 printf ' lights-out turn on: turn on machine if not yet running\n' +} + +check_argument_count() { + local count="$1" + shift + if [ $# -ne "${count}" ]; then + >&2 printf 'wrong number of arguments: "%s" expects %s argument' "$1" "${count}" + if [ "${count}" -ne 1 ]; then + printf 's' + fi + printf ', but %s were given\n\n' "$#" + display_help + exit 1 + fi +} + +if [ $# -lt 1 ]; then + display_help + exit 1 +fi + +if [ ! -r '#ETCDIR#/lights-out.conf' ]; then + >&2 printf 'cannot read config file "#ETCDIR#/lights-out.conf"\n' + exit 1 +fi + +if ! command -v gpio >/dev/null 2>&1; then + >&2 printf 'cannot find command "gpio" - is it installed?\n' + exit +fi + +unset inputs +unset outputs +declare -A inputs +declare -A outputs + +. '#ETCDIR#/lights-out.conf' + +if [ -z "${inputs['power']}" ]; then + >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no power input defined.\n' + exit 1 +fi + +if [ -z "${outputs['power']}" ]; then + >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no power output defined.\n' + exit 1 +fi + +if [ -z "${server}" ]; then + >&2 printf 'invalid config "#ETCDIR#/lights-out.conf": no server name defined.\n' + exit 1 +fi + +case "$1" in + 'get') + check_argument_count 2 "$@" + if [ -z "${inputs["$2"]}" ]; then + >&2 printf 'unknown input "%s"\n\n' "$2" + display_help + exit 1 + fi + value=$( + gpio read "${inputs["$2"]}" + ) || exit $? + echo $((1-value)) + ;; + 'help') + display_help + ;; + 'init') + check_argument_count 1 "$@" + for input in "${inputs[@]}"; do + gpio mode "${input}" in || exit $? + gpio mode "${input}" up || exit $? + done + for output in "${outputs[@]}"; do + gpio write "${output}" 0 || exit $? + gpio mode "${output}" out || exit $? + done + ;; + 'name') + printf '%s\n' "${server}" + ;; + 'push') + check_argument_count 3 "$@" + if [ -z "${outputs["$2"]}" ]; then + >&2 printf 'unknown output "%s"\n\n' "$2" + display_help + exit 1 + fi + gpio write "${outputs["$2"]}" 1 || exit $? + sleep "$3" + gpio write "${outputs["$2"]}" 0 || exit $? + ;; + 'turn') + if [ $# -ne 3 ] \ + || [ "x$3" != 'xon' ]; then + >&2 printf 'unknown command "%s"\n\n' "$*" + display_help + exit 1 + fi + value=$( + "$0" get power + ) || exit $? + if [ "${value}" -eq 0 ]; then + "$0" push power 0.1 || exit $? + fi + ;; + *) + >&2 printf 'unknown command "%s"\n\n' "$1" + display_help + exit 1 + ;; +esac diff --git a/lights-out@.service.in b/lights-out@.service.in new file mode 100644 index 0000000..2fe6759 --- /dev/null +++ b/lights-out@.service.in @@ -0,0 +1,8 @@ +[Unit] +Description=lights out %I +Needs=lights-out-init.service +After=lights-out-init.service + +[Service] +Type=oneshot +ExecStart=#BINDIR#/lights-out %I -- cgit v1.2.3-54-g00ecf