@@ -52,6 +52,16 @@ def halt_and_catch_fire(message, *args):
5252
5353url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON' .format (secrets ['zip' ])
5454
55+ def extract_hour (date_time ):
56+ split_date_time = date_time .split ()
57+ hour = split_date_time [1 ]
58+ suffix = split_date_time [2 ]
59+ if hour [0 ] == '0' :
60+ hour = hour [1 ]
61+ return '\n ' .join ([hour , suffix ])
62+
63+ def extract_date (date_time ):
64+ return ' ' .join (date_time .split ('/' )[0 :2 ])
5565
5666# Initialize the pyportal object and let us know what data to fetch and where
5767# to display it
@@ -62,33 +72,43 @@ def halt_and_catch_fire(message, *args):
6272 text_position = (20 , 60 ),
6373 text_color = 0xFFFFFF ,
6474 text_wrap = 35 ,
65- caption_text = 'UV Index' ,
66- caption_font = CAPTION_FONT_FILE ,
67- caption_position = (5 , 20 ),
68- caption_color = 0x000000 )
69-
70- def extract_hour (date_time ):
71- return '\n ' .join (date_time .split ()[1 :3 ])
72-
73- data = [{'hour' : extract_hour (d ['DATE_TIME' ]), 'value' : d ['UV_VALUE' ]} for d in json .loads (pyportal .fetch ()) if d ['UV_VALUE' ] > 0 ]
75+ caption_font = CAPTION_FONT_FILE )
7476
75- number_of_readings = len (data )
76-
77- whitespace = (number_of_readings - 1 ) * SPACE_BETWEEN_BARS + 2 * MARGIN
78- bar_width = (320 - whitespace ) // number_of_readings
79-
80- max_reading = max ([d ['value' ] for d in data ])
81-
82- canvas = displayio .Group (max_size = 24 )
77+ canvas = displayio .Group (max_size = 36 )
8378pyportal .splash .append (canvas )
8479hour_font = bitmap_font .load_font (HOUR_FONT_FILE )
8580
86- for i , reading in enumerate (data ):
87- bar_height = (MAX_BAR_HEIGHT * reading ['value' ]) // max_reading
88- x = MARGIN + i * (bar_width + SPACE_BETWEEN_BARS )
89- canvas .append (Rect (x , 200 - bar_height , bar_width , bar_height , fill = COLORS [reading ['value' ]]))
90- text = Label ( hour_font , x = x , y = 220 , text = reading ['hour' ], color = 0x000000 , line_spacing = 0.8 )
91- canvas .append (text )
92-
9381while True :
94- pass
82+ raw_data = json .loads (pyportal .fetch ())
83+ data = [{'hour' : extract_hour (d ['DATE_TIME' ]), 'value' : int (d ['UV_VALUE' ])}
84+ for d in raw_data
85+ if d ['UV_VALUE' ] > 0 ]
86+ the_day = raw_data [0 ]['DATE_TIME' ]
87+ pyportal .set_caption ('UV Index for {0}' .format (extract_date (the_day )),
88+ (80 , 20 ),
89+ 0x000000 )
90+ number_of_readings = len (data )
91+ whitespace = (number_of_readings - 1 ) * SPACE_BETWEEN_BARS + 2 * MARGIN
92+ bar_width = (320 - whitespace ) // number_of_readings
93+ max_reading = max ([d ['value' ] for d in data ])
94+
95+ while len (canvas ) > 0 :
96+ canvas .pop ()
97+
98+ for i , reading in enumerate (data ):
99+ bar_height = (MAX_BAR_HEIGHT * reading ['value' ]) // max_reading
100+ x = int (MARGIN + i * (bar_width + SPACE_BETWEEN_BARS ))
101+ canvas .append (Rect (x , 200 - bar_height ,
102+ bar_width , bar_height ,
103+ fill = COLORS [reading ['value' ]]))
104+ canvas .append (Label (hour_font ,
105+ x = x + 3 , y = 220 ,
106+ text = reading ['hour' ],
107+ color = 0x000000 ,
108+ line_spacing = 0.6 ))
109+ canvas .append (Label (hour_font ,
110+ x = x + (bar_width // 2 )- 4 , y = 208 - bar_height ,
111+ text = str (reading ['value' ]),
112+ color = 0x000000 ))
113+
114+ time .sleep (3600 ) #refresh hourly
0 commit comments