Harden main loop and store; by-id device default, BOARD_TZ

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Sonnet 5.5 committed 2026-10-07 00:55:46 +02:00
1 parent 8649d3abd0
commit 44a1b3cf8c
7 files changed
+100 -30

No files matched your search

+43 -20
View File
@@ -7,6 +7,7 @@ import os
import signal
import sys
import time
from datetime import timedelta
from pathlib import Path
LIB = os.environ.get("LCD_LIB_PATH", "/opt/lcd")
@@ -29,7 +30,9 @@ log = logging.getLogger("main")
DEVICE = os.environ.get("SCREEN_DEVICE", "AUTO")
ORIENTATION = Orientation[os.environ.get("SCREEN_ORIENTATION", "REVERSE_LANDSCAPE")]
BRIGHTNESS = int(os.environ.get("SCREEN_BRIGHTNESS", "50"))
TZ = os.environ.get("TZ", app.DEFAULT_TZ)
TZ = os.environ.get("BOARD_TZ", app.DEFAULT_TZ)
MAX_DRAW_FAILURES = 10
STARTUP_MAX_AGE = timedelta(hours=20)
DATA_DIR = Path(os.environ.get("DATA_DIR", "/data"))
LINES_FILE = Path(os.environ.get("LINES_FILE", "/app/lines.yaml"))
FONT_DIR = str(Path(__file__).parent / "assets" / "fonts")
@@ -58,44 +61,64 @@ def main() -> None:
last_attempt = None
last_ok = True
timetables = []
stale = False
mode = None # "board" or "clock"
last_minute = last_second = None
draw_failures = 0
# Draw from the existing cache straight away; only refresh at startup if it is old.
now = app.local_now(TZ)
timetables, stale = app.load_cache(lines, DATA_DIR, now, [], False)
try:
cache_fresh = not store.is_stale(lines, DATA_DIR, now, max_age=STARTUP_MAX_AGE)
except Exception:
log.exception("cache age check failed")
cache_fresh = False
if cache_fresh:
log.info("cache fresh, skipping startup refresh")
last_attempt, last_ok = now, True
while running:
now = app.local_now(TZ)
if app.refresh_due(last_attempt, now, last_ok):
report = None
try:
report = store.refresh(lines, DATA_DIR, now=now)
timetables = store.load_timetables(lines, DATA_DIR)
stale = store.is_stale(lines, DATA_DIR, now)
last_ok = not report.failed
log.info("refresh: %d updated, failed=%s, stale=%s", len(report.updated), report.failed, stale)
except Exception:
# Never crash or go blank: keep showing the previous timetables, retry in 15 min.
log.exception("refresh failed")
last_ok = False
timetables, stale = app.load_cache(lines, DATA_DIR, now, timetables, stale)
if report is not None:
log.info("refresh: %d updated, failed=%s, stale=%s", len(report.updated), report.failed, stale)
last_attempt = now
last_minute = None # redraw straight away
if now.second != last_second:
last_second = now.second
if now.minute != last_minute:
last_minute = now.minute
image = app.board_image(timetables, now, board_fonts, stale, state_holidays)
new_mode = "board" if image is not None else "clock"
if new_mode != mode:
log.info("showing %s", new_mode)
lcd.DisplayPILImage(Image.new("RGB", (board.W, board.H), board.BG), 0, 0)
mode = new_mode
if mode == "board":
lcd.DisplayPILImage(image, 0, 0)
else:
lcd.DisplayPILImage(render.render_info(now, clock_fonts), render.DIAL_W, 0)
if mode == "clock":
lcd.DisplayPILImage(render.render_dial(now), 0, 0)
try:
if now.minute != last_minute:
last_minute = now.minute
image = app.board_image(timetables, now, board_fonts, stale, state_holidays)
new_mode = "board" if image is not None else "clock"
if new_mode != mode:
log.info("showing %s", new_mode)
lcd.DisplayPILImage(Image.new("RGB", (board.W, board.H), board.BG), 0, 0)
mode = new_mode # only recorded once the clearing draw succeeded
if mode == "board":
lcd.DisplayPILImage(image, 0, 0)
else:
lcd.DisplayPILImage(render.render_info(now, clock_fonts), render.DIAL_W, 0)
if mode == "clock":
lcd.DisplayPILImage(render.render_dial(now), 0, 0)
draw_failures = 0
except Exception:
log.exception("draw failed")
draw_failures += 1
last_minute = None # retry on the next second
if draw_failures >= MAX_DRAW_FAILURES:
raise # let Docker restart the process and reopen the port
time.sleep(0.05)
lcd.closeSerial()