/
LIFXfun.py
executable file
·130 lines (103 loc) · 3.46 KB
/
LIFXfun.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
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
#!/usr/bin/env python
import requests
import json
import argparse
import time, datetime
import math
import sys
import ppgplot
token = "c714e99751f3b6c8c2bb9a51c1a938a3962b56cfb2fd98057f5b36dc71d3052d"
headers = {
"Authorization": "Bearer %s" % token,
}
def sinecurve(time, period):
periodSeconds = period * 60.
omega = 2*math.pi/periodSeconds
return 0.5 + 0.5 * math.sin(omega*time) + 0.2 * math.sin(3*omega*time)
class lightCurve:
def __init__(self):
self.time = []
self.brightness = []
self.period = 0
self.numPoints = 0
def initValues(self, time, brightness):
if len(time) != len(brightness):
return -1
self.time = time
self.brightness = brightness
self.numPoints = len(time)
return self.numPoints
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Downloads email list and puts it into a Google doc.')
parser.add_argument('--test', action='store_true', help='Use the test URL instead of the real URL.')
arg = parser.parse_args()
response = requests.get('https://api.lifx.com/v1beta1/lights/all', headers=headers)
bulbs = json.loads(response.text)
print response.text
print len(bulbs), "bulbs are registered."
numPoints = 60
period = 0.5
seconds = 0
step = period * 60. / numPoints
times = []
brightness = []
for x in range(numPoints):
times.append(seconds)
brightness.append(sinecurve(seconds, period))
seconds+= step
lightCurve = lightCurve()
lightCurve.initValues(times, brightness)
lightCurve.period = period * 60.
lightCurve.brightness[3] = 1.0
lightCurvePlot = {}
lightCurvePlot['pgplotHandle'] = ppgplot.pgopen('/xs')
ppgplot.pgpap(8, 0.618)
ppgplot.pgenv(0., lightCurve.period, 0.0, 1.0, 0, 0)
ppgplot.pglab("seconds", "brightness", "Light curve")
ppgplot.pgpt(lightCurve.time, lightCurve.brightness, 2)
ppgplot.pgsci(2)
ppgplot.pgline(lightCurve.time, lightCurve.brightness)
ppgplot.pgask(False)
connectedCount = 0
connectedBulbs = []
for b in bulbs:
print b
if b['connected'] == True:
connectedCount+= 1
connectedBulbs.append(b)
print b['label'], "is connected!"
print "Number of connected bulbs", connectedCount
params = {
"color": "kelvin:2700",
"power": "on",
"brightness:":"0"
}
response = requests.put("https://api.lifx.com/v1beta1/lights/all/state", headers=headers, params=params)
time.sleep(3)
for p in range(500):
for n in range(lightCurve.numPoints):
if n == lightCurve.numPoints-1:
timeToNextStep = lightCurve.period - lightCurve.time[n]
else:
timeToNextStep = lightCurve.time[n+1] - lightCurve.time[n]
print n, lightCurve.time[n], lightCurve.brightness[n], timeToNextStep
ppgplot.pgeras()
ppgplot.pgsci(1)
ppgplot.pgenv(0., lightCurve.period, 0.0, 1.0, 0, 0)
ppgplot.pglab("seconds", "brightness", "Light curve")
ppgplot.pgpt(lightCurve.time, lightCurve.brightness, 2)
ppgplot.pgsci(2)
ppgplot.pgline(lightCurve.time, lightCurve.brightness)
ppgplot.pgsci(3)
ppgplot.pgpt([lightCurve.time[n]], [lightCurve.brightness[n]], 3)
currentLineStyle = ppgplot.pgqls()
ppgplot.pgsls(2)
ppgplot.pgline([lightCurve.time[n], lightCurve.time[n]], [ 0, 1])
ppgplot.pgsls(currentLineStyle)
params = {"brightness": str(lightCurve.brightness[n]), "duration": str(timeToNextStep)}
print "Sending params:", params
response = requests.put("https://api.lifx.com/v1beta1/lights/all/state", headers=headers, params=params)
# print response.text
time.sleep(timeToNextStep)
ppgplot.pgclos()
sys.exit()