forked from tfreedman/plmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x10toinst
executable file
·150 lines (137 loc) · 3.91 KB
/
x10toinst
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
#!/bin/bash
# x10toinst - wrapper for plmsend to bridge x10 to insteon protocols
# 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
BINDIR="/usr/bin"
CONFDIR="/etc"
hextox10() {
local temp
local house
local unit
local command
if echo $1 | grep -qe "0252[[:xdigit:]]\{2\}[08]0"
then
temp=$(echo $1 | sed 's/0252\([0-9a-fA-F]\{2\}\)[08]0/\1/')
temp=$(echo $temp | tr a-f A-F)
house=$(echo $temp | sed 's/\(.\)./\1/' | tr 6E2A195D7F3B084C A-P)
temp=$(echo $temp | sed 's/.\(.\)/\1/' | tr 6E2A195D7F3B084C 1-9A-G | sed 's/\([A-G]\)/1\1/' | tr A-G 0-6)
if echo $1 | grep -qe "0252..00"
then
unit=$temp
echo "$house $unit"
else
case "$temp" in
1) command="All Lights Off"; ;;
2) command="Status = Off"; ;;
3) command="On"; ;;
4) command="Preset Dim"; ;;
5) command="All Lights On"; ;;
6) command="Hail Acknowledge"; ;;
7) command="Bright"; ;;
8) command="Status = On"; ;;
9) command="Extended Code"; ;;
10) command="Status Request"; ;;
11) command="Off"; ;;
12) command="Preset Dim"; ;;
13) command="All Units Off"; ;;
14) command="Hail Request"; ;;
15) command="Dim"; ;;
16) command="Extended Data"; ;;
esac
echo "$house $command"
fi
fi
}
cleanup() {
if [ "$plmcatpid" != "" ]
then
kill -9 ${plmcatpid} &>/dev/null
exit 1
else
pids=$(ps aux | grep plmcat | grep -v grep | wc -l)
if [ "$pids" = 1 ]
then
killall plmcat &>/dev/null
exit 2
fi
exit 3
fi
}
usage() {
cat << end_of_usage
USAGE: $(basename $0)
Note: watch for orphaned plmcat processes
end_of_usage
}
if [ "$1" == "-h" -o "$1" == "--help" ]
then
usage
exit 0
fi
if [ ! -e "${CONFDIR}/plmtools.conf" ]
then
DEV="/dev/ttyUSB0"
else
DEV=$(grep "^PLMTTY\>" ${CONFDIR}/plmtools.conf | sed 's/^.*"\(.*\)"$/\1/')
fi
trap cleanup EXIT
str1=$(ps aux | grep plmcat | grep -v grep | awk '{printf $2 "\n"}')
while read line; do
if [ "$firstrun" != "FALSE" ]
then
str2=$(ps aux | grep plmcat | grep -v grep | awk '{printf $2 "\n"}')
plmcatpid=$(cat <(echo $str1) <(echo $str2) | sort | uniq -u)
firstrun="FALSE"
fi
echo "$line" >&2
if echo "$line" | grep -qe "0252[[:xdigit:]]\{2\}00 0252[[:xdigit:]]\{2\}80"
# if echo "$line" | wc -w | grep -q "\<2\>"
then
x10msg1=$(hextox10 $(echo $line | cut -d\ -f 1))
house=$(echo $x10msg1 | cut -d\ -f 1)
unit=$(echo $x10msg1 | cut -d\ -f 2)
x10msg2=$(hextox10 $(echo $line | cut -d\ -f 2))
command=$(echo $x10msg2 | cut -d\ -f 2)
if grep -q "^$house $unit\>" ${CONFDIR}/x10toinst.conf
then
device=$(grep "^$house $unit\>" ${CONFDIR}/x10toinst.conf | awk '{print $3}')
else
device=""
fi
elif echo "$line" | grep -qe "0252[[:xdigit:]][45]80"
then
x10msg2=$(hextox10 $(echo $line | cut -d\ -f 2))
house2=$(echo $x10msg2 | cut -d\ -f 1)
if [ "$house" == "$house2" ]
then
command=$(echo $x10msg2 | cut -d\ -f 2)
fi
fi
if echo "$line" | grep -qe "^0252[[:xdigit:]]\{2\}[08]0\>"
then
if [ "$device" != "" ]
then
if [ "$command" != "" ]
then
echo "insteon $device $command"
${BINDIR}/insteon "$device" "$command"
else
echo "ERROR: unknown device or command"
fi
fi
fi
done < <(${BINDIR}/plmcat -d "$DEV")