feat(engine): Pi-side animation engine with protocol outputs and sequencer
Add src/engine, modelled on technicalkiwi/portal's player and sequence runner: - Pixel generator: runs the unchanged led-driver patterns under CPython (utime shim + NeoPixel-like buffer) on a per-zone render thread and streams frames to pixel outputs, with keepalive for static frames. - Output drivers: DDP, Art-Net (ArtDMX/ArtSync), sACN/E1.31, USB-DMX (Enttec Pro / Open DMX), plus an ESP32 preset output that reuses the existing preset + select delivery path. Registry in db/output.json, CRUD and test pattern at /outputs. - Sequencer: timed preset chains per zone (portal step shape), deadline scheduling, loop/repeat, resume after restart; legacy sequence records are migrated. Zone play/stop, sequence and scene play/stop endpoints. - CLI (python -m engine.cli), Outputs and Sequences modals, preset tiles drive pixel outputs, API/README docs, engine test suite. ESP32 firmware is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
827eb97203
commit
a5a7fc575f
42 files changed
+4117
-75
No files matched your search
@@ -0,0 +1,214 @@
|
||||
"""Sequence model normalisation/migration and the SequenceRunner."""
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from engine.core import EngineError
|
||||
from engine.sequence_runner import SequenceRunner
|
||||
from models.squence import _normalize, migrate_legacy
|
||||
|
||||
|
||||
class FakeSequences(dict):
|
||||
def read(self, id):
|
||||
return self.get(str(id))
|
||||
|
||||
def update(self, id, data):
|
||||
if str(id) not in self:
|
||||
return False
|
||||
self[str(id)].update(data)
|
||||
return True
|
||||
|
||||
def list(self):
|
||||
return list(self.keys())
|
||||
|
||||
|
||||
class FakeEngine:
|
||||
def __init__(self):
|
||||
self.plays = []
|
||||
|
||||
async def play(self, zone_id, preset_id, source="manual"):
|
||||
self.plays.append((zone_id, str(preset_id), source))
|
||||
|
||||
|
||||
def seq(zone="1", steps=("a", "b"), duration=0.02, **kw):
|
||||
doc = {"name": "s", "zone_id": zone, "loop": True, "repeat_count": 0, "active": False,
|
||||
"steps": [{"preset_id": p, "duration_s": duration} for p in steps]}
|
||||
doc.update(kw)
|
||||
return doc
|
||||
|
||||
|
||||
# -- model ----------------------------------------------------------------------
|
||||
|
||||
def test_normalize_clamps_and_defaults():
|
||||
doc = _normalize({"name": " x ", "zone_id": 2, "steps": [
|
||||
{"preset_id": 1, "duration_s": 0.01}, {"preset_id": "2", "duration_s": 9999}, {"preset_id": "3"}]})
|
||||
assert doc["name"] == "x" and doc["zone_id"] == "2" and doc["loop"] is True
|
||||
assert [s["duration_s"] for s in doc["steps"]] == [0.5, 600.0, 5.0]
|
||||
assert doc["steps"][0]["preset_id"] == "1"
|
||||
with pytest.raises(ValueError):
|
||||
_normalize({"steps": [{"duration_s": 3}]})
|
||||
|
||||
|
||||
def test_migrate_legacy_record():
|
||||
legacy = {"group_name": "Main Group", "presets": ["1", "2"], "sequence_duration": 3000,
|
||||
"sequence_transition": 500, "sequence_loop": False, "sequence_repeat_count": 2,
|
||||
"sequence_active": True, "sequence_index": 1, "sequence_start_time": 0}
|
||||
doc, migrated = migrate_legacy(legacy)
|
||||
assert migrated
|
||||
assert doc == {"name": "Main Group", "zone_id": None, "loop": False, "repeat_count": 2,
|
||||
"steps": [{"preset_id": "1", "duration_s": 3.0}, {"preset_id": "2", "duration_s": 3.0}],
|
||||
"active": False}
|
||||
same, migrated = migrate_legacy(doc)
|
||||
assert not migrated and same is doc
|
||||
|
||||
|
||||
# -- runner ---------------------------------------------------------------------
|
||||
|
||||
def test_steps_play_in_order_and_repeat_count_ends():
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(steps=("a", "b", "c"), repeat_count=2)})
|
||||
eng = FakeEngine()
|
||||
runner = SequenceRunner(eng, sequences_model=model)
|
||||
await runner.play("1")
|
||||
assert model["1"]["active"] is True
|
||||
await asyncio.sleep(0.4)
|
||||
return eng, model, runner
|
||||
|
||||
eng, model, runner = asyncio.run(go())
|
||||
assert [p for _z, p, _s in eng.plays] == ["a", "b", "c", "a", "b", "c"]
|
||||
assert all(s == "sequence" for _z, _p, s in eng.plays)
|
||||
assert model["1"]["active"] is False
|
||||
st = runner.status()["1"]
|
||||
assert st["running"] is False and st["pass"] == 2
|
||||
|
||||
|
||||
def test_no_loop_plays_once():
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(loop=False)})
|
||||
eng = FakeEngine()
|
||||
await SequenceRunner(eng, sequences_model=model).play("1")
|
||||
await asyncio.sleep(0.2)
|
||||
return eng
|
||||
|
||||
assert [p for _z, p, _s in asyncio.run(go()).plays] == ["a", "b"]
|
||||
|
||||
|
||||
def test_infinite_loop_until_stop():
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq()})
|
||||
eng = FakeEngine()
|
||||
runner = SequenceRunner(eng, sequences_model=model)
|
||||
await runner.play("1")
|
||||
await asyncio.sleep(0.25)
|
||||
assert await runner.stop("1") is True
|
||||
n = len(eng.plays)
|
||||
await asyncio.sleep(0.1)
|
||||
return eng, n, model
|
||||
|
||||
eng, n, model = asyncio.run(go())
|
||||
assert n >= 6 and len(eng.plays) == n
|
||||
assert model["1"]["active"] is False
|
||||
|
||||
|
||||
def test_new_sequence_replaces_old_on_same_zone():
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(steps=("a",)), "2": seq(steps=("z",))})
|
||||
eng = FakeEngine()
|
||||
runner = SequenceRunner(eng, sequences_model=model)
|
||||
await runner.play("1")
|
||||
await asyncio.sleep(0.05)
|
||||
await runner.play("2")
|
||||
await asyncio.sleep(0.05)
|
||||
plays_after_switch = list(eng.plays)
|
||||
await runner.stop_all()
|
||||
return plays_after_switch, model, runner
|
||||
|
||||
plays, model, runner = asyncio.run(go())
|
||||
switch = plays.index(("1", "z", "sequence"))
|
||||
assert all(p == "z" for _z, p, _s in plays[switch:])
|
||||
assert model["1"]["active"] is False
|
||||
assert runner.status()["1"]["running"] is False
|
||||
|
||||
|
||||
def test_stop_all_keeps_active_for_resume():
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(), "2": seq(zone="2")})
|
||||
eng = FakeEngine()
|
||||
runner = SequenceRunner(eng, sequences_model=model)
|
||||
await runner.play("1")
|
||||
await runner.play("2")
|
||||
await runner.stop_all(persist=False)
|
||||
assert model["1"]["active"] and model["2"]["active"]
|
||||
fresh = SequenceRunner(FakeEngine(), sequences_model=model)
|
||||
resumed = await fresh.resume_active()
|
||||
await fresh.stop_all()
|
||||
return resumed
|
||||
|
||||
assert sorted(asyncio.run(go())) == ["1", "2"]
|
||||
|
||||
|
||||
def test_play_errors():
|
||||
async def go():
|
||||
model = FakeSequences({"nozone": seq(zone=None), "empty": seq(steps=())})
|
||||
runner = SequenceRunner(FakeEngine(), sequences_model=model)
|
||||
for sid in ("missing", "nozone", "empty"):
|
||||
with pytest.raises(EngineError):
|
||||
await runner.play(sid)
|
||||
|
||||
asyncio.run(go())
|
||||
|
||||
|
||||
def test_failing_step_does_not_kill_sequence():
|
||||
class Flaky(FakeEngine):
|
||||
async def play(self, zone_id, preset_id, source="manual"):
|
||||
await super().play(zone_id, preset_id, source)
|
||||
if preset_id == "a":
|
||||
raise EngineError("Preset not found", 404)
|
||||
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(loop=False)})
|
||||
eng = Flaky()
|
||||
runner = SequenceRunner(eng, sequences_model=model)
|
||||
await runner.play("1")
|
||||
await asyncio.sleep(0.2)
|
||||
return eng, runner
|
||||
|
||||
eng, runner = asyncio.run(go())
|
||||
assert [p for _z, p, _s in eng.plays] == ["a", "b"]
|
||||
assert runner.status()["1"]["error"] == "Preset not found"
|
||||
|
||||
|
||||
def test_manual_engine_play_stops_zone_sequence():
|
||||
from engine.core import Engine
|
||||
from engine.zone_targets import ZoneTargets
|
||||
|
||||
class NullPlayer:
|
||||
def __init__(self, targets, library, brightness):
|
||||
self.targets = targets
|
||||
|
||||
async def start(self):
|
||||
pass
|
||||
|
||||
async def apply(self, preset_id, payload, drivers=True):
|
||||
pass
|
||||
|
||||
async def stop(self):
|
||||
pass
|
||||
|
||||
def status(self):
|
||||
return {}
|
||||
|
||||
async def go():
|
||||
model = FakeSequences({"1": seq(zone="5", steps=("a", "b"))})
|
||||
eng = Engine(library=object(), zone_resolver=lambda z: ZoneTargets(z, [], ["dj"]),
|
||||
payload_resolver=lambda p: {"p": "on"}, player_factory=NullPlayer,
|
||||
sequences_model=model)
|
||||
await eng.sequences.play("1")
|
||||
await asyncio.sleep(0.03)
|
||||
await eng.play("5", "x") # manual pick
|
||||
return eng, model
|
||||
|
||||
eng, model = asyncio.run(go())
|
||||
assert eng.sequences.zone_for("1") is None
|
||||
assert model["1"]["active"] is False
|
||||
Reference in new issue
Block a user