-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.py
executable file
·106 lines (90 loc) · 2.75 KB
/
rest.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
#!/usr/bin/env python
import web
import json
import time
import metoffer
from BlinkyTape import BlinkyTape
from datetime import datetime
bb = BlinkyTape('/dev/ttyACM0')
urls = (
'/colour', 'show_colour',
'/pattern', 'show_pattern',
'/weather', 'show_weather',
)
app = web.application(urls, globals())
class show_weather:
def GET(self):
temps = getTemperatures()
x = 0
for i in temps:
if (datetime.utcnow().day != i["timestamp"][0].day or i["timestamp"][0].hour > datetime.utcnow().hour-3):
colour = convertTemperatureToRgb(i["Temperature"][0])
bb.sendPixel(colour[0],colour[1],colour[2])
x = x + 1
for i in range(x,60):
bb.sendPixel(0,0,0)
bb.show()
return 'status: "success"'
class show_colour:
def POST(self):
data = web.data()
j = json.loads(data)
red = j['red']
green = j['green']
blue = j['blue']
bb.displayColor(red, green, blue)
return 'status: "success"'
class show_pattern:
def POST(self):
data = web.data()
j = json.loads(data)
pixel_count = len(j)
for i in range(0,60):
if (i >= pixel_count):
bb.sendPixel(0,0,0)
else:
bb.sendPixel(j[i]['red'],j[i]['green'],j[i]['blue'])
bb.show()
return 'status: "success"'
if __name__ == "__main__":
app.run()
RGB_OFFSET = 25
def getTemperatures():
print "getTemperatures start"
api_key = '35d53d57-9aa0-45de-9772-0871f0ceffda'
M = metoffer.MetOffer(api_key)
x = M.nearest_loc_forecast(52.2793,-1.5803, metoffer.THREE_HOURLY)
y = metoffer.parse_val(x)
print "data retrieved"
for i in y.data:
print("{} - {}".format(i["timestamp"][0].strftime("%d %b, %H:%M"), i["Temperature"][0]))
print "getTemperatures end"
return y.data
def increasingColourOffset(offset):
return offset * RGB_OFFSET
def decreasingColourOffset(offset):
return 255 - increasingColourOffset(offset)
#20 10 0 10 20 30 40
def convertTemperatureToRgb(temperature):
if (temperature <= -15):
return [0,0,255]
elif (temperature <= -5):
# -20 < t <= -5
g = increasingColourOffset(temperature + 15)
return [0,g,255]
elif (temperature <= 5):
# -5 < t <= 10
b = decreasingColourOffset(temperature + 5)
return [0,255,b]
elif (temperature <= 15):
# 10 < t <= 25
r = increasingColourOffset(temperature - 5)
return [r,255,0]
elif (temperature <= 25):
# 25 < t <= 40
g = decreasingColourOffset(temperature - 15)
return [255,g,0]
else:
# 40 < t
return [255,0,0]
return [0,0,0]