Refactor to generate the Widgets

This commit is contained in:
nemunaire 2022-12-27 21:27:25 +01:00
commit 0644bdc68f
2 changed files with 125 additions and 65 deletions

86
main.py
View file

@ -27,6 +27,7 @@
from datetime import datetime, timedelta
import io
import locale
import logging
import os.path
import time
@ -34,39 +35,52 @@ locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
from PIL import Image, ImageDraw, ImageFont
class WidgetPlacement:
def __init__(self, module, *args, size, position, **kwargs):
self.module = module
self.args = args
self.size = size
self.position = position
self.kwargs = kwargs
def main(only_on_coming_evt=False):
image = Image.new('1', (480, 800), 255)
#image = Image.new('L', (480, 800), 'white')
draw = ImageDraw.Draw(image)
shape = []
import modules
config = modules.Config()
# alerts
alerts = []
# Weather
# Current Weather
from modules.weather import WeatherJumboCurrentModule
image.paste(WeatherJumboCurrentModule().draw_module(config, 480, 150), (0, 0))
shape.append(WidgetPlacement(WeatherJumboCurrentModule, size=(480,150), position=(0,0)))
# rule
image.paste(modules.RuleModule().draw_module(config, 450, 1), (30, 142))
shape.append(WidgetPlacement(modules.RuleModule, size=(450, 1), position=(30, 142)))
# pluie
from modules.weather import WeatherRainModule
image.paste(WeatherRainModule().draw_module(config, 480-int(480/1.6), 94), (0, 155))
shape.append(WidgetPlacement(WeatherRainModule, size=(480-int(480/1.6), 94), position=(0, 155)))
# moon phase
from modules.weather import WeatherMoonPhaseModule
moon = WeatherMoonPhaseModule().draw_module(config, 65, 65)
image.paste(moon, (0, 113), moon)
shape.append(WidgetPlacement(WeatherMoonPhaseModule, size=(65, 65), position=(0, 113)))
# ical
from modules.ical import IcalModule
ical = IcalModule(config)
cal = ical.draw_module(config, 480-int(480/1.6), 255)
image.paste(cal, (0, 250))
shape.append(WidgetPlacement(IcalModule, config, size=(480-int(480/1.6), 255), position=(0, 250)))
occuped_space = 0
ical = IcalModule(config)
evt_coming = ical.non_local_event_coming(config) or ical.local_event_ending(config)
if evt_coming:
from modules.ratp import RATPNextStopModule
@ -80,25 +94,51 @@ def main(only_on_coming_evt=False):
if occuped_space < 250:
# weekly weather
from modules.weather import WeeklyWeatherModule
image.paste(WeeklyWeatherModule().draw_module(config, int(480/1.6), 275), (480-int(480/1.6), 255 + occuped_space))
shape.append(WidgetPlacement(WeeklyWeatherModule, size=(int(480/1.6), 275), position=(480-int(480/1.6), 255 + occuped_space)))
# 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), 155))
shape.append(WidgetPlacement(RATPWeatherModule, size=(int(480/1.6), 94), position=(480-int(480/1.6), 155)))
alerts.append(RATPWeatherModule)
# Toolbar
from modules.weather import WeatherToolbarModule
image.paste(WeatherToolbarModule().draw_module(config, 480, 50), (0, 530))
shape.append(WidgetPlacement(WeatherToolbarModule, size=(480, 50), position=(0, 530)))
# alerts
alerts = []
alerts += [a for a in RATPWeatherModule().gen_alerts()]
from modules.sncf import SNCFWeatherModule
alerts += SNCFWeatherModule().gen_alerts("normandie")
alerts.append({"module": SNCFWeatherModule, "args": "normandie"})
from modules.weather import WeatherAlerts
alerts += WeatherAlerts().gen_alerts()
alerts.append(WeatherAlerts)
# sunrise/set
from modules.weather import WeatherSunModule
shape.append(WidgetPlacement(WeatherSunModule, size=(int(480/2), 20), position=(0, 780), start_align=5))
# Draw shapes
last_height = 0
last_y = 0
for s in shape:
try:
x,y = s.position
if y < 0:
y = last_height + last_y
width, height = s.size
if height < 0:
height = 480 - last_height - last_y
img = s.module(*s.args).draw_module(config, width, height, **s.kwargs)
if img.mode == "RGBA":
image.paste(img, (x,y), img)
else:
image.paste(img, (x,y))
except BaseException as e:
logging.exception(e)
alerts.append({
"title": "Impossible de dessiner " + s.module.__name__,
"description": type(e).__name__ + ": " + (e.message if hasattr(e, 'message') else str(e)),
"icon": "wi-earthquake.png",
})
from modules import AlertsModule
mod = AlertsModule(alerts).draw_module(config, 480, 330)
@ -116,10 +156,6 @@ def main(only_on_coming_evt=False):
else:
image.paste(WeatherTemperatureModule().draw_module(config, 480, 200), (0, 580))
# sunrise/set
from modules.weather import WeatherSunModule
image.paste(WeatherSunModule().draw_module(config, int(480/2), 20, start_align=5), (0, 780))
fnt = ImageFont.truetype(config.fnt_R_path, 11)
draw.text(
(475, 798),
@ -127,17 +163,17 @@ def main(only_on_coming_evt=False):
fill="black", anchor="rb", font=fnt
)
logging.info("image generated")
try:
import epd7in5
#print("image generated")
epd = epd7in5.EPD()
epd.init()
#print("initialized")
logging.info("initialized")
epd.display(epd.getbuffer(image))
#print("time to sleep")
logging.info("time to sleep")
epd.sleep()
except: