feat(signage,scenes): schedule lines with screens and smart days, screens' defaults, horizontal timeline, scenes
- Scenes (Alembic 0043, models/scene.py, /api/scenes, core.scenes.manage): named modes on or off (optionally until a time); price rules / deals get the condition "a scene is on"; signage lines can play only during a scene; turning one on tells every screen and pushes the online menu. - Signage schedules are lines (signage_schedule_items): playlist, screens ([] = every screen), priority, days, smart days (1st / last day, first / last week, Greek public holidays or not: core/calendar_days.py, mirrored in @xenia/signage), hours, dates, scene. No line → the screen's default playlist. 0043 turns the old named schedules into lines for their screens. - Screens: default playlist, TV shape, turn; what plays now and why. - Manager: tabs Screens · Playlists · Schedules · Slides (placeholder) · Message and weather; schedule lines with playlist / screens / priority / delete on one line; the playlist editor's horizontal timeline of fixed cards, the slide's basics beside its preview (one height), then its content; Management → Scenes; the scene condition in pricing. - Tests: backend (lines, smart days, scenes, prices), player logic, e2e. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
"""Signage schedules (plan step 5.4d): which playlist a screen plays when.
|
||||
"""Signage schedules (plan step 5.4d; lines with their screens and scenes, owner 2026-11-21): which
|
||||
playlist a screen plays when.
|
||||
|
||||
A schedule is a list of entries {playlist_id, days, time_from, time_to, date_from, date_to,
|
||||
priority} and a fallback playlist. At a moment (venue time) the entries that match are:
|
||||
A screen's lines are the schedule lines (models/signage.py SignageScheduleItem) that are on and
|
||||
name it (or no screen = every screen). At a moment (venue time) a line matches when:
|
||||
- its days include today (empty = every day),
|
||||
- its smart days hold (core/calendar_days.py: 1st / last day, first / last week, public
|
||||
holidays or not; empty = every day),
|
||||
- the time is inside [time_from, time_to) (empty = all day; a window that passes midnight,
|
||||
e.g. 22:00-02:00, counts the after-midnight part for the day it started),
|
||||
- today is inside [date_from, date_to] (empty = no limit).
|
||||
- today is inside [date_from, date_to] (empty = no limit),
|
||||
- its scene (if any) is on.
|
||||
The winner is the highest priority, then the most specific (dates > days > hours), then the
|
||||
later entry in the list. No match → the fallback (None = a blank screen).
|
||||
later line. No match → the screen's default playlist (None = a blank screen).
|
||||
|
||||
`pick()` is pure (unit-tested; the player in 5.5a does the same in JavaScript so a screen
|
||||
switches by itself while the server is down). `schedule_worker` tells a screen when its pick
|
||||
@@ -30,12 +34,15 @@ def _minutes(hhmm: str | None) -> int | None:
|
||||
|
||||
|
||||
def _date_ok(e: dict, day) -> bool:
|
||||
from core.calendar_days import month_days_ok
|
||||
d = day.isoformat()
|
||||
return (not e.get("date_from") or d >= e["date_from"]) and (not e.get("date_to") or d <= e["date_to"])
|
||||
return (not e.get("date_from") or d >= e["date_from"]) and (not e.get("date_to") or d <= e["date_to"]) and month_days_ok(e.get("month_days"), day)
|
||||
|
||||
|
||||
def matches(e: dict, local: datetime) -> bool:
|
||||
"""Does entry `e` cover this venue-local moment?"""
|
||||
def matches(e: dict, local: datetime, scenes=None) -> bool:
|
||||
"""Does entry `e` cover this venue-local moment (with these scenes on)?"""
|
||||
if e.get("scene_id") is not None and e["scene_id"] not in (scenes or ()):
|
||||
return False
|
||||
days = e.get("days") or []
|
||||
start, end = _minutes(e.get("time_from")), _minutes(e.get("time_to"))
|
||||
now = local.hour * 60 + local.minute
|
||||
@@ -55,15 +62,15 @@ def matches(e: dict, local: datetime) -> bool:
|
||||
|
||||
|
||||
def _specific(e: dict) -> int:
|
||||
return (4 if (e.get("date_from") or e.get("date_to")) else 0) + (2 if e.get("days") else 0) + \
|
||||
return (4 if (e.get("date_from") or e.get("date_to")) else 0) + (2 if (e.get("days") or e.get("month_days")) else 0) + \
|
||||
(1 if (e.get("time_from") or e.get("time_to")) else 0)
|
||||
|
||||
|
||||
def pick(entries: list[dict], fallback: int | None, local: datetime) -> int | None:
|
||||
def pick(entries: list[dict], fallback: int | None, local: datetime, scenes=None) -> int | None:
|
||||
"""The playlist id that plays at `local` (venue time)."""
|
||||
best, best_key = None, None
|
||||
for i, e in enumerate(entries or []):
|
||||
if e.get("playlist_id") is None or not matches(e, local):
|
||||
if e.get("playlist_id") is None or not matches(e, local, scenes):
|
||||
continue
|
||||
key = (e.get("priority") or 0, _specific(e), i)
|
||||
if best_key is None or key > best_key:
|
||||
@@ -76,20 +83,43 @@ def pick(entries: list[dict], fallback: int | None, local: datetime) -> int | No
|
||||
_playing: dict[int, int | None] = {} # screen id → the playlist it was told about
|
||||
|
||||
|
||||
def due_changes(db, local: datetime | None = None) -> list[int]:
|
||||
"""The screens on a schedule whose pick is different from the last one seen (and remember it)."""
|
||||
ENTRY_KEYS = ("playlist_id", "days", "month_days", "time_from", "time_to", "date_from", "date_to", "priority", "scene_id")
|
||||
|
||||
|
||||
def entry_of(item) -> dict:
|
||||
"""A schedule line as the screens and pick() read it."""
|
||||
return {k: getattr(item, k) for k in ENTRY_KEYS}
|
||||
|
||||
|
||||
def lines_for(db, screen_id: int) -> list:
|
||||
"""The schedule lines that are on and name this screen (or no screen), in order."""
|
||||
from models.signage import SignageScheduleItem
|
||||
return [i for i in db.query(SignageScheduleItem).filter(SignageScheduleItem.enabled.is_(True))
|
||||
.order_by(SignageScheduleItem.position, SignageScheduleItem.id) if not i.screen_ids or screen_id in i.screen_ids]
|
||||
|
||||
|
||||
def now_for(db, dev, local: datetime | None = None, scenes=None) -> tuple[int | None, bool]:
|
||||
"""(the playlist a screen plays now, whether a schedule line picked it)."""
|
||||
from core.timeutil import venue_now
|
||||
from models.signage import SignageSchedule
|
||||
from routers.signage import _screen_config, _screens
|
||||
from routers.signage import _screen_config
|
||||
from services.scenes import active_ids
|
||||
local = local or venue_now()
|
||||
scenes = active_ids(db) if scenes is None else scenes
|
||||
default = _screen_config(dev).get("playlist")
|
||||
got = pick([entry_of(i) for i in lines_for(db, dev.id)], None, local, scenes)
|
||||
return (got, True) if got is not None else (default, False)
|
||||
|
||||
|
||||
def due_changes(db, local: datetime | None = None) -> list[int]:
|
||||
"""The screens whose pick is different from the last one seen (and remember it)."""
|
||||
from core.timeutil import venue_now
|
||||
from routers.signage import _screens
|
||||
from services.scenes import active_ids
|
||||
local = local or venue_now()
|
||||
scenes = active_ids(db)
|
||||
changed = []
|
||||
for d in _screens(db):
|
||||
sid = _screen_config(d).get("schedule")
|
||||
if not sid:
|
||||
_playing.pop(d.id, None)
|
||||
continue
|
||||
s = db.get(SignageSchedule, sid)
|
||||
now = pick(s.entries or [], s.fallback_playlist_id, local) if s else None
|
||||
now = now_for(db, d, local, scenes)[0]
|
||||
if d.id in _playing and _playing[d.id] != now:
|
||||
changed.append(d.id)
|
||||
_playing[d.id] = now
|
||||
|
||||
Reference in New Issue
Block a user