"""Stage simulator: Art-Net decode and the XCHC patch.""" from __future__ import annotations import json import threading from pathlib import Path from urllib.request import urlopen from simulator.artnet import ArtNetNode, build_artdmx, parse_artdmx, parse_opcode from simulator.decode import apply_cto, decode_can, decode_mover, lens_angle, lens_spin, mix_rgbw, view_stage from simulator.server import Simulator, make_server from simulator.stage import apply_show, load_stage, set_heading, wrap_degrees ROOT = Path(__file__).resolve().parents[1] def test_artdmx_roundtrip(): frame = bytearray(512) frame[87] = 255 frame[89] = 200 packet = build_artdmx(frame, universe=0, sequence=3) parsed = parse_artdmx(packet) assert parsed is not None universe, data = parsed assert universe == 0 assert data[87] == 255 assert data[89] == 200 assert parse_opcode(packet) == 0x5000 def test_poll_is_not_dmx(): packet = b"Art-Net\x00" + bytes([0x00, 0x20]) + bytes(10) assert parse_opcode(packet) == 0x2000 assert parse_artdmx(packet) is None def test_xchc_patch_matches_the_stage_map(): stage = load_stage(ROOT / "stage" / "map.json") apply_show(stage, ROOT / "shows" / "xchc.qxw") by_id = {fixture.id: fixture for fixture in stage.fixtures} assert by_id["#1"].address == 87 assert by_id["#1"].dmx == 88 assert by_id["#8"].address == 143 assert by_id["M1"].address == 0 assert by_id["M1"].dmx == 1 assert by_id["M2"].address == 44 assert by_id["M1"].channels == 43 assert by_id["#3"].kind == "can" assert stage.output_ip == "192.168.1.228" assert stage.name == "xchc" assert by_id["#9"].x == -3.5 and by_id["#9"].y == -0.8 assert by_id["#11"].y == -6.8 and by_id["#12"].x == 3.5 assert by_id["#13"].x == -3.5 and by_id["#15"].x == 3.5 assert by_id["#14"].x == 0.0 and by_id["#14"].y == -7.65 assert by_id["#1"].address == 87 and by_id["#9"].address == 151 assert by_id["#15"].address == 199 and by_id["M1"].address == 0 assert by_id["M2"].address == 44 and by_id["#15"].channels == 8 assert by_id["M3"].address == 257 and by_id["M3"].channels == 43 assert by_id["M3"].x == -1.75 and by_id["M3"].y == -7.65 assert by_id["M4"].address == 300 and by_id["M4"].x == 1.75 assert by_id["#1"].direction == 90 and by_id["#8"].direction == -90 assert by_id["#3"].direction == 0 and by_id["#14"].direction == 0 assert by_id["M1"].zero == 0 and by_id["M4"].zero == 0 assert stage.warnings == [] def test_can_red_and_white(): assert mix_rgbw(0, 0, 0, 255, 255) == (255, 255, 255) viewed = decode_can([255, 0, 255, 0, 0, 0, 0, 0]) assert (viewed["r"], viewed["g"], viewed["b"]) == (255, 0, 0) assert viewed["function"] == "mix" assert viewed["strobe_hz"] == 0 assert decode_can([255, 255, 255, 0, 0, 0, 0, 0])["strobe_hz"] > 0 assert decode_can([128, 0, 0, 0, 0, 0, 120, 0])["effect"] is True def test_can_macro_functions(): assert decode_can([255, 0, 0, 0, 0, 0, 51, 0])["function"] == "fixed" assert decode_can([255, 0, 0, 0, 0, 0, 51, 0])["r"] == 255 assert decode_can([0, 0, 0, 0, 0, 0, 80, 0])["r"] == 0 jump = decode_can([255, 0, 0, 0, 0, 0, 120, 200]) assert jump["function"] == "jump" assert jump["speed_hz"] > 1 assert jump["palette"][0] == [255, 0, 0] pulse = decode_can([255, 0, 255, 80, 0, 0, 170, 0]) assert pulse["function"] == "pulse" assert pulse["r"] == 255 and pulse["g"] == 80 assert decode_can([255, 0, 0, 0, 0, 0, 220, 0])["function"] == "gradient" sound = decode_can([255, 0, 0, 0, 0, 0, 255, 0]) assert sound["function"] == "sound" and sound["sound"] == 2 def test_mover_home_points_at_the_audience(): channels = [0] * 43 channels[0] = 128 # pan coarse, fine 0 → 32768, the centre of 540° channels[2] = 160 channels[8] = 255 channels[10] = 255 # red on the first pixel viewed = decode_mover(channels) assert viewed["pan_deg"] == 0 assert viewed["r"] == 255 assert viewed["beam_m"] > 2 parked = [0] * 43 parked[8] = 255 assert decode_mover(parked)["beam_m"] >= 2.2 assert lens_spin(0) == 0 assert lens_spin(160) < 0 assert lens_spin(230) > 0 assert lens_angle(0) == 0 assert 25 < lens_angle(64) < 35 def test_mover_functions(): channels = [0] * 43 channels[5] = 230 channels[6] = 255 channels[8] = 255 channels[39] = 20 macro = decode_mover(channels) assert macro["function"] == "macro" assert macro["r"] == 255 and macro["b"] == 0 assert macro["zoom_deg"] == 45 assert macro["spin_dps"] > 0 channels[40] = 103 static = decode_mover(channels) assert static["function"] == "static" assert static["pixels"][3][0] == 255 assert static["pixels"][0] == [0, 0, 0] channels[41] = 30 dynamic = decode_mover(channels) assert dynamic["function"] == "dynamic" assert dynamic["dynamic"] == "chase" assert dynamic["pixels"][0][0] == 255 assert dynamic["speed_hz"] > 0 warm = [0] * 43 warm[8] = 255 warm[10] = warm[11] = warm[12] = 255 warm[38] = 255 tinted = decode_mover(warm) assert tinted["r"] == 255 assert tinted["b"] < apply_cto(255, 255, 255, 0)[2] assert tinted["b"] < 100 strobe = [0] * 43 strobe[9] = 240 assert decode_mover(strobe)["strobe_mode"] == "random" assert decode_mover([0] * 43)["strobe_mode"] == "open" def test_map_definition_selects_the_channels(): stage = load_stage(ROOT / "stage" / "map.json") mover = next(fixture for fixture in stage.fixtures if fixture.id == "M1") can = next(fixture for fixture in stage.fixtures if fixture.id == "#1") assert can.channel_names[:3] == ["Dimmer", "Strobe", "Red"] assert mover.channel_names[5] == "Lens Rotate" assert mover.channel_names[6] == "Zoom" assert mover.mode == "43 Channel" frame = bytearray(512) frame[mover.address + mover.channel_names.index("Pan")] = 128 frame[mover.address + mover.channel_names.index("Zoom")] = 255 frame[mover.address + mover.channel_names.index("Center Brightness")] = 255 frame[mover.address + mover.channel_names.index("Red 1")] = 255 frame[mover.address + mover.channel_names.index("Lens Rotate")] = 230 live = view_stage(stage, {0: bytes(frame)})["M1"] assert live["zoom_deg"] == 45 assert live["spin_dps"] > 0 assert live["r"] == 255 assert live["pan_deg"] == 0 def test_view_reads_universe_bytes(): stage = load_stage(ROOT / "stage" / "map.json") apply_show(stage, ROOT / "shows" / "xchc.qxw") frame = bytearray(512) frame[87] = 255 frame[89] = 0 frame[90] = 255 live = view_stage(stage, {0: bytes(frame)}) assert (live["#1"]["r"], live["#1"]["g"], live["#1"]["b"]) == (0, 255, 0) assert live["#2"]["dimmer"] == 0 def test_http_state_follows_artnet(): stage = load_stage(ROOT / "stage" / "map.json") apply_show(stage, ROOT / "shows" / "xchc.qxw") node = ArtNetNode("127.0.0.1", 0) node.start() server = make_server(Simulator(stage, node), "127.0.0.1", 0) thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() try: frame = bytearray(512) frame[0] = 128 frame[8] = 255 frame[10] = 255 node.write(0, frame) port = server.server_address[1] with urlopen(f"http://127.0.0.1:{port}/api/state") as response: payload = json.load(response) assert payload["packets"] == 1 assert payload["fixtures"]["M1"]["r"] == 255 with urlopen(f"http://127.0.0.1:{port}/api/stage") as response: described = json.load(response) assert described["fixtures"][0]["x"] == -3.5 finally: server.shutdown() node.close() def test_heading_is_stored_in_the_map(tmp_path): target = tmp_path / "map.json" target.write_text((ROOT / "stage" / "map.json").read_text()) stage = load_stage(target) assert wrap_degrees(270) == -90 set_heading(stage, "#1", direction=45) set_heading(stage, "M1", zero=-30) saved = json.loads(target.read_text()) assert saved["fixtures"]["#1"]["direction"] == 45 assert saved["fixtures"]["#2"]["direction"] == 90 assert saved["fixtures"]["M1"]["zero"] == -30 assert saved["fixtures"]["M2"]["zero"] == 0 again = load_stage(target) by_id = {fixture.id: fixture for fixture in again.fixtures} assert by_id["#1"].direction == 45 assert by_id["M1"].zero == -30