improve documentation
[libremanage.git] / relay-cmd
CommitLineData
ea7163f4
IK
1#!/bin/bash
2
d2b5d6a9
IK
3trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4
5usage() {
6 cat <<EOF
7Usage: relay-cmd poweroff|poweron|reboot HOSTNAME|CHANNEL [RELAY_ID]
8
b0d2afab
IK
9RELAY_ID is not needed if only 1 relay device is connected. To
10understand CHANNEL and RELAY_ID, run hidusb-relay-cmd and
11hidusb-relay-cmd state. I recommend writing a config in
12/etc/relay-cmd.conf , then using HOSTNAME instead of CHANNEL and
13RELAY_ID. It serves as documentation of what you connected the relay
14to. Example config:
d2b5d6a9 15
b0d2afab 16# This config is just sourced from bash, so make sure its valid bash.
d2b5d6a9
IK
17# relay_id is not needed if only 1 relay is connected.
18cephserver3_channel=1
19cephserver3_relay_id=HURTM
b0d2afab
IK
20cephserver2_channel=2
21cephserver2_relay_id=HURTM
d2b5d6a9
IK
22EOF
23 exit $1
24}
25
26
27if (( $# < 2 )); then
28 usage
29fi
30
31read action chan relay_id <<<"$@"
32
33if [[ -e /etc/relay-cmd.conf ]]; then
34 source /etc/relay-cmd.conf
35fi
36
b0d2afab 37# we know HOSTNAME is given if it doesn't start with a number.
d2b5d6a9
IK
38if [[ $chan != [0-9]* ]]; then
39 chan_var=${chan}_channel
40 chan=${!chan_var}
41 if [[ ! $chan ]]; then
42 echo "error: channel not found in /etc/relay-cmd.conf" >&2
43 exit 1
44 fi
45 if [[ ! $relay_id ]]; then
46 relay_id_var=${chan}_relay_id
47 relay=${!relay_id_var}
48 fi
49fi
50
ea7163f4
IK
51
52if hidusb-relay-cmd state | grep ON; then
53 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
54fi
55
ea7163f4
IK
56
57case $(hidusb-relay-cmd state|wc -l) in
58 0)
59 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
60 exit 1
61 ;;
62 1) : ;;
d2b5d6a9
IK
63 *)
64 if [[ ! $relay_id ]]; then
65 echo "error: more than 1 relay device, so passing its id is required" >&2
66 exit 1
67 fi
68 ;;
ea7163f4
IK
69esac
70
d2b5d6a9 71
ea7163f4
IK
72if [[ $relay_id ]]; then
73 # leave this as an empty var if its not passed
74 relay_id_arg="id=$relay_id"
75fi
76
d2b5d6a9
IK
77# verbose command
78v() {
79 printf "+ %s\n" "$*"
80 "$@"
81}
82
83
84# ignore hup so we complete even if there is a connection problem.
ea7163f4 85trap '' HUP
d2b5d6a9 86echo "$0: doing $action. shell commands are printed to the terminal:"
ea7163f4
IK
87case $action in
88 poweroff)
d2b5d6a9
IK
89 v hidusb-relay-cmd $relay_id_arg on $chan
90 v sleep 6
91 v hidusb-relay-cmd $relay_id_arg off $chan
ea7163f4
IK
92 ;;
93 poweron)
d2b5d6a9
IK
94 v hidusb-relay-cmd $relay_id_arg on $chan
95 v sleep 1
96 v hidusb-relay-cmd $relay_id_arg off $chan
ea7163f4
IK
97 ;;
98 reboot)
d2b5d6a9
IK
99 v hidusb-relay-cmd $relay_id_arg on $chan
100 v sleep 6
101 v hidusb-relay-cmd $relay_id_arg off $chan
102 v sleep 1
103 v hidusb-relay-cmd $relay_id_arg on $chan
104 v sleep 1
105 v hidusb-relay-cmd $relay_id_arg off $chan
ea7163f4
IK
106 ;;
107 *)
108 echo "error: action arg not supported" >&2
109 exit 1
110esac
d2b5d6a9 111echo "$0: script ended. exiting"