Files
led-controller/test/api/test_sim_net.py
T
jimmyandClaude Opus 5.5 35207a868a sim: fixture editor for DDP and Art-Net/DMX fixtures
The rig sim receives Art-Net (UDP 6454, answers ArtPoll) and DDP (each port the
room's DDP strips use) in sim_net.c and passes changed universes and pixels to
the page over /sim/ws, now with a kind byte in front of each binary frame.

Rooms gain fixture types: ddp strips, artnet pixel strips, and artnet cans and
movers that read named channels from the room's dmx_definitions (the qlcplus
stage map's format). Overlapping DMX channels are refused. Cans and movers are
decoded and drawn as in the qlcplus stage simulator (dmx.js, dmx-draw.js).

In Edit mode: pick a type to add, edit port/offset/universe/address/colour
order/definition, see a can's or mover's live channels, make a controller
output aimed at the sim and send its test. The room dialog lists DMX
definitions, with presets, a channel editor, and .qxf and stage map import.

GET /sim/api/dmx shows what the receiver has had. Tests: test/api/test_sim_net.py
and test/browser/dmx_decode.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:13:58 +13:00

226 lines
9.5 KiB
Python

"""The simulated rig's network fixtures (sim-controller/components/sim/sim_net.c and sim_rig.c): rooms with DDP and
Art-Net fixtures, and the Art-Net / DDP receiver behind GET /sim/api/dmx.
make sim-web # the controller with the rig at /sim/
python3 -m pytest test/api/test_sim_net.py --host localhost:8080
Skipped unless the controller serves /sim/api/dmx. The receiver must be on this machine (packets go to the
controller's host on UDP 6454 and 4048; LED_SIM_ARTNET_PORT moves the Art-Net one).
"""
import os
import socket
import time
from urllib.parse import urlparse
import pytest
from helpers import unique
ARTNET_PORT = int(os.environ.get("LED_SIM_ARTNET_PORT", "6454"))
DDP_PORT = 4048
@pytest.fixture(scope="module")
def sim(c):
r = c.get("/sim/api/dmx")
if r.status_code != 200:
pytest.skip("no simulated rig with network fixtures here")
return r.json()
@pytest.fixture(scope="module")
def udp_host(base_url):
return urlparse(base_url).hostname
@pytest.fixture
def room(c, sim):
"""A new room, deleted afterwards; yields its name."""
name = unique("net")[:40].replace(" ", "-")
r = c.post("/sim/api/rooms", json={"name": name, "width": 6, "depth": 4})
assert r.status_code == 200, r.text
yield name
current = c.get("/sim/api/rooms").json()["current"]
if current == name:
c.post("/sim/api/room", json={"name": "default"})
c.delete(f"/sim/api/rooms/{name}")
BCC145 = {"manufacturer": "beamZ", "model": "BCC145", "mode": "8 Channel",
"channels": ["Dimmer", "Strobe", "Red", "Green", "Blue", "White", "Macro", "Speed"]}
def base_room(fixtures, dmx_definitions=None):
return {"width": 6, "depth": 4, "definitions": {"bar-119": {"leds": 119}},
"dmx_definitions": dmx_definitions or {"beamZ BCC145": BCC145}, "fixtures": fixtures}
def put(c, name, body):
return c.put(f"/sim/api/rooms/{name}", json=body)
def artdmx(universe, data):
data = bytes(data) + b"\0" * (len(data) & 1)
n = len(data)
return (b"Art-Net\0" + bytes([0x00, 0x50, 0, 14, 1, 0, universe & 0xFF, (universe >> 8) & 0x7F, n >> 8, n & 0xFF])
+ data)
def ddp(offset, data, push=True):
n = len(data)
return bytes([0x40 | (1 if push else 0), 1, 0x0B, 1]) + offset.to_bytes(4, "big") + n.to_bytes(2, "big") + bytes(data)
def send(host, port, pkt):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(pkt, (host, port))
def status(c, data=False):
return c.get("/sim/api/dmx" + ("?data=1" if data else "")).json()
def wait_for(fn, timeout=3.0):
end = time.time() + timeout
while time.time() < end:
v = fn()
if v:
return v
time.sleep(0.05)
return fn()
# --- room schema ---------------------------------------------------------------------------------------------
def test_old_rooms_default_to_espnow(c, room):
r = put(c, room, base_room({"a": {"def": "bar-119", "mac": "02:51:4d:00:10:01", "x": 0, "y": 1}}))
assert r.status_code == 200, r.text
f = r.json()["room"]["fixtures"]["a"]
assert f["type"] == "espnow" and f["mac"] == "02:51:4d:00:10:01"
def test_ddp_and_artnet_fixtures_round_trip(c, room):
body = base_room({
"strip": {"type": "ddp", "def": "bar-119", "leds": 60, "port": 4049, "offset": 10, "color_order": "grb"},
"px": {"type": "artnet", "kind": "pixels", "def": "bar-119", "leds": 200, "universe": 1, "address": 1},
"can1": {"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 0, "address": 9},
"mover": {"type": "artnet", "kind": "mover", "dmx_def": "beamZ BCC145", "universe": 0, "address": 17,
"zero": 45, "pan_reverse": True},
})
r = put(c, room, body)
assert r.status_code == 200, r.text
fx = c.get(f"/sim/api/rooms/{room}").json()["fixtures"]
assert fx["strip"]["port"] == 4049 and fx["strip"]["offset"] == 10
assert fx["strip"]["color_order"] == "grb" and "mac" not in fx["strip"]
assert fx["px"]["kind"] == "pixels" and fx["px"]["color_order"] == "rgb"
assert fx["can1"]["dmx_def"] == "beamZ BCC145" and "def" not in fx["can1"]
assert fx["mover"]["zero"] == 45 and fx["mover"]["pan_reverse"] is True
assert c.get(f"/sim/api/rooms/{room}").json()["dmx_definitions"]["beamZ BCC145"]["channels"][2] == "Red"
@pytest.mark.parametrize("fixture, words", [
({"type": "dmx"}, "type is"),
({"type": "artnet", "kind": "laser", "universe": 0, "address": 1}, "kind is"),
({"type": "artnet", "kind": "can", "dmx_def": "nope", "universe": 0, "address": 1}, "dmx_def"),
({"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 0, "address": 600}, "address"),
({"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 0, "address": 510}, "past channel 512"),
({"type": "ddp", "def": "bar-119", "port": 70000}, "port"),
({"type": "ddp", "def": "bar-119", "color_order": "rgbw"}, "color_order"),
({"type": "ddp"}, "strip type"),
])
def test_bad_fixtures_are_refused(c, room, fixture, words):
r = put(c, room, base_room({"f": fixture}))
assert r.status_code == 400 and words in r.json()["error"], r.text
def test_dmx_fixtures_may_not_share_channels(c, room):
body = base_room({
"a": {"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 0, "address": 1},
"b": {"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 0, "address": 8},
})
r = put(c, room, body)
assert r.status_code == 400 and "share DMX channels" in r.json()["error"]
body["fixtures"]["b"]["address"] = 9
assert put(c, room, body).status_code == 200
body["fixtures"]["b"]["universe"] = 1
body["fixtures"]["b"]["address"] = 1
assert put(c, room, body).status_code == 200
def test_pixels_run_on_into_the_next_universe(c, room):
# 200 pixels from universe 0: 170 there, 30 in universe 1, which a can at 1 then overlaps
body = base_room({
"px": {"type": "artnet", "kind": "pixels", "def": "bar-119", "leds": 200, "universe": 0, "address": 1},
"can": {"type": "artnet", "kind": "can", "dmx_def": "beamZ BCC145", "universe": 1, "address": 1},
})
assert put(c, room, body).status_code == 400
body["fixtures"]["can"]["address"] = 91 # past 30 pixels (90 channels)
assert put(c, room, body).status_code == 200
def test_bad_dmx_definitions_are_refused(c, room):
r = put(c, room, base_room({}, {"empty": {"channels": []}}))
assert r.status_code == 400 and "channels" in r.json()["error"]
# --- the receiver --------------------------------------------------------------------------------------------
def test_artnet_universe_is_received(c, sim, udp_host):
if not sim["artnet"]["listening"]:
pytest.skip("Art-Net port not open: " + sim["artnet"].get("error", ""))
before = sim["artnet"]["packets"]
frame = bytes([255, 0, 200, 10, 20]) + bytes(507)
send(udp_host, ARTNET_PORT, artdmx(7, frame))
got = wait_for(lambda: status(c, True)["artnet"]["universes"].get("7"))
assert got and got["data"].startswith("ff00c80a14")
assert status(c)["artnet"]["packets"] > before
def test_artpoll_gets_a_reply(sim, udp_host):
if not sim["artnet"]["listening"]:
pytest.skip("Art-Net port not open")
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(2)
s.sendto(b"Art-Net\0" + bytes([0x00, 0x20, 0, 14, 0, 0]), (udp_host, ARTNET_PORT))
reply, _ = s.recvfrom(1024)
assert reply.startswith(b"Art-Net\0") and reply[8:10] == bytes([0x00, 0x21])
assert b"led-sim" in reply
def test_ddp_port_follows_the_active_room(c, room, udp_host):
body = base_room({"s": {"type": "ddp", "def": "bar-119", "leds": 10, "port": 4051}})
assert put(c, room, body).status_code == 200
assert c.post("/sim/api/room", json={"name": room}).status_code == 200
assert wait_for(lambda: status(c)["ddp"].get("4051", {}).get("listening"))
send(udp_host, 4051, ddp(3, [1, 2, 3, 4, 5, 6]))
got = wait_for(lambda: status(c, True)["ddp"]["4051"]["packets"] and status(c, True)["ddp"]["4051"])
assert got["data"] == "000000" + "010203040506" and got["pixels"] == 3
c.post("/sim/api/room", json={"name": "default"})
assert wait_for(lambda: "4051" not in status(c)["ddp"])
def test_controller_artnet_output_reaches_the_sim(c, sim):
"""A controller output aimed at the sim: its test frames land in the universe. The test request holds the
HTTP server until red, green, blue, white and black have gone out, so only the end state can be read here."""
if not sim["artnet"]["listening"]:
pytest.skip("Art-Net port not open")
r = c.post("/outputs", json={"name": unique("sim-out"), "protocol": "artnet", "host": "127.0.0.1",
"port": ARTNET_PORT, "universe": 300, "pixel_count": 4, "enabled": True})
if r.status_code not in (200, 201):
pytest.skip(f"outputs not available: {r.status_code}")
oid = r.json().get("id")
before = status(c)["artnet"]["universes"].get("300", {}).get("packets", 0)
try:
r = c.post(f"/outputs/{oid}/test", json={"step_s": 0.2}, timeout=20)
assert r.status_code == 200, r.text
sent = r.json()["status"]["packets"]
u = wait_for(lambda: (s := status(c, True)["artnet"]["universes"].get("300")) and
s["packets"] - before >= sent and s)
assert u and u["packets"] - before >= sent >= 5, (u, sent)
assert bytes.fromhex(u["data"])[:12] == bytes(12) # ends on black
finally:
c.delete(f"/outputs/{oid}")