diff --git a/stage_mqtt/stage_mqtt_install.sh b/stage_mqtt/stage_mqtt_install.sh index 0da0a1d..7df2b9c 100755 --- a/stage_mqtt/stage_mqtt_install.sh +++ b/stage_mqtt/stage_mqtt_install.sh @@ -499,6 +499,7 @@ topic iomatex/+/client/pub/action/${DEVICE_ID} out 0 "" "" topic iomatex/+/client/pub/module/${DEVICE_ID}/scanning out 0 "" "" topic iomatex/+/client/pub/camera/${DEVICE_ID}/scanning out 0 "" "" topic iomatex/+/client/pub/camera/${DEVICE_ID}/ptz out 1 "" "" +topic iomatex/+/client/pub/camera/${DEVICE_ID}/stream out 1 "" "" topic iomatex/+/client/pub/response/event/push out 0 "" "" topic iomatex/+/client/pub/interface/${DEVICE_ID}/# out 0 "" "" topic iomatex/+/client/pub/interface/meta/${DEVICE_ID} out 0 "" "" @@ -524,6 +525,7 @@ topic iomatex/+/client/sub/action/${DEVICE_ID} in 0 "" "" topic iomatex/+/client/sub/module/${DEVICE_ID}/scanning in 0 "" "" topic iomatex/+/client/sub/camera/${DEVICE_ID}/scanning in 0 "" "" topic iomatex/+/client/sub/camera/${DEVICE_ID}/ptz in 1 "" "" +topic iomatex/+/client/sub/camera/${DEVICE_ID}/stream in 1 "" "" topic iomatex/+/client/sub/event/speech/widget/${DEVICE_ID} in 0 "" "" topic iomatex/+/client/sub/interface/${DEVICE_ID} in 0 "" "" topic iomatex/+/client/sub/interface/action/${DEVICE_ID} in 0 "" "" @@ -558,6 +560,7 @@ topic it-screen/+/client/pub/action/${DEVICE_ID} out 0 "" "" topic it-screen/+/client/pub/module/${DEVICE_ID}/scanning out 0 "" "" topic it-screen/+/client/pub/camera/${DEVICE_ID}/scanning out 0 "" "" topic it-screen/+/client/pub/camera/${DEVICE_ID}/ptz out 1 "" "" +topic it-screen/+/client/pub/camera/${DEVICE_ID}/stream out 1 "" "" topic it-screen/+/client/pub/response/event/push out 0 "" "" topic it-screen/+/client/pub/interface/${DEVICE_ID}/# out 0 "" "" topic it-screen/+/client/pub/interface/meta/${DEVICE_ID} out 0 "" "" @@ -583,6 +586,7 @@ topic it-screen/+/client/sub/action/${DEVICE_ID} in 0 "" "" topic it-screen/+/client/sub/module/${DEVICE_ID}/scanning in 0 "" "" topic it-screen/+/client/sub/camera/${DEVICE_ID}/scanning in 0 "" "" topic it-screen/+/client/sub/camera/${DEVICE_ID}/ptz in 1 "" "" +topic it-screen/+/client/sub/camera/${DEVICE_ID}/stream in 1 "" "" topic it-screen/+/client/sub/event/speech/widget/${DEVICE_ID} in 0 "" "" topic it-screen/+/client/sub/interface/${DEVICE_ID} in 0 "" "" topic it-screen/+/client/sub/interface/action/${DEVICE_ID} in 0 "" "" @@ -934,7 +938,7 @@ interfaces = profile.get("interfaces") if not isinstance(interfaces, dict): interfaces = { key: {"state": "unknown"} - for key in ("rs485", "screen", "radio", "wifi", "ethernet") + for key in ("rs485", "screen", "radio", "wifi", "ethernet", "camera") } payload = { @@ -981,7 +985,7 @@ except (TypeError, ValueError): interfaces = profile.get("interfaces") or {} print(",".join( f"{name}={str((interfaces.get(name) or {}).get('state') or 'unknown')}" - for name in ("rs485", "screen", "radio", "wifi", "ethernet") + for name in ("rs485", "screen", "radio", "wifi", "ethernet", "camera") )) __IOMATEX_HEALTH_SUMMARY__ )" diff --git a/stage_mqtt/test_mqtt_bridge_contract.py b/stage_mqtt/test_mqtt_bridge_contract.py index b1e6006..eb24c07 100644 --- a/stage_mqtt/test_mqtt_bridge_contract.py +++ b/stage_mqtt/test_mqtt_bridge_contract.py @@ -49,7 +49,7 @@ class MqttBridgeContractTest(unittest.TestCase): self.source, ) self.assertIn('"interfaces": interfaces', self.source) - for interface_name in ("rs485", "screen", "radio", "wifi", "ethernet"): + for interface_name in ("rs485", "screen", "radio", "wifi", "ethernet", "camera"): self.assertIn(f'"{interface_name}"', self.source) def test_health_timestamp_and_transition_logging_follow_diagnostics(self): @@ -74,6 +74,14 @@ class MqttBridgeContractTest(unittest.TestCase): f"topic {prefix}/+/client/sub/interface/${{DEVICE_ID}} in", self.source, ) + self.assertIn( + f"topic {prefix}/+/client/pub/camera/${{DEVICE_ID}}/stream out 1", + self.source, + ) + self.assertIn( + f"topic {prefix}/+/client/sub/camera/${{DEVICE_ID}}/stream in 1", + self.source, + ) self.assertNotIn( f"topic {prefix}/+/client/pub/# in", self.source, diff --git a/stage_ui/console_core/backend/app.py b/stage_ui/console_core/backend/app.py index d5659b2..e82966f 100644 --- a/stage_ui/console_core/backend/app.py +++ b/stage_ui/console_core/backend/app.py @@ -2598,11 +2598,57 @@ def _runtime_interface_state(active=False, configured=True, raw_state="", disabl return "unknown" +def build_camera_runtime_profile(): + devices = [] + v4l2_ctl = shutil.which("v4l2-ctl") + ffmpeg = shutil.which("ffmpeg") + for path in sorted(glob.glob("/dev/video*")): + name = os.path.basename(path) + if not re.fullmatch(r"video[0-9]+", name) or not os.access(path, os.R_OK): + continue + if not v4l2_ctl: + continue + try: + result = subprocess.run( + [v4l2_ctl, "--device", path, "--all"], + capture_output=True, + text=True, + timeout=2, + check=False, + ) + except (OSError, subprocess.SubprocessError): + continue + capabilities = f"{result.stdout}\n{result.stderr}" + if result.returncode != 0 or not re.search(r"Video Capture(?: Multiplanar)?", capabilities): + continue + device_name = name + try: + with open(f"/sys/class/video4linux/{name}/name", "r", encoding="utf-8") as name_file: + device_name = name_file.read().strip() or name + except OSError: + pass + devices.append({"id": path, "name": device_name, "facing": "external"}) + + streaming_supported = bool(ffmpeg and v4l2_ctl) + state = "available" if devices and streaming_supported else "degraded" if devices else "unavailable" + return { + "state": state, + "reason": "detected" if devices else "not_detected", + "permission": "granted" if devices else "unknown", + "count": len(devices), + "default_id": devices[0]["id"] if devices else None, + "devices": devices, + "streaming_supported": streaming_supported, + "transports": ["srt"] if streaming_supported else [], + "provider": "v4l2", + } + + def build_runtime_profile_payload(): observed_at_ms = int(time.time() * 1000) interfaces = { key: {"state": "unknown"} - for key in ("rs485", "screen", "radio", "wifi", "ethernet") + for key in ("rs485", "screen", "radio", "wifi", "ethernet", "camera") } try: @@ -2695,6 +2741,18 @@ def build_runtime_profile_payload(): except Exception as exc: interfaces["rs485"]["error"] = str(exc) + try: + interfaces["camera"] = build_camera_runtime_profile() + except Exception as exc: + interfaces["camera"] = { + "state": "unknown", + "reason": "query_failed", + "count": 0, + "devices": [], + "streaming_supported": False, + "error": str(exc), + } + return { "schema_version": 1, "observed_at_ms": observed_at_ms, diff --git a/stage_ui/console_core/backend/test_runtime_profile.py b/stage_ui/console_core/backend/test_runtime_profile.py index 3255ed0..0cc9497 100644 --- a/stage_ui/console_core/backend/test_runtime_profile.py +++ b/stage_ui/console_core/backend/test_runtime_profile.py @@ -65,7 +65,10 @@ class RuntimeProfileTest(unittest.TestCase): with mock.patch.object(app, "collect_provider_cards", return_value=(runtime_cards, ["00_core_services.py"])) as collect_cards, \ mock.patch.object(app, "get_network_summary", return_value=network) as get_network, \ - mock.patch.object(app, "modbus_list_ports_payload", return_value=modbus): + mock.patch.object(app, "modbus_list_ports_payload", return_value=modbus), \ + mock.patch.object(app, "build_camera_runtime_profile", return_value={ + "state": "unavailable", "count": 0, "devices": [], "streaming_supported": True, + }): payload = app.build_runtime_profile_payload() collect_cards.assert_called_once_with( @@ -80,8 +83,28 @@ class RuntimeProfileTest(unittest.TestCase): self.assertEqual("disabled", interfaces["wifi"]["state"]) self.assertEqual("available", interfaces["ethernet"]["state"]) self.assertEqual("available", interfaces["rs485"]["state"]) + self.assertEqual("unavailable", interfaces["camera"]["state"]) self.assertNotIn("busy_process", interfaces["rs485"]["ports"][0]) + def test_camera_profile_accepts_only_readable_v4l2_capture_nodes(self): + completed = app.subprocess.CompletedProcess( + args=[], returncode=0, stdout="Capabilities: Video Capture\n", stderr="" + ) + with mock.patch.object(app.glob, "glob", return_value=["/dev/video0", "/dev/video-enc0"]), \ + mock.patch.object(app.os, "access", return_value=True), \ + mock.patch.object(app.shutil, "which", side_effect=lambda name: f"/usr/bin/{name}"), \ + mock.patch.object(app.subprocess, "run", return_value=completed) as run, \ + mock.patch("builtins.open", mock.mock_open(read_data="USB Camera\n")): + camera = app.build_camera_runtime_profile() + + self.assertEqual("available", camera["state"]) + self.assertTrue(camera["streaming_supported"]) + self.assertEqual([{"id": "/dev/video0", "name": "USB Camera", "facing": "external"}], camera["devices"]) + run.assert_called_once_with( + ["/usr/bin/v4l2-ctl", "--device", "/dev/video0", "--all"], + capture_output=True, text=True, timeout=2, check=False, + ) + if __name__ == "__main__": unittest.main()