Use multiple pages, fix timezone

This commit is contained in:
Stefan Bethke 2025-06-02 22:10:54 +02:00
commit 8f75af52d1
4 changed files with 38 additions and 23 deletions

View file

@ -30,9 +30,9 @@ websocket_clients = WebSocketClients()
buba = BubaCmd(config.serial, websocket_clients.send) buba = BubaCmd(config.serial, websocket_clients.send)
animator = BubaAnimator(buba) animator = BubaAnimator(buba)
# animator.add(BubaCharset) #, single=12) # animator.add(BubaCharset) #, single=12)
animator.add(BubaTime) # animator.add(BubaTime)
animator.add(DBFAnimation, ds100="AHST", station="Holstenstraße") # animator.add(DBFAnimation, ds100="AHST", station="Holstenstraße")
animator.add(DBFAnimation, ds100="AHS", station="Altona", count=9) # 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") animator.add(IcalEvents, url="https://cloud.hamburg.ccc.de/remote.php/dav/public-calendars/QJAdExziSnNJEz5g?export", title="CCCHH Events")
@app.route("/static/<filepath>") @app.route("/static/<filepath>")

View file

@ -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 See also https://github.com/derf/db-fakedisplay/blob/main/README.md
""" """
import datetime import datetime
from itertools import islice
from threading import Thread from threading import Thread
from time import sleep from time import sleep
@ -95,11 +94,6 @@ class DBFAnimation(BubaAnimation):
train = train.replace(" ", "") train = train.replace(" ", "")
return train return train
@staticmethod
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
def run(self): def run(self):
all = self.trains[:self.count] all = self.trains[:self.count]
all_len = int((len(all)+1)/3) all_len = int((len(all)+1)/3)

View file

@ -1,9 +1,12 @@
import os
from datetime import timedelta, datetime from datetime import timedelta, datetime
from threading import Thread
from time import sleep from time import sleep
from buba.bubaanimator import BubaAnimation
import icalevents.icalevents import icalevents.icalevents
from pytz import timezone
from buba.bubaanimator import BubaAnimation
from buba.bubacmd import BubaCmd from buba.bubacmd import BubaCmd
@ -12,12 +15,20 @@ class IcalEvents(BubaAnimation):
super().__init__(buba) super().__init__(buba)
self.url = url self.url = url
self.title = title self.title = title
self.events = icalevents.icalevents.events(url) self.events = []
self.log.debug(f"Events loaded {len(self.events)}") Thread(target=self.update, daemon=True).start()
def __repr__(self): def __repr__(self):
return f"<{type(self).__name__}, {self.url}>" 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 @staticmethod
def ellipsis(text, max=28): def ellipsis(text, max=28):
if len(text) > max: if len(text) > max:
@ -25,13 +36,17 @@ class IcalEvents(BubaAnimation):
return text return text
def run(self): def run(self):
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:
self.buba.text(page=0, row=0, col_start=0, col_end=119, text=self.title, align=BubaCmd.ALIGN_LEFT) 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]): for (i, event) in enumerate(events):
if event.start - datetime.now(event.start.tzinfo) < timedelta(hours=20): if event.start - datetime.now(event.start.tzinfo) < timedelta(hours=24):
when = event.start.strftime("%H:%M") when = event.start.strftime("%H:%M")
else: else:
when = event.start.strftime("%d.%m.") 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=0, col_end=104, text=self.ellipsis(event.summary))
self.buba.text(page=0, row=i + 1, col_start=105, col_end=119, self.buba.text(page=0, row=i + 1, col_start=105, col_end=119,
text=when, align=BubaCmd.ALIGN_RIGHT) text=when, align=BubaCmd.ALIGN_RIGHT)

View file

@ -1,4 +1,5 @@
import logging import logging
from itertools import islice
from threading import Thread from threading import Thread
from time import sleep from time import sleep
@ -11,6 +12,11 @@ class BubaAnimation:
self.buba = buba self.buba = buba
pass pass
@staticmethod
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
def run(self): def run(self):
pass pass