-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
#!/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 <<EOF
+Usage: relay-cmd poweroff|poweron|reboot HOSTNAME|CHANNEL [RELAY_ID]
+
+For CHANNEL and RELAY_ID, run hidusb-relay-cmd and hidusb-relay-cmd state.
+I recommend writing a config to use HOSTNAME instead. Config is /etc/relay-cmd.conf
+Example config:
+
+# This config is just bash that gets sourced.
+# relay_id is not needed if only 1 relay is connected.
+cephserver3_channel=1
+cephserver3_relay_id=HURTM
+EOF
+ exit $1
+}
+
+
+if (( $# < 2 )); then
+ usage
+fi
+
+read action chan relay_id <<<"$@"
+
+if [[ -e /etc/relay-cmd.conf ]]; then
+ source /etc/relay-cmd.conf
+fi
+
+if [[ $chan != [0-9]* ]]; then
+ chan_var=${chan}_channel
+ chan=${!chan_var}
+ if [[ ! $chan ]]; then
+ echo "error: channel not found in /etc/relay-cmd.conf" >&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)
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"