led-controller-version-3 3.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- led_controller_version_3-3.0.2.dist-info/METADATA +172 -0
- led_controller_version_3-3.0.2.dist-info/RECORD +52 -0
- led_controller_version_3-3.0.2.dist-info/WHEEL +4 -0
- led_controller_version_3-3.0.2.dist-info/entry_points.txt +13 -0
- lefx/device/respeaker/__init__.py +35 -0
- lefx/device/respeaker/cli.py +146 -0
- lefx/device/respeaker/contention.py +523 -0
- lefx/device/respeaker/provider.py +158 -0
- lefx/device/respeaker/registration.py +130 -0
- lefx/device/respeaker/sink.py +108 -0
- lefx/device/respeaker/transport.py +344 -0
- lefx/device/respeaker/xvf.py +313 -0
- lefx/engine/__init__.py +110 -0
- lefx/engine/composer.py +203 -0
- lefx/engine/config.py +27 -0
- lefx/engine/errors.py +84 -0
- lefx/engine/inputs.py +196 -0
- lefx/engine/invocation.py +129 -0
- lefx/engine/layers.py +114 -0
- lefx/engine/library.py +181 -0
- lefx/engine/packages/__init__.py +45 -0
- lefx/engine/packages/loader.py +375 -0
- lefx/engine/packages/manifest.py +308 -0
- lefx/engine/registry.py +347 -0
- lefx/engine/renderer.py +63 -0
- lefx/engine/runtime.py +393 -0
- lefx/engine/store.py +125 -0
- lefx/interfaces/__init__.py +40 -0
- lefx/interfaces/api.py +301 -0
- lefx/interfaces/cli.py +380 -0
- lefx/interfaces/client.py +165 -0
- lefx/interfaces/config.py +305 -0
- lefx/interfaces/discovery.py +308 -0
- lefx/interfaces/hosting.py +123 -0
- lefx/interfaces/paths.py +79 -0
- lefx/interfaces/presentation.py +118 -0
- lefx/interfaces/service.py +468 -0
- lefx/sdk/__init__.py +176 -0
- lefx/sdk/calibration.py +202 -0
- lefx/sdk/color.py +108 -0
- lefx/sdk/context.py +72 -0
- lefx/sdk/definitions.py +526 -0
- lefx/sdk/effects.py +63 -0
- lefx/sdk/errors.py +90 -0
- lefx/sdk/parameters.py +469 -0
- lefx/sdk/ports.py +76 -0
- lefx/sdk/validation.py +150 -0
- lefx/sdk/values.py +212 -0
- lefx/sets/core_set/__init__.py +32 -0
- lefx/sets/core_set/core-set.lefxset +0 -0
- lefx/sets/smartspeaker_set/__init__.py +32 -0
- lefx/sets/smartspeaker_set/smartspeaker-set.lefxset +0 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
"""The controller service: render loop, sink, providers and persistence.
|
|
2
|
+
|
|
3
|
+
Everything the engine needs from the outside world is assembled here — and only
|
|
4
|
+
here. The service knows which sink is installed and where state is kept; the
|
|
5
|
+
engine knows neither.
|
|
6
|
+
|
|
7
|
+
It carries no application meaning. There is no state named "offline", no
|
|
8
|
+
countdown, no direction command. When the output goes away the fact is published
|
|
9
|
+
and reported; deciding what the ring should then show is an application's job,
|
|
10
|
+
and the recipe for doing it sits in an integration rather than in this file.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import logging
|
|
17
|
+
import threading
|
|
18
|
+
import time
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import Any, Callable, Mapping
|
|
21
|
+
|
|
22
|
+
from lefx.engine import (
|
|
23
|
+
EffectLibrary,
|
|
24
|
+
EffectRuntime,
|
|
25
|
+
EngineConfig,
|
|
26
|
+
Invocation,
|
|
27
|
+
LayerId,
|
|
28
|
+
slot_for,
|
|
29
|
+
)
|
|
30
|
+
from lefx.sdk import FrameSink, StateSlot
|
|
31
|
+
|
|
32
|
+
from . import discovery, paths, presentation
|
|
33
|
+
|
|
34
|
+
logger = logging.getLogger("lefx.interfaces.service")
|
|
35
|
+
|
|
36
|
+
StatusListener = Callable[[str, dict[str, Any]], None]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ControllerService:
|
|
40
|
+
"""Owns the runtime, the render thread and everything around them."""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
*,
|
|
45
|
+
sink: FrameSink | str | None = None,
|
|
46
|
+
led_count: int = 12,
|
|
47
|
+
fps: float = 30.0,
|
|
48
|
+
search_paths: list[str | Path] | None = None,
|
|
49
|
+
state_file: str | Path | None = None,
|
|
50
|
+
sink_options: Mapping[str, Any] | None = None,
|
|
51
|
+
input_device: str | None = None,
|
|
52
|
+
autostart_providers: bool = True,
|
|
53
|
+
) -> None:
|
|
54
|
+
self.config = EngineConfig(led_count=led_count, fps=fps)
|
|
55
|
+
self.library = EffectLibrary(
|
|
56
|
+
search_paths=search_paths if search_paths is not None else paths.package_search_paths()
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
self.device_options = dict(sink_options or {})
|
|
60
|
+
self.sink_name, self.sink = self._build_sink(sink, dict(self.device_options))
|
|
61
|
+
|
|
62
|
+
# Choosing the output device chooses the input device with it: a
|
|
63
|
+
# reSpeaker's microphones come with its ring. Naming one separately is
|
|
64
|
+
# for the unusual case — hardware direction on a simulated display.
|
|
65
|
+
self.input_device = self.sink_name if input_device is None else input_device
|
|
66
|
+
self.providers: dict[str, Any] = (
|
|
67
|
+
# The same options go to both halves of a device. They configure the
|
|
68
|
+
# device, not the sink: a DoA calibration or a transport port is a
|
|
69
|
+
# property of the thing, and having to state it twice under two
|
|
70
|
+
# names would be a way to state it inconsistently. Factories ignore
|
|
71
|
+
# what they do not recognise, which is what makes one set do.
|
|
72
|
+
discovery.create_providers(
|
|
73
|
+
device=self.input_device, led_count=led_count, **self.device_options
|
|
74
|
+
)
|
|
75
|
+
if autostart_providers
|
|
76
|
+
else {}
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
self.runtime = EffectRuntime(
|
|
80
|
+
self.library.registry,
|
|
81
|
+
sink=self.sink,
|
|
82
|
+
config=self.config,
|
|
83
|
+
input_providers=discovery.provider_callables(self.providers),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
self.state_file = Path(state_file) if state_file is not None else paths.background_state_file()
|
|
87
|
+
self._lock = threading.RLock()
|
|
88
|
+
self._stop = threading.Event()
|
|
89
|
+
self._thread: threading.Thread | None = None
|
|
90
|
+
self._render_count = 0
|
|
91
|
+
self._last_error: str | None = None
|
|
92
|
+
self._started_at: float | None = None
|
|
93
|
+
self._listeners: list[StatusListener] = []
|
|
94
|
+
self._sink_available: bool | None = None
|
|
95
|
+
self._persisted_signature: str | None = None
|
|
96
|
+
|
|
97
|
+
self.restore_background_state()
|
|
98
|
+
|
|
99
|
+
# -- construction -------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
def _build_sink(
|
|
102
|
+
self, sink: FrameSink | str | None, options: dict[str, Any]
|
|
103
|
+
) -> tuple[str, FrameSink]:
|
|
104
|
+
if sink is None:
|
|
105
|
+
return discovery.NullSink.name, discovery.NullSink()
|
|
106
|
+
if isinstance(sink, str):
|
|
107
|
+
options.setdefault("led_count", self.config.led_count)
|
|
108
|
+
return sink, discovery.create_sink(sink, **options)
|
|
109
|
+
return getattr(sink, "name", type(sink).__name__), sink
|
|
110
|
+
|
|
111
|
+
# -- lifecycle ----------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
def start(self) -> None:
|
|
114
|
+
if self._thread is not None and self._thread.is_alive():
|
|
115
|
+
return
|
|
116
|
+
self._stop.clear()
|
|
117
|
+
self._started_at = time.time()
|
|
118
|
+
self._thread = threading.Thread(target=self._loop, name="lefx-render", daemon=True)
|
|
119
|
+
self._thread.start()
|
|
120
|
+
logger.info(
|
|
121
|
+
"service started sink=%s led_count=%s fps=%s effects=%s",
|
|
122
|
+
self.sink_name,
|
|
123
|
+
self.config.led_count,
|
|
124
|
+
self.config.fps,
|
|
125
|
+
len(self.library.registry),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
def stop(self) -> None:
|
|
129
|
+
self._stop.set()
|
|
130
|
+
if self._thread is not None:
|
|
131
|
+
self._thread.join(timeout=2.0)
|
|
132
|
+
self._thread = None
|
|
133
|
+
with self._lock:
|
|
134
|
+
self.runtime.close()
|
|
135
|
+
self._close_providers()
|
|
136
|
+
self.library.close()
|
|
137
|
+
self._started_at = None
|
|
138
|
+
logger.info("service stopped")
|
|
139
|
+
|
|
140
|
+
def _close_providers(self) -> None:
|
|
141
|
+
"""Let providers release what they hold. Not every provider has anything.
|
|
142
|
+
|
|
143
|
+
Closing is optional on the port for the same reason ``refresh`` is: a
|
|
144
|
+
provider computing a value from the clock has nothing to release, and
|
|
145
|
+
requiring the method would be ceremony. One that owns a socket or a
|
|
146
|
+
thread does, and leaving it running would outlive the service.
|
|
147
|
+
"""
|
|
148
|
+
for name, provider in self.providers.items():
|
|
149
|
+
close = getattr(provider, "close", None)
|
|
150
|
+
if close is None:
|
|
151
|
+
continue
|
|
152
|
+
try:
|
|
153
|
+
close()
|
|
154
|
+
except Exception:
|
|
155
|
+
logger.exception("input provider %r failed to close", name)
|
|
156
|
+
|
|
157
|
+
def __enter__(self) -> "ControllerService":
|
|
158
|
+
self.start()
|
|
159
|
+
return self
|
|
160
|
+
|
|
161
|
+
def __exit__(self, *exc: object) -> None:
|
|
162
|
+
self.stop()
|
|
163
|
+
|
|
164
|
+
def _loop(self) -> None:
|
|
165
|
+
interval = 1.0 / self.config.fps
|
|
166
|
+
while not self._stop.is_set():
|
|
167
|
+
started = time.monotonic()
|
|
168
|
+
try:
|
|
169
|
+
self.render_once(started)
|
|
170
|
+
except Exception as exc:
|
|
171
|
+
self._last_error = repr(exc)
|
|
172
|
+
logger.exception("render loop iteration failed")
|
|
173
|
+
self._stop.wait(max(0.0, interval - (time.monotonic() - started)))
|
|
174
|
+
|
|
175
|
+
def render_once(self, now: float | None = None) -> None:
|
|
176
|
+
moment = time.monotonic() if now is None else now
|
|
177
|
+
with self._lock:
|
|
178
|
+
self._refresh_providers(moment)
|
|
179
|
+
self.runtime.render_once(moment)
|
|
180
|
+
self._render_count += 1
|
|
181
|
+
self._publish_sink_state()
|
|
182
|
+
self._persist_background_state()
|
|
183
|
+
|
|
184
|
+
def _refresh_providers(self, now: float) -> None:
|
|
185
|
+
"""Poll each provider on its own schedule, not once per effect instance."""
|
|
186
|
+
for name, provider in self.providers.items():
|
|
187
|
+
refresh = getattr(provider, "refresh", None)
|
|
188
|
+
if refresh is None:
|
|
189
|
+
continue
|
|
190
|
+
try:
|
|
191
|
+
refresh(now)
|
|
192
|
+
except Exception as exc:
|
|
193
|
+
logger.warning("input provider %r failed to refresh: %s", name, exc)
|
|
194
|
+
|
|
195
|
+
# -- status publication -------------------------------------------------
|
|
196
|
+
|
|
197
|
+
def add_listener(self, listener: StatusListener) -> None:
|
|
198
|
+
"""Subscribe to service-level events such as the output going away.
|
|
199
|
+
|
|
200
|
+
The engine has no concept of a device. An application that wants the
|
|
201
|
+
ring to say something when the hardware disappears listens here and
|
|
202
|
+
issues an ordinary command; that mapping stays out of the engine.
|
|
203
|
+
"""
|
|
204
|
+
self._listeners.append(listener)
|
|
205
|
+
|
|
206
|
+
def _publish_sink_state(self) -> None:
|
|
207
|
+
status = self.sink.status()
|
|
208
|
+
if status.available == self._sink_available:
|
|
209
|
+
return
|
|
210
|
+
self._sink_available = status.available
|
|
211
|
+
payload = {"sink": self.sink_name, **status.to_dict()}
|
|
212
|
+
logger.info("output %s (%s)", "available" if status.available else "unavailable", self.sink_name)
|
|
213
|
+
self._emit("sink_changed", payload)
|
|
214
|
+
|
|
215
|
+
def _emit(self, event: str, payload: dict[str, Any]) -> None:
|
|
216
|
+
for listener in list(self._listeners):
|
|
217
|
+
try:
|
|
218
|
+
listener(event, payload)
|
|
219
|
+
except Exception:
|
|
220
|
+
logger.exception("status listener failed for %r", event)
|
|
221
|
+
|
|
222
|
+
# -- background state persistence ---------------------------------------
|
|
223
|
+
|
|
224
|
+
def restore_background_state(self) -> None:
|
|
225
|
+
"""Bring back the background state a previous run left behind.
|
|
226
|
+
|
|
227
|
+
Only the background slot is persisted, and only for definitions that
|
|
228
|
+
declare themselves restorable. The primary state is a live application
|
|
229
|
+
concern and is not service configuration.
|
|
230
|
+
|
|
231
|
+
Public because the catalogue can change after construction — after a
|
|
232
|
+
source is registered, the state that could not be restored before may
|
|
233
|
+
now exist.
|
|
234
|
+
"""
|
|
235
|
+
try:
|
|
236
|
+
payload = json.loads(self.state_file.read_text(encoding="utf-8"))
|
|
237
|
+
except (OSError, json.JSONDecodeError):
|
|
238
|
+
return
|
|
239
|
+
target = payload.get("effect_id")
|
|
240
|
+
if not target:
|
|
241
|
+
return
|
|
242
|
+
try:
|
|
243
|
+
self.runtime.set_state(
|
|
244
|
+
target, payload.get("config"), slot=StateSlot.BACKGROUND, now=time.monotonic()
|
|
245
|
+
)
|
|
246
|
+
logger.info("restored background state %r", target)
|
|
247
|
+
except Exception as exc:
|
|
248
|
+
logger.warning("could not restore background state %r: %s", target, exc)
|
|
249
|
+
|
|
250
|
+
def _persist_background_state(self) -> None:
|
|
251
|
+
invocation = self.runtime.store.active(LayerId.BACKGROUND_STATE)
|
|
252
|
+
signature = None if invocation is None else self._signature(invocation)
|
|
253
|
+
if signature == self._persisted_signature:
|
|
254
|
+
return
|
|
255
|
+
self._persisted_signature = signature
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
self.state_file.parent.mkdir(parents=True, exist_ok=True)
|
|
259
|
+
if invocation is None:
|
|
260
|
+
self.state_file.unlink(missing_ok=True)
|
|
261
|
+
return
|
|
262
|
+
if not getattr(invocation.definition, "restorable", False):
|
|
263
|
+
self.state_file.unlink(missing_ok=True)
|
|
264
|
+
return
|
|
265
|
+
self.state_file.write_text(
|
|
266
|
+
json.dumps(
|
|
267
|
+
{
|
|
268
|
+
"schema_version": 3,
|
|
269
|
+
"effect_id": invocation.effect_id,
|
|
270
|
+
"config": invocation.params,
|
|
271
|
+
"saved_at": time.time(),
|
|
272
|
+
},
|
|
273
|
+
indent=2,
|
|
274
|
+
sort_keys=True,
|
|
275
|
+
),
|
|
276
|
+
encoding="utf-8",
|
|
277
|
+
)
|
|
278
|
+
except OSError as exc:
|
|
279
|
+
logger.warning("could not persist background state: %s", exc)
|
|
280
|
+
|
|
281
|
+
@staticmethod
|
|
282
|
+
def _signature(invocation: Invocation) -> str:
|
|
283
|
+
return json.dumps(
|
|
284
|
+
{"id": invocation.effect_id, "config": invocation.params}, sort_keys=True, default=str
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
# -- commands -----------------------------------------------------------
|
|
288
|
+
|
|
289
|
+
def _command(self, action: Callable[[], Any]) -> Any:
|
|
290
|
+
"""Run a command, then render so the change is visible immediately."""
|
|
291
|
+
with self._lock:
|
|
292
|
+
result = action()
|
|
293
|
+
self.runtime.render_once(time.monotonic())
|
|
294
|
+
self._persist_background_state()
|
|
295
|
+
return result
|
|
296
|
+
|
|
297
|
+
def set_state(
|
|
298
|
+
self, target: str, config: Mapping[str, Any] | None = None, *, slot: str = "primary",
|
|
299
|
+
action: str = "on",
|
|
300
|
+
) -> dict[str, Any]:
|
|
301
|
+
invocation = self._command(
|
|
302
|
+
lambda: self.runtime.set_state(target, config, slot=slot, action=action)
|
|
303
|
+
)
|
|
304
|
+
return self._acknowledge("set", "state", target=target, slot=slot, action=action,
|
|
305
|
+
invocation=invocation)
|
|
306
|
+
|
|
307
|
+
def clear_state(self, *, slot: str = "primary") -> dict[str, Any]:
|
|
308
|
+
self._command(lambda: self.runtime.clear_state(slot=slot))
|
|
309
|
+
return self._acknowledge("clear", "state", slot=slot)
|
|
310
|
+
|
|
311
|
+
def set_overlay(
|
|
312
|
+
self, target: str, *, channel: str | None = None,
|
|
313
|
+
config: Mapping[str, Any] | None = None, inputs: Mapping[str, Any] | None = None,
|
|
314
|
+
action: str = "on",
|
|
315
|
+
) -> dict[str, Any]:
|
|
316
|
+
invocation = self._command(
|
|
317
|
+
lambda: self.runtime.set_overlay(
|
|
318
|
+
target, channel=channel, config=config, inputs=inputs, action=action
|
|
319
|
+
)
|
|
320
|
+
)
|
|
321
|
+
return self._acknowledge("set", "overlay", target=target, channel=channel,
|
|
322
|
+
action=action, invocation=invocation)
|
|
323
|
+
|
|
324
|
+
def update_overlay(self, channel: str, inputs: Mapping[str, Any] | None = None) -> dict[str, Any]:
|
|
325
|
+
invocation = self._command(lambda: self.runtime.update_overlay(channel, inputs))
|
|
326
|
+
return self._acknowledge("update", "overlay", channel=channel, invocation=invocation)
|
|
327
|
+
|
|
328
|
+
def clear_overlay(self, channel: str) -> dict[str, Any]:
|
|
329
|
+
self._command(lambda: self.runtime.clear_overlay(channel))
|
|
330
|
+
return self._acknowledge("clear", "overlay", channel=channel)
|
|
331
|
+
|
|
332
|
+
def emit_event(
|
|
333
|
+
self, target: str, config: Mapping[str, Any] | None = None, *,
|
|
334
|
+
priority: int | None = None, duration_ms: int | None = None,
|
|
335
|
+
) -> dict[str, Any]:
|
|
336
|
+
invocation = self._command(
|
|
337
|
+
lambda: self.runtime.emit_event(
|
|
338
|
+
target, config, priority=priority, duration_ms=duration_ms
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
return self._acknowledge("emit", "event", target=target, invocation=invocation)
|
|
342
|
+
|
|
343
|
+
def set_output(
|
|
344
|
+
self, *, brightness: float | None = None, enabled: bool | None = None
|
|
345
|
+
) -> dict[str, Any]:
|
|
346
|
+
def apply() -> None:
|
|
347
|
+
if brightness is not None:
|
|
348
|
+
self.runtime.set_brightness(brightness)
|
|
349
|
+
if enabled is not None:
|
|
350
|
+
self.runtime.set_enabled(enabled)
|
|
351
|
+
|
|
352
|
+
self._command(apply)
|
|
353
|
+
return {
|
|
354
|
+
"ok": True,
|
|
355
|
+
"operation": "output",
|
|
356
|
+
"brightness": self.runtime.output.brightness,
|
|
357
|
+
"enabled": self.runtime.output.enabled,
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
def clear_all(self) -> dict[str, Any]:
|
|
361
|
+
self._command(self.runtime.clear_all)
|
|
362
|
+
return self._acknowledge("clear", "all")
|
|
363
|
+
|
|
364
|
+
def _acknowledge(
|
|
365
|
+
self, operation: str, subject: str, *, invocation: Invocation | None = None, **fields: Any
|
|
366
|
+
) -> dict[str, Any]:
|
|
367
|
+
payload: dict[str, Any] = {"ok": True, "operation": operation, "type": subject}
|
|
368
|
+
payload.update({key: value for key, value in fields.items() if value is not None})
|
|
369
|
+
if invocation is not None:
|
|
370
|
+
slot = slot_for(invocation.layer)
|
|
371
|
+
payload["applied"] = {
|
|
372
|
+
"invocation_id": invocation.invocation_id,
|
|
373
|
+
"effect_id": invocation.effect_id,
|
|
374
|
+
"layer": invocation.layer.value,
|
|
375
|
+
"slot": None if slot is None else slot.value,
|
|
376
|
+
"channel": invocation.channel,
|
|
377
|
+
"preset_id": invocation.preset_id,
|
|
378
|
+
"config": dict(invocation.params),
|
|
379
|
+
"duration_ms": invocation.duration_ms,
|
|
380
|
+
}
|
|
381
|
+
elif operation in {"set"}:
|
|
382
|
+
payload["applied"] = None
|
|
383
|
+
return payload
|
|
384
|
+
|
|
385
|
+
# -- reads --------------------------------------------------------------
|
|
386
|
+
|
|
387
|
+
def list_definitions(self, definition_type=None, *, details: bool = False) -> list[Any]:
|
|
388
|
+
with self._lock:
|
|
389
|
+
effects = self.library.registry.list_effects(definition_type=definition_type)
|
|
390
|
+
if not details:
|
|
391
|
+
return [effect.effect_id for effect in effects]
|
|
392
|
+
return [presentation.definition(effect) for effect in effects]
|
|
393
|
+
|
|
394
|
+
def list_presets(self, definition_type=None, *, details: bool = False) -> list[Any]:
|
|
395
|
+
with self._lock:
|
|
396
|
+
presets = self.library.registry.list_presets(definition_type=definition_type)
|
|
397
|
+
if not details:
|
|
398
|
+
return [item.preset_id for item in presets]
|
|
399
|
+
return [presentation.preset(item) for item in presets]
|
|
400
|
+
|
|
401
|
+
def show(self, target: str) -> dict[str, Any]:
|
|
402
|
+
with self._lock:
|
|
403
|
+
return presentation.resolved(target, self.library.registry.resolve(target))
|
|
404
|
+
|
|
405
|
+
def status(self) -> dict[str, Any]:
|
|
406
|
+
with self._lock:
|
|
407
|
+
payload = self.runtime.status(time.monotonic())
|
|
408
|
+
payload.update(
|
|
409
|
+
{
|
|
410
|
+
"service": {
|
|
411
|
+
"running": self._thread is not None and self._thread.is_alive(),
|
|
412
|
+
"started_at": self._started_at,
|
|
413
|
+
"render_count": self._render_count,
|
|
414
|
+
"last_error": self._last_error,
|
|
415
|
+
"sink": self.sink_name,
|
|
416
|
+
"input_device": self.input_device,
|
|
417
|
+
},
|
|
418
|
+
"effects": {
|
|
419
|
+
"definitions": len(self.library.registry),
|
|
420
|
+
"presets": len(self.library.registry.list_presets()),
|
|
421
|
+
"sources": self.library.sources(),
|
|
422
|
+
},
|
|
423
|
+
"input_providers": {
|
|
424
|
+
name: provider.status(time.monotonic())
|
|
425
|
+
for name, provider in self.providers.items()
|
|
426
|
+
if hasattr(provider, "status")
|
|
427
|
+
},
|
|
428
|
+
"installed": discovery.describe(),
|
|
429
|
+
}
|
|
430
|
+
)
|
|
431
|
+
return payload
|
|
432
|
+
|
|
433
|
+
def health(self) -> dict[str, Any]:
|
|
434
|
+
sink = self.sink.status()
|
|
435
|
+
return {
|
|
436
|
+
"status": "ok" if sink.available and self._last_error is None else "degraded",
|
|
437
|
+
"running": self._thread is not None and self._thread.is_alive(),
|
|
438
|
+
"sink": {"name": self.sink_name, **sink.to_dict()},
|
|
439
|
+
"render_count": self._render_count,
|
|
440
|
+
"last_error": self._last_error,
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
# -- package sources ----------------------------------------------------
|
|
444
|
+
|
|
445
|
+
def list_sources(self) -> list[dict[str, Any]]:
|
|
446
|
+
with self._lock:
|
|
447
|
+
return self.library.sources()
|
|
448
|
+
|
|
449
|
+
def register_source(self, path: str, *, enabled: bool = True) -> dict[str, Any]:
|
|
450
|
+
with self._lock:
|
|
451
|
+
entry = self.library.add_source(path, enabled=enabled)
|
|
452
|
+
self.runtime.set_registry(self.library.registry)
|
|
453
|
+
return {"ok": True, "source": entry.to_dict(), "sources": self.library.sources()}
|
|
454
|
+
|
|
455
|
+
def reload_sources(self) -> dict[str, Any]:
|
|
456
|
+
with self._lock:
|
|
457
|
+
self.library.reload()
|
|
458
|
+
self.runtime.set_registry(self.library.registry)
|
|
459
|
+
return {"ok": True, "sources": self.library.sources()}
|
|
460
|
+
|
|
461
|
+
def remove_source(self, source_id: str) -> dict[str, Any]:
|
|
462
|
+
with self._lock:
|
|
463
|
+
self.library.remove_source(source_id)
|
|
464
|
+
self.runtime.set_registry(self.library.registry)
|
|
465
|
+
return {"ok": True, "sources": self.library.sources()}
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
__all__ = ["ControllerService", "StatusListener"]
|
lefx/sdk/__init__.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""LEFX V3 SDK — the contract between effect packages and the engine.
|
|
2
|
+
|
|
3
|
+
This is the whole public surface an effect author needs. Importing anything
|
|
4
|
+
below the top level is supported but not required; importing anything outside
|
|
5
|
+
this package from an effect source is rejected by the build.
|
|
6
|
+
|
|
7
|
+
A minimal state::
|
|
8
|
+
|
|
9
|
+
from lefx.sdk import (
|
|
10
|
+
BaseEffect, ColorModel, ParamDefinition, ParamType,
|
|
11
|
+
RenderContext, StateDefinition, parse_color, scale_color,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
class SolidState(BaseEffect):
|
|
15
|
+
definition = StateDefinition(
|
|
16
|
+
id="solid",
|
|
17
|
+
title="Solid",
|
|
18
|
+
description="Fills the ring with one colour.",
|
|
19
|
+
parameter_schema={
|
|
20
|
+
"color": ParamDefinition(name="color", type=ParamType.COLOR, default="blue"),
|
|
21
|
+
"brightness": ParamDefinition(
|
|
22
|
+
name="brightness", type=ParamType.FLOAT,
|
|
23
|
+
default=1.0, minimum=0.0, maximum=1.0,
|
|
24
|
+
),
|
|
25
|
+
},
|
|
26
|
+
color_model=ColorModel.MONO,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
def render(self, ctx: RenderContext) -> list[int | None]:
|
|
30
|
+
color = scale_color(parse_color(ctx.params["color"]), ctx.params["brightness"])
|
|
31
|
+
return [color] * ctx.led_count
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from __future__ import annotations
|
|
35
|
+
|
|
36
|
+
from .calibration import (
|
|
37
|
+
DEFAULT_CALIBRATION_FILE,
|
|
38
|
+
ENV_CALIBRATION_PATH,
|
|
39
|
+
DoaCalibration,
|
|
40
|
+
as_flag,
|
|
41
|
+
calibration_path,
|
|
42
|
+
load_calibration,
|
|
43
|
+
resolve_calibration,
|
|
44
|
+
save_calibration,
|
|
45
|
+
)
|
|
46
|
+
from .color import (
|
|
47
|
+
blend,
|
|
48
|
+
clamp_channel,
|
|
49
|
+
evenly_spaced_positions,
|
|
50
|
+
position_for_angle,
|
|
51
|
+
positions_for_angle,
|
|
52
|
+
rgb,
|
|
53
|
+
scale_color,
|
|
54
|
+
sector_for_angle,
|
|
55
|
+
segment_lengths,
|
|
56
|
+
)
|
|
57
|
+
from .context import InputContext, RenderContext
|
|
58
|
+
from .definitions import (
|
|
59
|
+
AnyDefinition,
|
|
60
|
+
ColorModel,
|
|
61
|
+
CompositionMode,
|
|
62
|
+
ControlledOverlayDefinition,
|
|
63
|
+
DefinitionBase,
|
|
64
|
+
DefinitionKind,
|
|
65
|
+
DefinitionType,
|
|
66
|
+
DurationField,
|
|
67
|
+
EventDefinition,
|
|
68
|
+
InputMode,
|
|
69
|
+
InputSamplingPolicy,
|
|
70
|
+
OverlayMode,
|
|
71
|
+
StateDefinition,
|
|
72
|
+
StateSlot,
|
|
73
|
+
TimedOverlayDefinition,
|
|
74
|
+
resolved_default_configuration,
|
|
75
|
+
)
|
|
76
|
+
from .effects import BaseEffect
|
|
77
|
+
from .errors import (
|
|
78
|
+
ParameterValidationError,
|
|
79
|
+
SchemaError,
|
|
80
|
+
ValidationIssue,
|
|
81
|
+
ValueNormalizationError,
|
|
82
|
+
)
|
|
83
|
+
from .parameters import (
|
|
84
|
+
MISSING,
|
|
85
|
+
RESERVED_PARAMETERS,
|
|
86
|
+
ParamDefinition,
|
|
87
|
+
ParamType,
|
|
88
|
+
normalize_parameter_value,
|
|
89
|
+
)
|
|
90
|
+
from .ports import FrameSink, InputProvider, OutputFrame, SinkStatus
|
|
91
|
+
from .validation import (
|
|
92
|
+
initial_runtime_inputs,
|
|
93
|
+
normalize_runtime_inputs,
|
|
94
|
+
normalize_values,
|
|
95
|
+
resolve_configuration,
|
|
96
|
+
)
|
|
97
|
+
from .values import (
|
|
98
|
+
COLOR_CATALOG,
|
|
99
|
+
NamedColor,
|
|
100
|
+
describe_color,
|
|
101
|
+
format_color,
|
|
102
|
+
parse_angle_degrees,
|
|
103
|
+
parse_bool,
|
|
104
|
+
parse_color,
|
|
105
|
+
parse_duration_ms,
|
|
106
|
+
parse_ratio,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
SDK_VERSION = "3.0.0"
|
|
110
|
+
"""Contract generation. A package built against a different major will not load."""
|
|
111
|
+
|
|
112
|
+
__all__ = [
|
|
113
|
+
"AnyDefinition",
|
|
114
|
+
"BaseEffect",
|
|
115
|
+
"COLOR_CATALOG",
|
|
116
|
+
"ColorModel",
|
|
117
|
+
"CompositionMode",
|
|
118
|
+
"ControlledOverlayDefinition",
|
|
119
|
+
"DefinitionBase",
|
|
120
|
+
"DefinitionKind",
|
|
121
|
+
"DEFAULT_CALIBRATION_FILE",
|
|
122
|
+
"DefinitionType",
|
|
123
|
+
"DoaCalibration",
|
|
124
|
+
"DurationField",
|
|
125
|
+
"ENV_CALIBRATION_PATH",
|
|
126
|
+
"EventDefinition",
|
|
127
|
+
"FrameSink",
|
|
128
|
+
"InputContext",
|
|
129
|
+
"InputMode",
|
|
130
|
+
"InputProvider",
|
|
131
|
+
"InputSamplingPolicy",
|
|
132
|
+
"MISSING",
|
|
133
|
+
"NamedColor",
|
|
134
|
+
"OutputFrame",
|
|
135
|
+
"OverlayMode",
|
|
136
|
+
"ParamDefinition",
|
|
137
|
+
"ParamType",
|
|
138
|
+
"ParameterValidationError",
|
|
139
|
+
"RESERVED_PARAMETERS",
|
|
140
|
+
"RenderContext",
|
|
141
|
+
"SDK_VERSION",
|
|
142
|
+
"SchemaError",
|
|
143
|
+
"SinkStatus",
|
|
144
|
+
"StateDefinition",
|
|
145
|
+
"StateSlot",
|
|
146
|
+
"TimedOverlayDefinition",
|
|
147
|
+
"ValidationIssue",
|
|
148
|
+
"ValueNormalizationError",
|
|
149
|
+
"blend",
|
|
150
|
+
"clamp_channel",
|
|
151
|
+
"describe_color",
|
|
152
|
+
"evenly_spaced_positions",
|
|
153
|
+
"format_color",
|
|
154
|
+
"initial_runtime_inputs",
|
|
155
|
+
"normalize_parameter_value",
|
|
156
|
+
"normalize_runtime_inputs",
|
|
157
|
+
"normalize_values",
|
|
158
|
+
"parse_angle_degrees",
|
|
159
|
+
"parse_bool",
|
|
160
|
+
"parse_color",
|
|
161
|
+
"parse_duration_ms",
|
|
162
|
+
"parse_ratio",
|
|
163
|
+
"as_flag",
|
|
164
|
+
"calibration_path",
|
|
165
|
+
"load_calibration",
|
|
166
|
+
"position_for_angle",
|
|
167
|
+
"positions_for_angle",
|
|
168
|
+
"resolve_calibration",
|
|
169
|
+
"save_calibration",
|
|
170
|
+
"sector_for_angle",
|
|
171
|
+
"resolve_configuration",
|
|
172
|
+
"resolved_default_configuration",
|
|
173
|
+
"rgb",
|
|
174
|
+
"scale_color",
|
|
175
|
+
"segment_lengths",
|
|
176
|
+
]
|