4141cwd = ("/" + __file__ ).rsplit ('/' , 1 )[0 ]
4242
4343CAPTION_FONT_FILE = cwd + '/fonts/Helvetica-Bold-16.bdf'
44- TEXT_FONT_FILE = cwd + '/fonts/Helvetica-Bold-16.bdf'
45- HOUR_FONT_FILE = cwd + '/fonts/Arial-Bold-12.bdf'
46-
47- def halt_and_catch_fire (message , * args ):
48- """Log a critical error and stall the system."""
49- print (message % args )
50- while True :
51- pass
44+ BAR_FONT_FILE = cwd + '/fonts/Arial-Bold-12.bdf'
5245
5346#pylint:disable=line-too-long
5447url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON' .format (secrets ['zip' ])
5548#pylint:enable=line-too-long
5649
5750def extract_hour (date_time ):
51+ """Extract the hour in a format to use for display:
52+ :param date_time: the timestamp from EPA UV readings
53+ """
5854 split_date_time = date_time .split ()
5955 hour = split_date_time [1 ]
6056 suffix = split_date_time [2 ]
@@ -63,25 +59,32 @@ def extract_hour(date_time):
6359 return '\n ' .join ([hour , suffix ])
6460
6561def extract_date (date_time ):
62+ """Extract the date in a format to use for display:
63+ :param date_time: the timestamp from EPA UV readings
64+ """
6665 return ' ' .join (date_time .split ('/' )[0 :2 ])
6766
68- # Initialize the pyportal object and let us know what data to fetch and where
69- # to display it
7067pyportal = PyPortal (url = url ,
7168 status_neopixel = board .NEOPIXEL ,
7269 default_bg = 0xFFFFFF ,
73- text_font = TEXT_FONT_FILE ,
74- text_position = (20 , 60 ),
75- text_color = 0xFFFFFF ,
76- text_wrap = 35 ,
7770 caption_font = CAPTION_FONT_FILE )
7871
7972canvas = displayio .Group (max_size = 36 )
8073pyportal .splash .append (canvas )
81- hour_font = bitmap_font .load_font (HOUR_FONT_FILE )
74+ bar_font = bitmap_font .load_font (BAR_FONT_FILE )
8275
8376while True :
84- raw_data = json .loads (pyportal .fetch ())
77+ json_payload = ''
78+ try :
79+ json_payload = pyportal .fetch ()
80+ raw_data = json .loads (json_payload )
81+ except (ValueError , RuntimeError ) as ex :
82+ print ('Error: ' , ex )
83+ if isinstance (ex , ValueError ):
84+ print ('JSON:' , json_payload )
85+ print ('Retrying in 10 minutes' )
86+ time .sleep (600 )
87+ continue
8588 data = [{'hour' : extract_hour (d ['DATE_TIME' ]), 'value' : int (d ['UV_VALUE' ])}
8689 for d in raw_data
8790 if d ['UV_VALUE' ] > 0 ]
@@ -103,12 +106,12 @@ def extract_date(date_time):
103106 canvas .append (Rect (x , 200 - bar_height ,
104107 bar_width , bar_height ,
105108 fill = COLORS [reading ['value' ]]))
106- canvas .append (Label (hour_font ,
109+ canvas .append (Label (bar_font ,
107110 x = x + 3 , y = 220 ,
108111 text = reading ['hour' ],
109112 color = 0x000000 ,
110113 line_spacing = 0.6 ))
111- canvas .append (Label (hour_font ,
114+ canvas .append (Label (bar_font ,
112115 x = x + (bar_width // 2 )- 4 , y = 208 - bar_height ,
113116 text = str (reading ['value' ]),
114117 color = 0x000000 ))
0 commit comments