-
Notifications
You must be signed in to change notification settings - Fork 769
/
code.py
127 lines (108 loc) · 4.06 KB
/
code.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
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
PyPortal UV Index display
Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!
Written by Dave Astels for Adafruit Industries
Copyright (c) 2019 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.
"""
import time
import json
import board
import displayio
from adafruit_pyportal import PyPortal
from adafruit_display_shapes.rect import Rect
from adafruit_display_text.Label import Label
from adafruit_bitmap_font import bitmap_font
try:
from secrets import secrets
except ImportError:
print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
raise
MAX_BAR_HEIGHT = 160
MARGIN = 10
SPACE_BETWEEN_BARS = 1
COLORS = [0x00FF00, 0x83C602, 0xa2CF02,
0xF7DE03, 0xF6B502, 0xF78802,
0xF65201, 0xEA2709,
0xDA0115, 0xFC019E, 0xB548FF,
0x988FFE, 0x7EA7FE, 0x66BFFD, 0x4BD9FF]
cwd = ("/"+__file__).rsplit('/', 1)[0]
CAPTION_FONT_FILE = cwd+'/fonts/Helvetica-Bold-16.bdf'
BAR_FONT_FILE = cwd+'/fonts/Arial-Bold-12.bdf'
#pylint:disable=line-too-long
url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON'.format(secrets['zip'])
#pylint:enable=line-too-long
def extract_hour(date_time):
"""Extract the hour in a format to use for display:
:param date_time: the timestamp from EPA UV readings
"""
split_date_time = date_time.split()
hour = split_date_time[1]
suffix = split_date_time[2]
if hour[0] == '0':
hour = hour[1]
return '\n'.join([hour, suffix])
def extract_date(date_time):
"""Extract the date in a format to use for display:
:param date_time: the timestamp from EPA UV readings
"""
return ' '.join(date_time.split('/')[0:2])
pyportal = PyPortal(url=url,
status_neopixel=board.NEOPIXEL,
default_bg=0xFFFFFF,
caption_font=CAPTION_FONT_FILE)
canvas = displayio.Group()
pyportal.splash.append(canvas)
bar_font = bitmap_font.load_font(BAR_FONT_FILE)
while True:
json_payload = ''
try:
json_payload = pyportal.fetch()
raw_data = json.loads(json_payload)
except (ValueError, RuntimeError) as ex:
print('Error: ', ex)
if isinstance(ex, ValueError):
print('JSON:', json_payload)
print('Retrying in 10 minutes')
time.sleep(600)
continue
data = []
for d in raw_data:
if d['UV_VALUE'] > 0:
entry = {}
entry['hour'] = extract_hour(d['DATE_TIME'])
entry['value'] = int(d['UV_VALUE'])
data.append(entry)
the_day = raw_data[0]['DATE_TIME']
pyportal.set_caption('UV Index for {0}'.format(extract_date(the_day)),
(80, 20),
0x000000)
number_of_readings = len(data)
whitespace = (number_of_readings - 1) * SPACE_BETWEEN_BARS + 2 * MARGIN
bar_width = (320 - whitespace) // number_of_readings
max_reading = max([d['value'] for d in data])
while len(canvas) > 0:
canvas.pop()
for i, reading in enumerate(data):
bar_height = (MAX_BAR_HEIGHT * reading['value']) // max_reading
x = int(MARGIN + i * (bar_width + SPACE_BETWEEN_BARS))
canvas.append(Rect(x, 200 - bar_height,
bar_width, bar_height,
fill=COLORS[reading['value']]))
canvas.append(Label(bar_font,
x=x+3, y=220,
text=reading['hour'],
color=0x000000,
line_spacing=0.6))
canvas.append(Label(bar_font,
x=x+(bar_width//2)-4, y=208-bar_height,
text=str(reading['value']),
color=0x000000))
time.sleep(3600) #refresh hourly