diff options
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile | 62 | ||||
-rw-r--r-- | configure-gpio.service | 25 | ||||
-rw-r--r-- | index.php.in (renamed from httpdocs/index.php) | 31 | ||||
-rw-r--r-- | lights-out-init.service.in | 10 | ||||
-rw-r--r-- | lights-out.conf | 8 | ||||
-rw-r--r-- | lights-out.in | 126 | ||||
-rw-r--r-- | lights-out@.service.in | 8 |
8 files changed, 239 insertions, 37 deletions
@@ -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 <opensource at eckner dot net> +# +# 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/index.php.in index a697be3..c3dc987 100644 --- a/httpdocs/index.php +++ b/index.php.in @@ -1,15 +1,28 @@ <?php -$f = popen('/usr/bin/gpio read 7', 'r'); +$f = popen('#BINDIR#/lights-out get power', 'r'); if ($f === false) { - print 'Cannot access gpio'; + print 'Cannot getpower status' . "\n"; die(); } -$l = trim(fgets($f)); +$power = trim(fgets($f)); pclose($f); -if ($l == '') { - print 'Cannot read gpio - is it installed?'; +if ($power == '') { + print 'Cannot read gpio - is it installed?' . "\n"; + die(); +} + +$f = popen('#BINDIR#/lights-out name', 'r'); +if ($f === false) { + print 'Error reading server name' . "\n"; + die(); +} +$server = trim(fgets($f)); +pclose($f); + +if ($server == '') { + print 'Empty server name in config' . "\n"; die(); } @@ -42,11 +55,9 @@ function push_it() { $duration = $duration[1]; if (($duration > 0) && ($duration < 300)) { - print 'pushing for ' . ($duration * 0.1) . ' seconds ... <br>' . "\n"; - shell_exec('/usr/bin/gpio write 9 1'); + print 'pushing power button for ' . ($duration * 0.1) . ' seconds ... <br>' . "\n"; flush(); - usleep(100000 * $duration); - shell_exec('/usr/bin/gpio write 9 0'); + shell_exec('#BINDIR#/lights-out push power ' . ($duration * 0.1)); print '... done<br>' . "\n"; } } @@ -59,7 +70,7 @@ if (array_key_exists('password', $_POST)) print '</head>' . "\n"; print '<body>' . "\n"; print 'Fenster is currently '; -if ($l == '1') +if ($power == '0') print '<font color="ff0000">off</font>'; else print '<font color="00ff00">on</font>'; 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 |