-
Notifications
You must be signed in to change notification settings - Fork 0
/
DashBulb.py
92 lines (74 loc) · 3.05 KB
/
DashBulb.py
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
from scapy.all import *
import subprocess
from flux_led import WifiLedBulb, BulbScanner
import datetime
import os
import sys
version = "1.0.3 " # assign version number for debugging
bulb = WifiLedBulb('192.168.XX.XX') # assign bulb IP
#bulb.refreshState() # refresh state to collect accurate staus
logfile = r"/home/pi/SANDBOX/Dash/logfiles.log" # name of my log file
WHITE_MAC_ADDRESS = 'XX:XX:XX:XX:XX:XX' # enter Dash Button's MAC Address here.
testMAC = 'XX:XX:XX:XX:XX:XX'
dashButton = {
"whiteDash" : 'XX:XX:XX:XX:XX:XX',
"redDash" : 'XX:XX:XX:XX:XX:XX'
}
whiteBulb = (0,0,0,191)
redBulb = (15,0,0,0)
#sniff for packets sent from button press
def detect_button(pkt):
for mac in dashButton:
if pkt.haslayer(DHCP) and pkt[Ether].src == dashButton[mac]:
#TODO: use switch case instead
write_log("Button pressed: ", logfile)
bulb.refreshState()
#write_log (bulb.getRgbw)
currentColor = bulb.getRgbw()
#write_log(currentColor, logfile)
if currentColor == whiteBulb:
if mac == "whiteDash":
write_log("White bulb shut off: ", logfile)
bulb.setRgb(0,0,0)
bulb.refreshState()
time.sleep(2)
elif mac == "redDash":
write_log("Red bulb set: ", logfile)
bulb.setRgb(15,0,0)
bulb.refreshState()
time.sleep(2)
elif currentColor == redBulb:
if mac == "redDash":
write_log("Red bulb shut off: ", logfile)
bulb.setRgb(0,0,0)
bulb.refreshState()
time.sleep(2)
elif mac == "whiteDash":
write_log("White bulb set: ", logfile)
bulb.setWarmWhite(75)
bulb.refreshState()
time.sleep(2)
else:
if mac == "redDash":
write_log("Red bulb set: ", logfile)
bulb.setRgb(15,0,0)
bulb.refreshState()
time.sleep(2)
elif mac == "whiteDash":
write_log("White bulb set: ", logfile)
bulb.setWarmWhite(75)
bulb.refreshState()
time.sleep(2)
return
#record actions to logfile for later debugging
def write_log(text, file):
f = open(file, 'a') # 'a' will append to an existing file if it exists
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
f.write("{}\n".format(text + timestamp)) # write the text to the logfile and move to next line
return
write_log("Running version:"+version, logfile)
#loops forever sniffing traffic and call the detect function
#Sniff can run forever but would hit two packets at a time.
#Using While loop and 1 packet filter to loop forever but only grab first packet
while True:
sniff( count=1, prn=detect_button, filter="(udp and (port 67 or 68))", store=0)