host.camera_visualizer.routes.config

Robot config and page building routes.

 1"""Robot config and page building routes."""
 2
 3import importlib
 4import json
 5import os
 6
 7_IN = 0.0254
 8
 9
10def reload_geometry():
11    """Re-import robot_geometry so edits are picked up on browser refresh."""
12    import constants.robot_geometry as geo
13    importlib.reload(geo)
14    return geo
15
16
17def build_config_json() -> str:
18    """Build the JSON config blob for the visualizer."""
19    geo = reload_geometry()
20    config = {
21        "robot_frame": {
22            "width_inches": round(geo.ROBOT_WIDTH_M / _IN, 2),
23            "length_inches": round(geo.ROBOT_LENGTH_M / _IN, 2),
24            "width_meters": round(geo.ROBOT_WIDTH_M, 4),
25            "length_meters": round(geo.ROBOT_LENGTH_M, 4),
26        },
27        "cameras": [cam.to_dict() for cam in geo.CAMERAS],
28        "mechanisms": [mech.to_dict() for mech in geo.MECHANISMS],
29    }
30    return json.dumps(config, indent=2)
31
32
33def build_page(html_dir: str) -> bytes:
34    """Read index.html and inject the config JSON."""
35    html_path = os.path.join(html_dir, 'index.html')
36    with open(html_path, 'r', encoding='utf-8') as f:
37        html = f.read()
38
39    config_json = build_config_json()
40    inject = f'<script>window.__ROBOT_CONFIG__ = {config_json};</script>\n'
41    html = html.replace(
42        '<script type="module">',
43        inject + '<script type="module">',
44        1,
45    )
46    return html.encode('utf-8')
def reload_geometry():
11def reload_geometry():
12    """Re-import robot_geometry so edits are picked up on browser refresh."""
13    import constants.robot_geometry as geo
14    importlib.reload(geo)
15    return geo

Re-import robot_geometry so edits are picked up on browser refresh.

def build_config_json() -> str:
18def build_config_json() -> str:
19    """Build the JSON config blob for the visualizer."""
20    geo = reload_geometry()
21    config = {
22        "robot_frame": {
23            "width_inches": round(geo.ROBOT_WIDTH_M / _IN, 2),
24            "length_inches": round(geo.ROBOT_LENGTH_M / _IN, 2),
25            "width_meters": round(geo.ROBOT_WIDTH_M, 4),
26            "length_meters": round(geo.ROBOT_LENGTH_M, 4),
27        },
28        "cameras": [cam.to_dict() for cam in geo.CAMERAS],
29        "mechanisms": [mech.to_dict() for mech in geo.MECHANISMS],
30    }
31    return json.dumps(config, indent=2)

Build the JSON config blob for the visualizer.

def build_page(html_dir: str) -> bytes:
34def build_page(html_dir: str) -> bytes:
35    """Read index.html and inject the config JSON."""
36    html_path = os.path.join(html_dir, 'index.html')
37    with open(html_path, 'r', encoding='utf-8') as f:
38        html = f.read()
39
40    config_json = build_config_json()
41    inject = f'<script>window.__ROBOT_CONFIG__ = {config_json};</script>\n'
42    html = html.replace(
43        '<script type="module">',
44        inject + '<script type="module">',
45        1,
46    )
47    return html.encode('utf-8')

Read index.html and inject the config JSON.