From 8f75af52d1a431d4c92277982ec036547fe4adda Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Mon, 2 Jun 2025 22:10:54 +0200 Subject: [PATCH] Use multiple pages, fix timezone --- buba/__main__.py | 6 ++--- buba/animations/dbf.py | 6 ----- buba/animations/icalevents.py | 43 +++++++++++++++++++++++------------ buba/bubaanimator.py | 6 +++++ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/buba/__main__.py b/buba/__main__.py index c35b646..27ce1d3 100644 --- a/buba/__main__.py +++ b/buba/__main__.py @@ -30,9 +30,9 @@ websocket_clients = WebSocketClients() buba = BubaCmd(config.serial, websocket_clients.send) animator = BubaAnimator(buba) # animator.add(BubaCharset) #, single=12) -animator.add(BubaTime) -animator.add(DBFAnimation, ds100="AHST", station="Holstenstraße") -animator.add(DBFAnimation, ds100="AHS", station="Altona", count=9) +# animator.add(BubaTime) +# animator.add(DBFAnimation, ds100="AHST", station="Holstenstraße") +# animator.add(DBFAnimation, ds100="AHS", station="Altona", count=9) animator.add(IcalEvents, url="https://cloud.hamburg.ccc.de/remote.php/dav/public-calendars/QJAdExziSnNJEz5g?export", title="CCCHH Events") @app.route("/static/") diff --git a/buba/animations/dbf.py b/buba/animations/dbf.py index b516883..caccb3c 100644 --- a/buba/animations/dbf.py +++ b/buba/animations/dbf.py @@ -4,7 +4,6 @@ Pull departure info from https://trains.xatlabs.com and display. See also https://github.com/derf/db-fakedisplay/blob/main/README.md """ import datetime -from itertools import islice from threading import Thread from time import sleep @@ -95,11 +94,6 @@ class DBFAnimation(BubaAnimation): train = train.replace(" ", "") return train - @staticmethod - def chunk(it, size): - it = iter(it) - return iter(lambda: tuple(islice(it, size)), ()) - def run(self): all = self.trains[:self.count] all_len = int((len(all)+1)/3) diff --git a/buba/animations/icalevents.py b/buba/animations/icalevents.py index 32fbd39..a3c63d2 100644 --- a/buba/animations/icalevents.py +++ b/buba/animations/icalevents.py @@ -1,9 +1,12 @@ +import os from datetime import timedelta, datetime +from threading import Thread from time import sleep -from buba.bubaanimator import BubaAnimation import icalevents.icalevents +from pytz import timezone +from buba.bubaanimator import BubaAnimation from buba.bubacmd import BubaCmd @@ -12,27 +15,39 @@ class IcalEvents(BubaAnimation): super().__init__(buba) self.url = url self.title = title - self.events = icalevents.icalevents.events(url) - self.log.debug(f"Events loaded {len(self.events)}") + self.events = [] + Thread(target=self.update, daemon=True).start() def __repr__(self): return f"<{type(self).__name__}, {self.url}>" + def update(self): + tz = timezone(os.getenv("TZ", "Europe/Berlin")) + events = icalevents.icalevents.events(self.url, sort=True) + for event in events: + event.start = event.start.astimezone(tz) + self.events = events + sleep(600) + @staticmethod def ellipsis(text, max=28): if len(text) > max: - text = text[:max-3] + "..." + text = text[:max - 3] + "..." return text def run(self): - self.buba.text(page=0, row=0, col_start=0, col_end=119, text=self.title, align=BubaCmd.ALIGN_LEFT) - for (i, event) in enumerate(self.events[0:3]): - if event.start - datetime.now(event.start.tzinfo) < timedelta(hours=20): - when = event.start.strftime("%H:%M") + for (page, events) in enumerate(self.chunk(self.events, 3)): + if len(self.events) > 3: + self.buba.text(page=0, row=0, col_start=0, col_end=119, + text=f"{self.title} ({page + 1}/{int(len(self.events) / 3)})", align=BubaCmd.ALIGN_LEFT) else: - when = event.start.strftime("%d.%m.") - self.log.info(f"event '{self.ellipsis(event.summary)} at {when}") - self.buba.text(page=0, row=i + 1, col_start=0, col_end=104, text=self.ellipsis(event.summary)) - self.buba.text(page=0, row=i + 1, col_start=105, col_end=119, - text=when, align=BubaCmd.ALIGN_RIGHT) - sleep(10) \ No newline at end of file + self.buba.text(page=0, row=0, col_start=0, col_end=119, text=self.title, align=BubaCmd.ALIGN_LEFT) + for (i, event) in enumerate(events): + if event.start - datetime.now(event.start.tzinfo) < timedelta(hours=24): + when = event.start.strftime("%H:%M") + else: + when = event.start.strftime("%d.%m.") + self.buba.text(page=0, row=i + 1, col_start=0, col_end=104, text=self.ellipsis(event.summary)) + self.buba.text(page=0, row=i + 1, col_start=105, col_end=119, + text=when, align=BubaCmd.ALIGN_RIGHT) + sleep(10) diff --git a/buba/bubaanimator.py b/buba/bubaanimator.py index a3eac49..17ba786 100644 --- a/buba/bubaanimator.py +++ b/buba/bubaanimator.py @@ -1,4 +1,5 @@ import logging +from itertools import islice from threading import Thread from time import sleep @@ -11,6 +12,11 @@ class BubaAnimation: self.buba = buba pass + @staticmethod + def chunk(it, size): + it = iter(it) + return iter(lambda: tuple(islice(it, size)), ()) + def run(self): pass