feat: run the departure board in the container, clock as fallback
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
410eb82a02
commit
e3304617f9
7 files changed
+245
-96
No files matched your search
@@ -0,0 +1,106 @@
|
||||
"""Container entry point: keep timetables fresh and show the departure board.
|
||||
|
||||
Falls back to the analog clock when there is nothing to show.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
LIB = os.environ.get("LCD_LIB_PATH", "/opt/lcd")
|
||||
sys.path.insert(0, LIB)
|
||||
|
||||
import holidays # noqa: E402
|
||||
from PIL import Image # noqa: E402
|
||||
|
||||
import app # noqa: E402
|
||||
import board # noqa: E402
|
||||
import render # noqa: E402
|
||||
import store # noqa: E402
|
||||
from library.lcd.lcd_comm import Orientation # noqa: E402
|
||||
from library.lcd.lcd_comm_rev_a import LcdCommRevA # noqa: E402
|
||||
|
||||
# force=True: the screen library configures logging at import, which would swallow ours.
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", force=True)
|
||||
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)
|
||||
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")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
lines = store.load_lines(LINES_FILE)
|
||||
state_holidays = holidays.CZ()
|
||||
board_fonts = board.load_fonts(FONT_DIR)
|
||||
clock_fonts = render.load_fonts(FONT_DIR)
|
||||
|
||||
log.info("Opening display on %s (%s), time zone %s", DEVICE, ORIENTATION.name, TZ)
|
||||
lcd = LcdCommRevA(com_port=DEVICE) if DEVICE != "AUTO" else LcdCommRevA()
|
||||
lcd.InitializeComm()
|
||||
lcd.SetOrientation(ORIENTATION)
|
||||
lcd.SetBrightness(BRIGHTNESS)
|
||||
|
||||
running = True
|
||||
|
||||
def stop(*_):
|
||||
nonlocal running
|
||||
running = False
|
||||
|
||||
signal.signal(signal.SIGTERM, stop)
|
||||
signal.signal(signal.SIGINT, stop)
|
||||
|
||||
last_attempt = None
|
||||
last_ok = True
|
||||
timetables = []
|
||||
stale = False
|
||||
mode = None # "board" or "clock"
|
||||
last_minute = last_second = None
|
||||
|
||||
while running:
|
||||
now = app.local_now(TZ)
|
||||
|
||||
if app.refresh_due(last_attempt, now, last_ok):
|
||||
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
|
||||
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)
|
||||
time.sleep(0.05)
|
||||
|
||||
lcd.closeSerial()
|
||||
log.info("Stopped")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user