add simple relay control command
authorIan Kelling <iank@fsf.org>
Mon, 4 Mar 2019 15:01:33 +0000 (10:01 -0500)
committerIan Kelling <iank@fsf.org>
Mon, 4 Mar 2019 15:01:33 +0000 (10:01 -0500)
README
relay-cmd [new file with mode: 0755]

diff --git a/README b/README
index b2a66e178746113bfc50fb1559b2bdbc30b3dd45..ed10dafdca5b888bb196b14cebdbd81bfead6a83 100644 (file)
--- 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 (executable)
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