Add air quality indicators

This commit is contained in:
nemunaire 2023-04-18 15:17:17 +02:00
parent 7b0a3dd5ac
commit e8fd423e5f
2 changed files with 77 additions and 1 deletions

View File

@ -98,7 +98,11 @@ def main(only_on_coming_evt=False, ignore_module=[], force_coming_event=True, ex
if occuped_space < 250:
# weekly weather
from modules.weather import WeeklyWeatherModule
shape.append(WidgetPlacement(WeeklyWeatherModule, size=(int(480/1.6), 275), position=(480-int(480/1.6), NEXT_STOP_Y + occuped_space + (5 if occuped_space else 0))))
shape.append(WidgetPlacement(WeeklyWeatherModule, size=(int(480/1.6), 165), position=(480-int(480/1.6), NEXT_STOP_Y + occuped_space + (5 if occuped_space else 0))))
# air quality
from modules.weather import WeatherAirQualityModule
shape.append(WidgetPlacement(WeatherAirQualityModule, size=(int(480/1.6), 100), position=(480-int(480/1.6), NEXT_STOP_Y + 165 + occuped_space + (5 if occuped_space else 0))))
# RATP weather
major_lines = ["M7", "M14", "RB", "TT3A"]

View File

@ -41,6 +41,44 @@ def draw_format_infos(infos, width, height, fnt_R, fnt_B, label_margin, align_he
return image
def draw_format_array(infos, width, height, fnt_R, fnt_B, label_margin, align_height=0, margin_bf_first=True, **kwargs):
image = Image.new('RGBA', (int(width), int(height)))
draw = ImageDraw.Draw(image)
nb_infos = len(infos.keys())
size = 0
title_hsize = 0
text_hsize = 0
for k,v in infos.items():
ksize,ksizeH = fnt_R.getsize(k)
vsize,vsizeH = fnt_B.getsize(v)
if title_hsize < ksizeH:
title_hsize = ksizeH
if text_hsize < vsizeH:
text_hsize = vsizeH
size += max(ksize,vsize)
size += label_margin
margin = (width - size) / nb_infos
align = 0
if margin_bf_first:
align += margin / 2
for k,v in infos.items():
size = max(fnt_R.getsize(k)[0],fnt_B.getsize(v)[0])
draw.text(
(align + (0 if "anchor" not in kwargs or kwargs["anchor"][0] != "m" else size/2), align_height-title_hsize/2),
k,
font=fnt_R, **kwargs
)
draw.text(
(align + (0 if "anchor" not in kwargs or kwargs["anchor"][0] != "m" else size/2), align_height+text_hsize/2),
v,
font=fnt_B, **kwargs
)
align += size + margin
return image
class WeatherToolbarModule:
def __init__(self):
@ -349,3 +387,37 @@ class WeatherAlerts:
})
return alerts
class WeatherAirQualityModule:
def __init__(self):
self.label_margin = 3
def draw_module(self, config, width, height):
image = Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(image)
fnt_R = ImageFont.truetype(config.fnt_R_path, 16)
fnt_B = ImageFont.truetype(config.fnt_RB_path, 20)
weather = WeatherAPI().get_currently()
infos1 = {
"CO": "%d μg/m³" % weather["air_quality"]["co"],
"O3": "%d μg/m³" % (weather["air_quality"]["o3"]),
"NO2": "%d μg/m³" % (weather["air_quality"]["no2"]),
}
infos2 = {
"SO2": "%d μg/m³" % (weather["air_quality"]["so2"]),
"PM2.5": "%d μg/m³" % (weather["air_quality"]["pm2_5"]),
"PM10": "%d μg/m³" % (weather["air_quality"]["pm10"]),
}
txt = draw_format_array(infos1, width, height/2, fnt_R, fnt_B, self.label_margin, align_height=height/4, anchor="mm", fill="black")
image.paste(txt, (0,0), txt)
txt = draw_format_array(infos2, width, height/2, fnt_R, fnt_B, self.label_margin, align_height=height/4, anchor="mm", fill="black")
image.paste(txt, (0,int(height/2)), txt)
return image