Skip to content

Commit aa81138

Browse files
committed
refactor: rename address -> url
1 parent cc7b4a7 commit aa81138

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

src/arduino/app_peripherals/camera/websocket_camera.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def __init__(
7474
self.frame_format = frame_format
7575
self.logger = logger
7676

77-
self._host = "0.0.0.0"
7877
host_ip = os.getenv("HOST_IP")
79-
self._host_ip = host_ip if host_ip is not None else self._host
78+
self._bind_ip = "0.0.0.0"
79+
self._external_ip = host_ip if host_ip is not None else self._bind_ip
8080

8181
self._frame_queue = queue.Queue(1)
8282
self._server = None
@@ -87,9 +87,9 @@ def __init__(
8787
self._client_lock = asyncio.Lock()
8888

8989
@property
90-
def address(self) -> str:
90+
def url(self) -> str:
9191
"""Return the WebSocket server address."""
92-
return f"{self.protocol}://{self._host_ip}:{self.port}"
92+
return f"{self.protocol}://{self._external_ip}:{self.port}"
9393

9494
def _open_camera(self) -> None:
9595
"""Start the WebSocket server."""
@@ -101,15 +101,15 @@ def _open_camera(self) -> None:
101101
start_timeout = self.timeout
102102
while time.time() - start_time < start_timeout:
103103
if self._server is not None:
104-
logger.info(f"WebSocket camera server started on {self.address}")
104+
logger.info(f"WebSocket camera server started on {self.url}")
105105
return
106106
time.sleep(0.1)
107107

108108
# Cleanup server thread if it failed to start in time
109109
if self._server_thread.is_alive():
110110
self._server_thread.join(timeout=1.0)
111111

112-
raise CameraOpenError(f"Failed to start WebSocket server on {self.address}")
112+
raise CameraOpenError(f"Failed to start WebSocket server on {self.url}")
113113

114114
def _start_server_thread(self) -> None:
115115
"""Run WebSocket server in its own thread with event loop."""
@@ -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._bind_ip,
135135
self.port,
136136
open_timeout=self.timeout,
137137
ping_timeout=self.timeout,

tests/arduino/app_peripherals/camera/test_camera.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ def test_camera_factory_with_ws_url_default_port():
4242
"""Test Camera factory with WebSocket URL without port."""
4343
camera = Camera("ws://localhost")
4444
assert isinstance(camera, WebSocketCamera)
45-
assert camera.address == "ws://0.0.0.0:8080"
45+
assert camera.url == "ws://0.0.0.0:8080"
4646
assert camera.port == 8080 # Default port
4747

4848

4949
def test_camera_factory_with_ws_url():
5050
"""Test Camera factory with WebSocket URL."""
5151
camera = Camera("ws://0.0.0.0:8080")
5252
assert isinstance(camera, WebSocketCamera)
53-
assert camera.address == "ws://0.0.0.0:8080"
53+
assert camera.url == "ws://0.0.0.0:8080"
5454
assert camera.port == 8080
5555

5656

5757
def test_camera_factory_with_wss_url():
5858
"""Test Camera factory with secure WebSocket URL."""
5959
camera = Camera("wss://192.168.1.100:9090")
6060
assert isinstance(camera, WebSocketCamera)
61-
assert camera.address == "ws://0.0.0.0:9090" # Host is always ignored
61+
assert camera.url == "ws://0.0.0.0:9090" # IP is always ignored
6262
assert camera.port == 9090
6363

6464

tests/arduino/app_peripherals/camera/test_websocket_camera.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def encoded_frame_json(encoded_frame_binary):
4343
def test_websocket_camera_init_default():
4444
"""Test WebSocketCamera initialization with default parameters."""
4545
camera = WebSocketCamera()
46-
assert camera.address == "ws://0.0.0.0:8080"
46+
assert camera.url == "ws://0.0.0.0:8080"
4747
assert camera.port == 8080
4848
assert camera.timeout == 3
4949
assert camera.frame_format == "binary"
@@ -55,7 +55,7 @@ def test_websocket_camera_init_default():
5555
def test_websocket_camera_init_custom():
5656
"""Test WebSocketCamera initialization with custom parameters."""
5757
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
58+
assert camera.url == "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"
@@ -167,7 +167,7 @@ def test_websocket_camera_read_frame_empty_queue():
167167
async def test_websocket_camera_capture_frame(encoded_frame_binary):
168168
"""Test capturing frame from WebSocket camera."""
169169
with WebSocketCamera(port=0, frame_format="binary") as camera:
170-
async with websockets.connect(camera.address) as ws:
170+
async with websockets.connect(camera.url) as ws:
171171
# Skip welcome message
172172
await ws.recv()
173173

@@ -189,15 +189,15 @@ async def test_websocket_camera_single_client():
189189

190190
try:
191191
# Connect first client
192-
async with websockets.connect(camera.address) as ws1:
192+
async with websockets.connect(camera.url) as ws1:
193193
# First client should receive welcome message
194194
welcome = await ws1.recv()
195195
message = json.loads(welcome)
196196
assert message["status"] == "connected"
197197

198198
# Try to connect second client while first is connected
199199
try:
200-
async with websockets.connect(camera.address) as ws2:
200+
async with websockets.connect(camera.url) as ws2:
201201
# Second client should receive rejection message
202202
rejection = await asyncio.wait_for(ws2.recv(), timeout=1.0)
203203
message = json.loads(rejection)
@@ -213,7 +213,7 @@ async def test_websocket_camera_single_client():
213213
async def test_websocket_camera_welcome_message():
214214
"""Test that welcome message is sent to connected client."""
215215
with WebSocketCamera(port=0) as camera:
216-
async with websockets.connect(camera.address) as ws:
216+
async with websockets.connect(camera.url) as ws:
217217
# Should receive welcome message
218218
welcome = await asyncio.wait_for(ws.recv(), timeout=1.0)
219219
message = json.loads(welcome)
@@ -226,7 +226,7 @@ async def test_websocket_camera_welcome_message():
226226
async def test_websocket_camera_receives_frames(encoded_frame_binary):
227227
"""Test that server receives and queues frames from client."""
228228
with WebSocketCamera(port=0, frame_format="binary") as camera:
229-
async with websockets.connect(camera.address) as ws:
229+
async with websockets.connect(camera.url) as ws:
230230
# Skip welcome message
231231
await ws.recv()
232232

@@ -247,7 +247,7 @@ async def test_websocket_camera_disconnects_client_on_stop():
247247
camera.start()
248248

249249
try:
250-
async with websockets.connect(camera.address) as ws:
250+
async with websockets.connect(camera.url) as ws:
251251
# Client connected, receive welcome message
252252
welcome = await ws.recv()
253253
message = json.loads(welcome)
@@ -286,7 +286,7 @@ def test_websocket_camera_stop_without_client():
286286
async def test_websocket_camera_backpressure(sample_frame):
287287
"""Test that old frames are dropped when new frames arrive faster than they're consumed."""
288288
with WebSocketCamera(port=0, frame_format="binary") as camera:
289-
async with websockets.connect(camera.address) as ws:
289+
async with websockets.connect(camera.url) as ws:
290290
await ws.recv() # Skip welcome message
291291

292292
_, buffer1 = cv2.imencode(".jpg", np.ones((480, 640, 3), dtype=np.uint8) * 1)
@@ -318,6 +318,7 @@ def adjustment(frame):
318318

319319
# Capture uses adjustments
320320
frame = camera.capture()
321+
assert frame is not None
321322

322323
# The adjustment is applied in capture()
323324
expected = sample_frame + 50
@@ -355,7 +356,7 @@ def event_listener(event_type, data):
355356

356357
# This should emit connection and disconnection events
357358
async def client_task():
358-
async with websockets.connect(camera.address):
359+
async with websockets.connect(camera.url):
359360
pass
360361

361362
# Run client concurrently to properly test event handling
@@ -438,7 +439,7 @@ def event_listener(event_type, data):
438439

439440
# This should emit a connection event but no disconnection event
440441
async def client_task():
441-
async with websockets.connect(camera.address):
442+
async with websockets.connect(camera.url):
442443
pass
443444
await can_close.wait()
444445

0 commit comments

Comments
 (0)