Commit b00f5963 authored by vertighel's avatar vertighel
Browse files

refactor: web/__init__.py — _camera_views() da cameras.ini, no _VIEWER_MAP



_configured_cameras() e _camera_views() leggono cameras.ini.
WS 'refresh' e tile-request usano cam_id.
Viewer route usa _camera_views() per costruire panels.

Co-Authored-By: default avatarClaude Sonnet 4.6 <noreply@anthropic.com>
parent cdefb5ce
Loading
Loading
Loading
Loading
+41 −60
Original line number Diff line number Diff line
@@ -32,24 +32,37 @@ streamer = StreamManager(broadcaster)


def _configured_cameras():
    """
    Read ``viewer.ini`` and return all ``(station, camera)`` pairs.
    """Read cameras.ini and return all cam_id strings."""
    cfg = configparser.ConfigParser()
    cfg.read(Path(__file__).parent.parent / 'config' / 'cameras.ini')
    return list(cfg.sections())

    Returns
    -------
    list of tuple
        Each element is ``(station, camera)``.

def _camera_views():
    """
    Build the viewer name → panel list map from cameras.ini.

    Returns a dict where each key is a view name ('scicam1', 'combo1', …)
    and each value is a list of panel dicts with 'id', 'cam_id', 'has_loop'.
    """
    cfg = configparser.ConfigParser()
    cfg_path = Path(__file__).parent.parent / 'config' / 'viewer.ini'
    cfg.read(cfg_path)
    cameras = []
    for section in cfg.sections():
        parts = section.split('/', 1)
        if len(parts) == 2:
            cameras.append((parts[0], parts[1]))
    return cameras
    cfg.read(Path(__file__).parent.parent / 'config' / 'cameras.ini')

    views   = {}
    by_station = {}

    for cam_id in cfg.sections():
        has_loop = cfg.get(cam_id, 'device', fallback=None) is not None
        station  = cfg.getint(cam_id, 'station', fallback=None)
        panel    = {'id': cam_id, 'cam_id': cam_id, 'has_loop': has_loop}
        views[cam_id] = [panel]
        if station is not None:
            by_station.setdefault(station, []).append(panel)

    for n, panels in sorted(by_station.items()):
        views[f'combo{n}'] = sorted(panels, key=lambda p: p['cam_id'])

    return views


@web_blueprint.before_app_serving
@@ -142,11 +155,10 @@ async def ws():
                log.info(f"WebSocket: force_enabled → {streamer.force_enabled}")

            elif action == "refresh":
                station = msg.get("station", "")
                camera  = msg.get("camera",  "")
                if station and camera:
                cam_id = msg.get("cam_id", "")
                if cam_id:
                    asyncio.create_task(
                        streamer.broadcast_preview_now(station, camera)
                        streamer.broadcast_preview_now(cam_id)
                    )

            elif action == "tile-request":
@@ -186,15 +198,14 @@ async def _handle_tile_request(ws_conn, msg: dict) -> None:
    msg : dict
        Parsed JSON from the client.
    """
    station = msg.get("station", "")
    camera  = msg.get("camera",  "")
    cam_id = msg.get("cam_id", "")
    cx     = int(msg.get("cx", 0))
    cy     = int(msg.get("cy", 0))
    rx     = int(msg.get("rx", 50))
    ry     = int(msg.get("ry", 50))
    seq    = msg.get("seq", 0)

    data = get_cached_data(station, camera)
    data = get_cached_data(cam_id)
    if data is None:
        return

@@ -356,26 +367,6 @@ async def subsystem_page(subsystem_name, endpoint_name=None):
    )


_VIEWER_MAP = {
    'scicam1': [{'id': 'scicam1', 'station': 'station1', 'camera': 'scicam'}],
    'scicam2': [{'id': 'scicam2', 'station': 'station2', 'camera': 'scicam'}],
    'scicam3': [{'id': 'scicam3', 'station': 'station3', 'camera': 'scicam'}],
    'teccam1': [{'id': 'teccam1', 'station': 'station1', 'camera': 'teccam'}],
    'teccam2': [{'id': 'teccam2', 'station': 'station2', 'camera': 'teccam'}],
    'teccam3': [{'id': 'teccam3', 'station': 'station3', 'camera': 'teccam'}],
    'combo1':  [
        {'id': 'combo1-sci', 'station': 'station1', 'camera': 'scicam'},
        {'id': 'combo1-tec', 'station': 'station1', 'camera': 'teccam'},
    ],
    'combo2':  [
        {'id': 'combo2-sci', 'station': 'station2', 'camera': 'scicam'},
        {'id': 'combo2-tec', 'station': 'station2', 'camera': 'teccam'},
    ],
    'combo3':  [
        {'id': 'combo3-sci', 'station': 'station3', 'camera': 'scicam'},
        {'id': 'combo3-tec', 'station': 'station3', 'camera': 'teccam'},
    ],
}


@web_blueprint.route('/viewer')
@@ -392,7 +383,7 @@ async def viewer(name):
    Parameters
    ----------
    name : str
        One of the keys in ``_VIEWER_MAP`` (e.g. ``'combo1'``, ``'scicam2'``).
        A cam_id (e.g. ``'scicam1'``) or combo key (e.g. ``'combo1'``).

    Returns
    -------
@@ -401,20 +392,10 @@ async def viewer(name):
    """
    from quart import abort

    panel_defs = _VIEWER_MAP.get(name)
    if panel_defs is None:
    panels = _camera_views().get(name)
    if panels is None:
        abort(404)

    _vcfg = configparser.ConfigParser()
    _vcfg.read(Path(__file__).parent.parent / 'config' / 'viewer.ini')

    panels = []
    for p in panel_defs:
        has_loop = _vcfg.get(
            f"{p['station']}/{p['camera']}", 'device', fallback=None
        ) is not None
        panels.append({**p, 'has_loop': has_loop})

    return await render_template(
        'viewer.html',
        view_name=name,