-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue-toggle.py.bak.6.24.2016
327 lines (291 loc) · 6.88 KB
/
hue-toggle.py.bak.6.24.2016
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
from scapy.all import *
import requests,json
import MySQLdb
import time
import datetime
bridgeIP = "192.168.1.2" #IP Address of your Bridge
user = "VaA-1-uG7zUmFzZHhXIjinA-I-Dr2IUjvtz-O9Pj" #Username you generated
# tideBtn = "10:0d:7f:79:46:86" #MAC address of your Dash button
tideBtn = "f0:27:2d:a7:db:b3"
allBtn = "74:c2:46:a8:f3:b8"
slimjimBtn = "74:c2:46:b2:88:4d"
gatoradeBtn = "f0:27:2d:a2:2c:f1"
# lightID = "3" #ID of the light you want to control
lightIDs = ["1", "2", "3"]
# "Adam-Bookshelf" Is light ID: 1
# "Adam-Overhang" Is light ID: 3
# "Adam-Nightstand" Is light ID: 2
firstState = {
'1': {
# 'on':True,
'hue':47125,
'colormode':'xy',
'effect':'none',
'alert':'none',
'xy':[
0.1684,
0.0416
],
'reachable':True,
'bri':37,
'ct':153,
'sat':253
},
'2': {
# 'on':True,
'hue':64488,
'colormode':'xy',
'effect':'none',
'alert':'none',
'xy':[
0.6451,
0.3062
],
'reachable':True,
'bri':38,
'ct':153,
'sat':253
},
'3': {
# 'on':True,
'hue':47125,
'colormode':'xy',
'effect':'none',
'alert':'none',
'xy':[
0.1684,
0.0416
],
'reachable':True,
'bri':37,
'ct':153,
'sat':253
}
}
midState = {
'1': {
# 'on':True,
'bri':48,
'hue':566,
'sat':228,
'effect':'none',
'xy':[
0.6395,
0.3315
],
'ct':153,
'alert':'none',
'colormode':'xy',
'reachable':True
},
'2': {
# 'on':True,
'bri':128,
'hue':44173,
'sat':252,
'effect':'none',
'xy':[
0.2014,
0.1073
],
'ct':153,
'alert':'none',
'colormode':'xy',
'reachable':True
},
'3': {
# 'on':True,
'bri':136,
'hue':54526,
'sat':209,
'effect':'none',
'xy':[
0.3732,
0.1931
],
'ct':239,
'alert':'none',
'colormode':'xy',
'reachable':True
}
}
brightState = {
'1': {
# 'on':True,
'bri':254,
'hue':33129,
'sat':52,
'effect':'none',
'xy':[
0.3689,
0.3714
],
'ct':231,
'alert':'none',
'colormode':'xy',
'reachable':True
},
'2': {
# 'on':True,
'bri':254,
'hue':33129,
'sat':52,
'effect':'none',
'xy':[
0.3689,
0.3714
],
'ct':231,
'alert':'none',
'colormode':'xy',
'reachable':True
},
'3': {
# 'on':True,
'bri':254,
'hue':33129,
'sat':52,
'effect':'none',
'xy':[
0.3689,
0.3714
],
'ct':231,
'alert':'none',
'colormode':'xy',
'reachable':True
}
}
#-----------------
# database stuff
#-----------------
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="robot", # your username
passwd="r0b0t!", # your password
db="21newbury") # name of the data base
cur=db.cursor()
db.close()
def connectDB():
global db
global cur
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="robot", # your username
passwd="r0b0t!", # your password
db="21newbury") # name of the data base
cur=db.cursor()
def closeDB():
global db
db.close()
def insertMsgInDb(ip, mac, os=""):
cur.execute("INSERT INTO dash_watch (ip, mac, os) VALUES (\'" + str(ip) + "\', \'" + str(mac) + "\', \'" + str(os) + "\')")
#-----------------
# light states
#-----------------
def all_off():
# print "\tall_off()"
for lightID in lightIDs:
url = "http://"+bridgeIP+"/api/"+user+"/lights/"+lightID
r = requests.put(url+"/state",json.dumps({'bri': 0}))
r = requests.put(url+"/state",json.dumps({'on':False}))
def set_state(state):
# print "\tset_state(" + str(state) + ")"
for lightID in lightIDs:
url = "http://"+bridgeIP+"/api/"+user+"/lights/"+lightID
s = requests.put(url+"/state",json.dumps({'on':True}))
r = requests.put(url+"/state",json.dumps(state[lightID]))
# s = requests.put(url+"/state",json.dumps({'on':True}))
#-----------------
# light control
#-----------------
def get_light_state(id):
url = "http://"+bridgeIP+"/api/"+user+"/lights/"+id
r = requests.get(url)
data = json.loads(r.text)
return data
def test_light_state(id, scene):
data = get_light_state(id)
state = data["state"]
thresh = 5
# print "id: " + id
# print "test - hue: " + str(abs(state["hue"] - scene[id]["hue"]))
# print "*state: " + str(state["hue"])
# print "*scene: " + str(scene[id]["hue"])
# print "test - sat: " + str(abs(state["sat"] - scene[id]["sat"]))
# print "*state: " + str(state["sat"])
# print "*scene: " + str(scene[id]["sat"])
# print "test - bri: " + str(abs(state["bri"] - scene[id]["bri"]))
# print "*state: " + str(state["bri"])
# print "*scene: " + str(scene[id]["bri"])
hue = True if abs(state["hue"] - scene[id]["hue"]) < (thresh *3) else False
sat = True if abs(state["sat"] - scene[id]["sat"]) < thresh else False
bri = True if abs(state["bri"] - scene[id]["bri"]) < thresh else False
result = hue and sat and bri
return result
def toggle_low():
first = True
for lightID in lightIDs:
data = get_light_state(lightID)
if data["state"]["on"] == True:
if first != False:
first = test_light_state(lightID, firstState)
if first == True:
print "\t*****set state off"
all_off()
else:
print "\t*****set state first"
set_state(firstState)
def toggle_bright():
mid = True
bright = True
for lightID in lightIDs:
data = get_light_state(lightID)
if data["state"]["on"] == True:
if mid != False:
mid = test_light_state(lightID, midState)
# print "\ttest mid-" + str(lightID) + ": " + str(mid)
if bright != False:
bright = test_light_state(lightID, brightState)
# print "\ttest bright-" + str(lightID) + ": " + str(bright)
if mid == False and bright == False:
print "\t*****set state mid"
set_state(midState)
if mid == True and bright == True:
print "\t*****set state mid"
set_state(midState)
if mid == True and bright == False:
print "\t*****set state bright"
set_state(brightState)
if mid == False and bright == True:
print "\t*****set state mid"
# all_off()
set_state(midState)
#-----------------
# packet sniffing
#-----------------
def arp_display(pkt):
print "*****"
print "pkt"
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print st
# print dir(pkt)
# print dir(pkt[ARP])
connectDB()
insertMsgInDb(pkt[ARP].psrc, pkt[ARP].hwsrc)
db.commit()
closeDB()
if pkt[ARP].op == 1:
print "*****"
print "\thwsrc: " + pkt[ARP].hwsrc
print "\tpsrc: " + pkt[ARP].psrc
# if pkt[ARP].psrc == '0.0.0.0': # ARP Probe
if pkt[ARP].hwsrc == slimjimBtn:
print "\tpkt[ARP].hwsrc == slimjimBtn"
toggle_low()
if pkt[ARP].hwsrc == gatoradeBtn:
print "\tpkt[ARP].hwsrc == gatoradeBtn"
toggle_bright()
print "start"
print sniff(prn=arp_display, filter="arp", store=0, count=0)
print "stop"