Skip to content

Commit cc7b4a7

Browse files
committed
refactor: remove host from WebSocketCamera
1 parent 6e78491 commit cc7b4a7

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

src/arduino/app_peripherals/camera/camera.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ def __new__(
108108
# WebSocket Camera - extract host and port from URL
109109
from .websocket_camera import WebSocketCamera
110110

111-
host = parsed.hostname or "localhost"
112111
port = parsed.port or 8080
113-
return WebSocketCamera(host=host, port=port, resolution=resolution, fps=fps, adjustments=adjustments, **kwargs)
112+
return WebSocketCamera(port=port, resolution=resolution, fps=fps, adjustments=adjustments, **kwargs)
114113
elif source.startswith("/dev/video") or source.startswith("/dev/v4l/by-id/") or source.startswith("/dev/v4l/by-path/"):
115114
# V4L device path, by-id, or by-path
116115
from .v4l_camera import V4LCamera

src/arduino/app_peripherals/camera/websocket_camera.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class WebSocketCamera(BaseCamera):
4646

4747
def __init__(
4848
self,
49-
host: str = "0.0.0.0",
5049
port: int = 8080,
5150
timeout: int = 3,
5251
frame_format: Literal["binary", "base64", "json"] = "binary",
@@ -70,14 +69,15 @@ def __init__(
7069
super().__init__(resolution, fps, adjustments)
7170

7271
self.protocol = "ws"
73-
host_ip = os.getenv("HOST_IP")
74-
self.host_ip = host_ip if host_ip is not None else host
75-
self.host = host
7672
self.port = port
7773
self.timeout = timeout
7874
self.frame_format = frame_format
7975
self.logger = logger
8076

77+
self._host = "0.0.0.0"
78+
host_ip = os.getenv("HOST_IP")
79+
self._host_ip = host_ip if host_ip is not None else self._host
80+
8181
self._frame_queue = queue.Queue(1)
8282
self._server = None
8383
self._loop = None
@@ -89,7 +89,7 @@ def __init__(
8989
@property
9090
def address(self) -> str:
9191
"""Return the WebSocket server address."""
92-
return f"{self.protocol}://{self.host_ip}:{self.port}"
92+
return f"{self.protocol}://{self._host_ip}:{self.port}"
9393

9494
def _open_camera(self) -> None:
9595
"""Start the WebSocket server."""
@@ -131,7 +131,7 @@ async def _start_server(self) -> None:
131131
self._server = await asyncio.wait_for(
132132
websockets.serve(
133133
self._ws_handler,
134-
self.host,
134+
self._host,
135135
self.port,
136136
open_timeout=self.timeout,
137137
ping_timeout=self.timeout,

tests/arduino/app_peripherals/camera/test_camera.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,30 @@ def test_camera_factory_with_https_url():
3838
assert camera.url == "https://192.168.1.100:8080/video"
3939

4040

41+
def test_camera_factory_with_ws_url_default_port():
42+
"""Test Camera factory with WebSocket URL without port."""
43+
camera = Camera("ws://localhost")
44+
assert isinstance(camera, WebSocketCamera)
45+
assert camera.address == "ws://0.0.0.0:8080"
46+
assert camera.port == 8080 # Default port
47+
48+
4149
def test_camera_factory_with_ws_url():
4250
"""Test Camera factory with WebSocket URL."""
4351
camera = Camera("ws://0.0.0.0:8080")
4452
assert isinstance(camera, WebSocketCamera)
45-
assert camera.host == "0.0.0.0"
53+
assert camera.address == "ws://0.0.0.0:8080"
4654
assert camera.port == 8080
4755

4856

4957
def test_camera_factory_with_wss_url():
5058
"""Test Camera factory with secure WebSocket URL."""
5159
camera = Camera("wss://192.168.1.100:9090")
5260
assert isinstance(camera, WebSocketCamera)
53-
assert camera.host == "192.168.1.100"
61+
assert camera.address == "ws://0.0.0.0:9090" # Host is always ignored
5462
assert camera.port == 9090
5563

5664

57-
def test_camera_factory_with_ws_url_default_port():
58-
"""Test Camera factory with WebSocket URL without port."""
59-
camera = Camera("ws://localhost")
60-
assert isinstance(camera, WebSocketCamera)
61-
assert camera.host == "localhost"
62-
assert camera.port == 8080 # Default port
63-
64-
6565
def test_camera_factory_with_ip_camera_kwargs():
6666
"""Test Camera factory with IP camera specific kwargs."""
6767
camera = Camera("rtsp://192.168.1.100/stream", username="admin", password="secret", timeout=30)

tests/arduino/app_peripherals/camera/test_websocket_camera.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def test_websocket_camera_init_default():
5454

5555
def test_websocket_camera_init_custom():
5656
"""Test WebSocketCamera initialization with custom parameters."""
57-
camera = WebSocketCamera(host="127.0.0.1", port=9090, timeout=30, frame_format="json", resolution=(1920, 1080), fps=30)
58-
assert camera.address == "ws://127.0.0.1:9090"
57+
camera = WebSocketCamera(port=9090, timeout=30, frame_format="json", resolution=(1920, 1080), fps=30)
58+
assert camera.address == "ws://0.0.0.0:9090" # No env var is set, so uses default host
5959
assert camera.port == 9090
6060
assert camera.timeout == 30
6161
assert camera.frame_format == "json"

0 commit comments

Comments
 (0)