-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiz.sh
300 lines (270 loc) · 6.81 KB
/
wiz.sh
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
local PORT=38899
declare -A lights
function __wiz-init() {
# hyrdrate the arp table with the available nodes
__wiz-light-info-raw &>/dev/null
# hyrdate the lights array
__assign_lights $1
}
function wiz() {
local ip=''
local location=$(tolower $1)
local command=$(tolower $2)
local name=$(tolower $3)
__wiz-init $location
case $command in
'set')
ip=$(__wiz-light-ip-for $name)
;;
'set-all')
__wiz-light-set-all "${@:3}"
return
;;
'get')
ip=$(__wiz-light-ip-for $name)
;;
'get-all')
__wiz-light-info-all
return
;;
*)
echo >&2 "Error: must provide set, set-all, get, or get-all"
return
esac
#if they provided incorrect light name then return error
if [[ $ip == *"Error"* ]]; then
echo $ip
return
fi
#at this point we know it's either set or get for individual and we have valid ip
case $command in
'set')
__wiz-light-set $ip "${@:4}"
;;
'get')
__wiz-light-info-for $name
;;
esac
}
function __wiz-light-info-raw() {
echo '{"method":"getPilot","params":{}}' | socat - UDP-DATAGRAM:255.255.255.255:38899,broadcast
}
function __wiz-light-info-all() {
results=$(mktemp)
for key ("${(@k)lights}"); do
result=$(mktemp)
info=$(__wiz-light-info-for $key)
local id=$(echo $info | jq -r '.id')
local ip=$(echo $info | jq -r '.ip')
local state=$(echo $info | jq -r '.state')
local scene=$(echo $info | jq -r '.scene')
local rgb=$(echo $info | jq -r '.rgb')
local cw=$(echo $info | jq -r '.cw')
local dimming=$(echo $info | jq -r '.dimming')
cat <<-EOF > "$result"
{
"id": "$id",
"ip": "$ip",
"state": "$state",
"scene": "$scene",
"rgb": "$rgb",
"cw": "$cw",
"dimming": "$dimming"
}
EOF
jq . "${result}" >> $results
done
jq -s . "${results}"
}
function __wiz-light-info-for() {
name=$1
ip=$(__wiz-light-ip-for $name)
if [ -z "$ip" ]
then
body=$(mktemp)
cat <<-EOF > "$body"
{
"id": "$name",
"ip": "error",
"state": "error",
"scene": "error",
"rgb": "error",
"cw": "error",
"dimming": "error"
}
EOF
jq . "${body}"
return
fi
cmd='{"method":"getPilot"}'
results=$(echo $cmd | nc -u -w 1 $ip $PORT)
sceneId="$(echo $results | jq '.result.sceneId')"
scene=$(__get_scene_name_from_id $sceneId)
[[ "$(echo $results | jq '.result.state')" == "true" ]] && state="on" || state="off"
[[ "$(echo $results | jq '.result.r')" != "null" ]] && r="$(echo $results | jq '.result.r')" || r="0"
[[ "$(echo $results | jq '.result.g')" != "null" ]] && g="$(echo $results | jq '.result.g')" || g="0"
[[ "$(echo $results | jq '.result.b')" != "null" ]] && b="$(echo $results | jq '.result.b')" || b="0"
[[ "$(echo $results | jq '.result.c')" != "null" ]] && c="$(echo $results | jq '.result.c')" || c="0"
[[ "$(echo $results | jq '.result.w')" != "null" ]] && w="$(echo $results | jq '.result.w')" || w="0"
[[ "$(echo $results | jq '.result.dimming')" != "null" ]] && dimming="$(echo $results | jq '.result.dimming')" || dimming="0"
body=$(mktemp)
cat <<-EOF > "$body"
{
"id": "$name",
"ip": "$ip",
"state": "$state",
"scene": "$scene",
"rgb": "$r $g $b",
"cw": "$c $w",
"dimming": "$dimming"
}
EOF
jq . "${body}"
}
function __wiz-light-set() {
ip=$1
cmd=""
case $2 in
on)
cmd='{"method":"setPilot","params":{"state":true}}'
;;
off)
cmd='{"method":"setPilot","params":{"state":false}}'
;;
rgb)
[[ ! -z "$3" ]] && r=$3 || r=0
[[ ! -z "$4" ]] && g=$4 || g=0
[[ ! -z "$5" ]] && b=$5 || b=0
[[ ! -z "$6" ]] && c=$6 || c=0
[[ ! -z "$7" ]] && w=$7 || w=0
cmd='{"method":"setPilot","params":{"r":'"$r"',"g":'"$g"',"b":'"$b"',"c":'"$c"',"w":'"$w"'}}'
# wiz home set nightstand rgb 56 0 255 0 112
;;
dimming)
# TODO: dimming between 10 and 100
dimming=10
if [ ! -z "$3" ] ; then dimming=$3; fi
cmd='{"method":"setPilot","params":{"dimming":'"$dimming"'}}'
;;
*)
scene=$(__get_scene_id_from_name $2)
cmd='{"method":"setPilot","params":{"sceneId":'"$scene"'}}'
;;
esac
results=$(echo $cmd | nc -u -w 1 $ip $PORT)
err=$results | jq '.error.message'
if [ ! -z $err ]; then echo $err; else echo "success for $1"; fi
}
function __wiz-light-set-all() {
for key ("${(@k)lights}"); do
ip=$(__wiz-light-ip-for $key)
__wiz-light-set $ip "$@"
done
}
function __wiz-light-ip-for() {
mac=$(__get_mac_from_name $1)
if [ -z $mac ]; then
echo "Error: $1 not in list of lights"
return
fi
ip=$(arp -a | grep $mac | awk '{print $2}' | sed 's/[()]//g')
echo $ip
}
function __get_scene_id_from_name() {
local name=$(tolower $1)
for key ("${(@k)wiz_scenes}"); do
if [[ $key == $name ]]; then
echo $wiz_scenes[$key]
fi
done
}
function __get_scene_name_from_id() {
local id=$1
for key ("${(@k)wiz_scenes}"); do
if [[ $wiz_scenes[$key] == $id ]]; then
echo $key
fi
done
}
function __get_mac_from_name() {
local name=$(tolower $1)
for key ("${(@k)lights}"); do
if [[ $key == $name ]]; then
echo $lights[$key]
fi
done
}
typeset -A wiz_scenes
wiz_scenes=(
"null" "0"
"ocean" "1"
"romance" "2"
"sunset" "3"
"party" "4"
"fireplace" "5"
"cozy" "6"
"forest" "7"
"pastel" "8"
"wake" "9"
"bedtime" "10"
"warm" "11"
"daylight" "12"
"cool" "13"
"night" "14"
"focus" "15"
"relax" "16"
"true" "17"
"tv" "18"
"plant" "19"
"spring" "20"
"summer" "21"
"fall" "22"
"deepdive" "23"
"jungle" "24"
"mojito" "25"
"club" "26"
"christmas" "27"
"halloween" "28"
"candlelight" "29"
"golden" "30"
"pulse" "31"
"steampunk" "32"
)
typeset -A wiz_home_macs
wiz_home_macs=(
"nightstand" "6c:29:90:4c:69:aa"
"bennett_r" "d8:a0:11:bc:3f:c9"
"bennett_l" "d8:a0:11:3e:d:bc"
"tree" "a8:bb:50:e1:30:48"
)
typeset -A wiz_office_macs
wiz_office_macs=(
"andrew" "d8:a0:11:be:cc:cb"
"ben" "44:4f:8e:b:5b:a0"
"coffee" "44:4f:8e:a:e0:f0"
"jj" "44:4f:8e:a:92:72"
)
function __assign_lights() {
lights=()
location=$(tolower $1)
case $location in
"office")
for key value in ${(kv)wiz_office_macs}; do
lights[$key]=$value
done
;;
"home")
for key value in ${(kv)wiz_home_macs}; do
lights[$key]=$value
done
;;
*)
echo "Error: location must be 'office' or 'home'"
return
;;
esac
}
function tolower() {
echo "$1" | awk '{print tolower($0)}'
}