From ea7163f4fada0e91aed329fa7d6605604e1fd976 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 4 Mar 2019 10:01:33 -0500 Subject: [PATCH] add simple relay control command --- README | 8 ++++++-- relay-cmd | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100755 relay-cmd diff --git a/README b/README index b2a66e1..ed10daf 100644 --- a/README +++ b/README @@ -1,5 +1,9 @@ -Move libremanage to your path and add a configuration. See the USAGE which will -pop up if you make a mistake! +Run hidusb-relay-cmd-setup. + +relay-cmd is a very simple command to use the relay. + +libremanage is an overly complicated command. It requires a +configuration. See the USAGE which will pop up if you make a mistake! TODO * IMPORTANT: libremanage right now does ssh relay command 1; sleep; ssh diff --git a/relay-cmd b/relay-cmd new file mode 100755 index 0000000..982e460 --- /dev/null +++ b/relay-cmd @@ -0,0 +1,55 @@ +#!/bin/bash + +# usage: relay-cmd poweroff|poweron|reboot chan [relay_id] +# relay_id not required if there is only one relay. + +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) + echo "error: got 0 lines from running hidusb-relay-cmd state" >&2 + exit 1 + ;; + 1) : ;; + *) 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 +trap '' HUP +case $action in + poweroff) + hidusb-relay-cmd $relay_id_arg on $chan + sleep 6 + 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 + ;; + 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 + ;; + *) + echo "error: action arg not supported" >&2 + exit 1 +esac -- 2.25.1