forked from tfreedman/plmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsteon
More file actions
executable file
·204 lines (187 loc) · 4.49 KB
/
insteon
File metadata and controls
executable file
·204 lines (187 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# insteon - wrapper to plmsend for a more intuitive interface
# Copyright (C) 2008 Matthew Randolph
# Please see the file COPYING for license information.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
CONFDIR="/etc"
BINDIR="/usr/bin"
usage() {
cat << end_of_usage
USAGE: $(basename $0) device command [command arg]
commands: on [0-100]
off
bright
dim
ping
status
level
end_of_usage
}
if [ "$1" == "-h" -o "$1" == "--help" -o "$#" -eq 0 ]
then
usage
exit 0
fi
if [ ! -e "${CONFDIR}/insteon.conf" ]
then
echo "ERROR: ${CONFDIR}/insteon.conf not found!" >&2
exit 1
fi
if [ ! -e "${CONFDIR}/plmtools.conf" ]
then
DEV="/dev/ttyUSB0"
else
DEV=$(grep "^PLMTTY\>" ${CONFDIR}/plmtools.conf | sed 's/^.*"\(.*\)"$/\1/')
fi
if [ "$1" == "everything" ]
then
for d in $(grep '^[^#].*[[:xdigit:]]\{6\}$' ${CONFDIR}/insteon.conf | awk '{printf $1 " "}')
do
${BINDIR}/insteon "$d" "$2"
done
exit 0
fi
line=$(grep "^$1\>" "${CONFDIR}/insteon.conf")
if [ "$line" == "" ]
then
echo "Unknown device"
exit 1
fi
if ! TO=$(grep "^TIMEOUT\>" ${CONFDIR}/insteon.conf | awk '{print $2}' | grep "[0-9]*")
then
TO=1000
fi
if HOPS=$(grep "^HOPS\>" ${CONFDIR}/insteon.conf | awk '{print $2}' | grep "[0-9]*")
then
case "$HOPS" in
0 | 1 | 2 | 3)
HOPS=$(printf "%.2X" $HOPS)
;;
*)
echo "ERROR: HOPS is out of range" >&2
exit 1
;;
esac
else
HOPS="03"
fi
if echo "$line" | grep -q '".*"'
then
scene=$(echo $line | sed 's/^[^"]*"//' | sed 's/".*//')
for d in $(echo $scene); do
insteon $d $2
done
exit 0
fi
device=$(echo $line | awk '{print $2}')
command="$(echo $2 | tr A-Z a-z)"
case "$command" in
on)
if [ "$3" != "" ]; then
brightness=$(printf "%.2x" $(echo "255 * $3 / 100" | bc))
else
brightness=FF
fi
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}11${brightness}
;;
off)
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1300
;;
bright | dim)
if grep -q "^ONLEVELS\>" "${CONFDIR}/insteon.conf"
then
LEVS=$(grep "^ONLEVELS\>" "${CONFDIR}/insteon.conf" | awk '{print $2}')
if [ $LEVS -eq 0 ]
then
LEVS=1
fi
TMP=$(echo "$LEVS % 2" | bc)
if [ $TMP -eq 0 ]
then
echo "On level of 50% is the most inefficient." >&2
echo "You should set ONLEVELS to an odd number." >&2
fi
else
LEVS=5
fi
if [ $LEVS -eq 31 ]
then
if [ "$command" == "bright" ]
then
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1500
else
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1600
fi
else
LEV="$(${BINDIR}/insteon $1 status | sed 's/.*\(..\)$/\1/')"
LEV=$(printf "%d" 0x${LEV})
ANS=$(echo "$LEV * $LEVS / 255" | bc)
TMP=$ANS
ANS=$(echo "$LEV * $LEVS % 255" | bc)
LENGTH=$(echo "length ( $ANS )" | bc)
ANS=$(echo "$ANS * (10 ^ $LENGTH) / 255" | bc)
STEP="${TMP}.${ANS}"
# echo "Current level $LEV is step ${STEP}"
ANS=$(echo "($STEP % 1) >= .5" | bc)
if [ $ANS -eq 1 ]
then
STEP=$(($TMP + 1))
else
STEP=$TMP
fi
# echo "Rounded step is ${STEP}"
if [ "$command" == "bright" ]
then
((STEP++))
if [ $STEP -gt $LEVS ]
then
STEP=0
fi
else
if [ $STEP -eq 0 ]
then
STEP=$LEVS
else
((STEP--))
fi
fi
# echo "Next step is $STEP"
ANS=$(echo "$STEP * 255 / $LEVS" | bc)
brightness=$(printf "%.2X" $ANS)
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}11${brightness}
fi
;;
ping)
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1000
;;
status)
${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1900
;;
level)
LEVEL="$(${BINDIR}/plmsend -t $TO -d "$DEV" 0262${device}${HOPS}1900 | sed 's/^.*\(..\)$/\1/')"
if [ "$LEVEL" != "" ]; then
LEVEL=$(printf "%d" 0x${LEVEL})
LEVEL=$(echo "$LEVEL * 100 / 255" | bc)
echo "$LEVEL"
fi
;;
*)
echo "Unknown command"
usage
exit 2
;;
esac
exit 0