From d2b5d6a9419272bb521bd2a80af863fe0e16805a Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 4 Mar 2019 10:42:27 -0500 Subject: [PATCH] make relay-cmd have config to replace libremanage --- README | 12 ++++--- relay-cmd | 94 ++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 79 insertions(+), 27 deletions(-) diff --git a/README b/README index ed10daf..01be120 100644 --- a/README +++ b/README @@ -1,11 +1,13 @@ -Run hidusb-relay-cmd-setup. +First, run hidusb-relay-cmd-setup on the device its connected to. Ian +Kelling also has a package for openwrt, not yet published. Ask him if +you are interested. -relay-cmd is a very simple command to use the relay. +relay-cmd is a simple script config to use the relay from the device its +connected to. NOT YET TESTED. -libremanage is an overly complicated command. It requires a -configuration. See the USAGE which will pop up if you make a mistake! +libremanage is an overly complicated command that is deprecated. Some +problems with it: -TODO * IMPORTANT: libremanage right now does ssh relay command 1; sleep; ssh relay command 2. That whole thing needs to be made into a local script or else if the second ssh command fails, the relay stays in the state diff --git a/relay-cmd b/relay-cmd index 982e460..6ed7eae 100755 --- a/relay-cmd +++ b/relay-cmd @@ -1,13 +1,52 @@ #!/bin/bash -# usage: relay-cmd poweroff|poweron|reboot chan [relay_id] -# relay_id not required if there is only one relay. +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR + +usage() { + cat <&2 + exit 1 + fi + if [[ ! $relay_id ]]; then + relay_id_var=${chan}_relay_id + relay=${!relay_id_var} + fi +fi + if hidusb-relay-cmd state | grep ON; then printf "%s\n" "WARNING: output from hidusb-relay-cmd state shows an ON relay. this could mean another command instance is running, or it got stuck on due to an uncompleted command." >&2 fi -read action chan relay_id <<<"$@" case $(hidusb-relay-cmd state|wc -l) in 0) @@ -15,41 +54,52 @@ case $(hidusb-relay-cmd state|wc -l) in exit 1 ;; 1) : ;; - *) if [[ ! $relay_id ]]; then - echo "error: more than 1 relay device, so passing its id is required" >&2 - exit 1 - fi - ;; + *) + if [[ ! $relay_id ]]; then + echo "error: more than 1 relay device, so passing its id is required" >&2 + exit 1 + fi + ;; esac + if [[ $relay_id ]]; then # leave this as an empty var if its not passed relay_id_arg="id=$relay_id" fi -# ignore hup so we complete +# verbose command +v() { + printf "+ %s\n" "$*" + "$@" +} + + +# ignore hup so we complete even if there is a connection problem. trap '' HUP +echo "$0: doing $action. shell commands are printed to the terminal:" case $action in poweroff) - hidusb-relay-cmd $relay_id_arg on $chan - sleep 6 - hidusb-relay-cmd $relay_id_arg off $chan + v hidusb-relay-cmd $relay_id_arg on $chan + v sleep 6 + v hidusb-relay-cmd $relay_id_arg off $chan ;; poweron) - hidusb-relay-cmd $relay_id_arg on $chan - sleep 1 - hidusb-relay-cmd $relay_id_arg off $chan + v hidusb-relay-cmd $relay_id_arg on $chan + v sleep 1 + v hidusb-relay-cmd $relay_id_arg off $chan ;; reboot) - hidusb-relay-cmd $relay_id_arg on $chan - sleep 6 - hidusb-relay-cmd $relay_id_arg off $chan - sleep 1 - hidusb-relay-cmd $relay_id_arg on $chan - sleep 1 - hidusb-relay-cmd $relay_id_arg off $chan + v hidusb-relay-cmd $relay_id_arg on $chan + v sleep 6 + v hidusb-relay-cmd $relay_id_arg off $chan + v sleep 1 + v hidusb-relay-cmd $relay_id_arg on $chan + v sleep 1 + v hidusb-relay-cmd $relay_id_arg off $chan ;; *) echo "error: action arg not supported" >&2 exit 1 esac +echo "$0: script ended. exiting" -- 2.25.1