Alert on RATP failures

This commit is contained in:
nemunaire 2022-08-14 18:25:06 +02:00
parent 24f8c46ff6
commit 18ab123c3a
3 changed files with 53 additions and 24 deletions

33
main.py
View File

@ -36,7 +36,7 @@ from PIL import Image, ImageDraw, ImageFont
def main():
image = Image.new('1', (480, 800), 255)
#image = Image.new('RGB', (480, 800), 'white')
#image = Image.new('L', (480, 800), 'white')
draw = ImageDraw.Draw(image)
import modules
@ -44,47 +44,56 @@ def main():
# Weather
# Toolbar
from modules.weather import WeatherToolbarModule
image.paste(WeatherToolbarModule().draw_module(config, 480, 50), (0, 0))
# Current Weather
from modules.weather import WeatherJumboCurrentModule
image.paste(WeatherJumboCurrentModule().draw_module(config, 480, 150), (0, 50))
image.paste(WeatherJumboCurrentModule().draw_module(config, 480, 150), (0, 0))
# rule
image.paste(modules.RuleModule().draw_module(config, 450, 1), (30, 192))
image.paste(modules.RuleModule().draw_module(config, 450, 1), (30, 142))
# pluie
from modules.weather import WeatherRainModule
image.paste(WeatherRainModule().draw_module(config, 480-int(480/1.6), 94), (0, 205))
image.paste(WeatherRainModule().draw_module(config, 480-int(480/1.6), 94), (0, 155))
# moon phase
from modules.weather import WeatherMoonPhaseModule
moon = WeatherMoonPhaseModule().draw_module(config, 65, 65)
image.paste(moon, (0, 163), moon)
image.paste(moon, (0, 113), moon)
# weekly weather
from modules.weather import WeeklyWeatherModule
image.paste(WeeklyWeatherModule().draw_module(config, int(480/1.6), 275), (480-int(480/1.6), 305))
image.paste(WeeklyWeatherModule().draw_module(config, int(480/1.6), 275), (480-int(480/1.6), 255))
# RATP weather
from modules.ratp import RATPWeatherModule
ratp = RATPWeatherModule().draw_module(config, int(480/1.6), 94)
image.paste(ratp, (480-int(480/1.6), 205))
image.paste(ratp, (480-int(480/1.6), 155))
# Toolbar
from modules.weather import WeatherToolbarModule
image.paste(WeatherToolbarModule().draw_module(config, 480, 50), (0, 530))
# alerts
alerts = []
from modules.weather import WeatherAlerts
alerts += [a for a in RATPWeatherModule().gen_alerts()]
alerts += WeatherAlerts().gen_alerts()
from modules import AlertsModule
mod = AlertsModule(alerts).draw_module(config, 480, 200)
mod = AlertsModule(alerts).draw_module(config, 480, 330)
if mod.height > 260:
image.paste(mod, (0, 580-mod.height+67), mod)
elif mod.height < 100:
image.paste(mod, (0, 580-mod.height-40), mod)
else:
image.paste(mod, (0, 580-mod.height-5), mod)
# températures
from modules.weather import WeatherTemperatureModule
if mod.height > 260:
image.paste(WeatherTemperatureModule().draw_module(config, 480, 200 - mod.height+260), (0, 580 + mod.height-260))
else:
image.paste(WeatherTemperatureModule().draw_module(config, 480, 200), (0, 580))
# sunrise/set

View File

@ -79,22 +79,28 @@ class AlertsModule:
self.icon_size = 50
self.alerts = alerts
def draw_module(self, config, width, height):
def draw_module(self, config, width, height, font_size=16):
image = Image.new('RGBA', (width, height), "#000")
draw = ImageDraw.Draw(image)
align = 0
more_alerts = 0
if len(self.alerts) > 0:
fnt_R = ImageFont.truetype(config.fnt_R_path, 16)
fnt_B = ImageFont.truetype(config.fnt_RB_path, 16)
fnt_R = ImageFont.truetype(config.fnt_R_path, font_size)
fnt_B = ImageFont.truetype(config.fnt_RB_path, font_size)
align = 9
for alert in self.alerts:
if alert["icon"] is not None:
if callable(alert["icon"]):
icon_img = alert["icon"](size=int(font_size*self.icon_size/16))
image.paste(icon_img, (int(self.icon_size / 2 - font_size*self.icon_size/32), align), icon_img)
else:
color_img = Image.new('RGB', (self.icon_size, self.icon_size), "#fff")
icon_img = Image.open("icons/" + alert["icon"]).resize((self.icon_size, self.icon_size))
image.paste(color_img, (0, align - 5), icon_img)
image.paste(color_img, (0, align), icon_img)
if "title" in alert:
draw.text(
((self.icon_size if alert["icon"] is not None else 0) - 5, align),
alert["title"],
@ -102,11 +108,12 @@ class AlertsModule:
)
if "subtitle" in alert and alert["subtitle"]:
draw.text(
((self.icon_size if alert["icon"] is not None else 0) + fnt_B.getsize(alert["title"])[0], align + 3),
((self.icon_size if alert["icon"] is not None else 0) + (fnt_B.getsize(alert["title"])[0] if "title" in alert else 0), align + 3),
alert["subtitle"],
fill="white", anchor="lt", font=fnt_R
)
if "title" in alert:
align += fnt_B.getsize(alert["title"])[1]
align += display_longtext(
@ -117,8 +124,21 @@ class AlertsModule:
maxwidth=width-self.icon_size-5
)
align += 7
if align > height:
more_alerts += 1
image = image.crop((0,0,width, align))
image = image.crop((0,0,width, min(align, height)))
if more_alerts > 0 and font_size > 12:
return self.draw_module(config, width, height, font_size-1)
elif more_alerts > 0:
draw = ImageDraw.Draw(image)
draw.rectangle((0, height-font_size, width, height), fill="black")
draw.text(
(width - 3, height),
("%d alertes supplémentaires" if more_alerts > 1 else "%d alerte supplémentaire") % more_alerts,
fill="white", anchor="rb", font=fnt_B
)
return image

View File

@ -88,7 +88,7 @@ class WeatherJumboCurrentModule:
draw.text(
(width*self.middle_align, height/3),
"%d˚ %s." % (math.trunc(curweather["temperature"]), curweather["summary"]),
fill="black", anchor="lb", font=fnt_Big
fill="black", anchor="ld", font=fnt_Big
)
thisdayweather = WeatherAPI().get_daily()["data"][0]