timestamp = time.localtime() now = time.struct_time(( timestamp[0], timestamp[1], timestamp[2], 0, 0, 0, # we don't track seconds -1, -1, False)) # we dont know day of week/year or DST print("Current time:", now) remaining = time.mktime(event_time) - time.mktime(now) print("Time remaining (s):", remaining) if remaining == 0: # oh, its event time! pyportal.set_background(event_background) countdown_text.text = '' pyportal.set_caption('', (13, 215), 0x000000) while True: # that's all folks pass days_remaining = remaining // 86400 print("%d days remaining" % (days_remaining)) countdown_text.text = '{:>3}'.format(days_remaining) pyportal.set_background(backgrounds[random.randint(0, len(backgrounds) - 1)]) # update every minute time.sleep(60)
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']]))
worst = 0 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 while len(canvas) > 0: canvas.pop() pyportal.set_caption(('Air Quality Index'), (70, 20), 0xFFFFFF) xpos = 20 ypos = 80 print('Name\tAQI\tCat.\tCatNum') for item in raw_data: print("{}\t{}\t{}\t{}".format(item['ParameterName'], item['AQI'], item['Category']['Name'], item['Category']['Number'])) aqidata = str(item['ParameterName']) + ': ' + str( item['AQI']) + ' ' + str(item['Category']['Name']) aqicolor = (COLORS[item['Category']['Number']]) aqi_label = Label(AQI_font, text=aqidata, color=aqicolor, x=xpos,
) while True: try: value = pyportal.fetch() print("Response is", value) if 0 <= value <= 50: pyportal.set_background(0x66bb6a) # good if 51 <= value <= 100: pyportal.set_background(0xffeb3b) # moderate if 101 <= value <= 150: pyportal.set_background(0xf39c12) # sensitive if 151 <= value <= 200: pyportal.set_background(0xff5722) # unhealthy if 201 <= value <= 300: pyportal.set_background(0x8e24aa) # very unhealthy if 301 <= value <= 500: pyportal.set_background(0xb71c1c) # hazardous if AVG_LAT and AVG_LONG: pyportal.set_caption( 'AQI for ({0:5.2f}, {1:6.2f})'.format(AVG_LAT, AVG_LONG), (15, 220), 0x000000) except ValueError as e: # Possibly PurpleAir is having load problems. # See https://github.com/e28eta/pyportal-aqi/issues/1 print("ValueError occurred, retrying! -", e) except RuntimeError as e: print("Some error occured, retrying! -", e) time.sleep(10 * 60) # wait 10 minutes before getting again