Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for active connections #147

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lane_assist/lane_assist.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def lane_assist_loop(self, image: np.ndarray) -> None:
self.__stop_line_assist.detect_and_handle(image, filtered_lines)

# If telemetry is enabled, send the image to the telemetry server.
if config["telemetry"]["enabled"]:
if config["telemetry"]["enabled"] and self.telemetry.any_listening():
rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)

# Draw the path on the image.
Expand Down
2 changes: 1 addition & 1 deletion src/lane_assist/preprocessing/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __generator() -> Generator[np.ndarray, None, None]:
topdown = calibration.transform([left_image, center_image, right_image])
thresholded = cv2.threshold(topdown, config["preprocessing"]["white_threshold"], 255, cv2.THRESH_BINARY)[1]

if config["telemetry"]["enabled"]:
if config["telemetry"]["enabled"] and telemetry.any_listening():
telemetry.websocket_handler.send_image("left", left_image)
telemetry.websocket_handler.send_image("center", center_image)
telemetry.websocket_handler.send_image("right", right_image)
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_sim_image_generator() -> Generator[np.ndarray, None, None]:
front_view, calibration.topdown_matrix, calibration.output_shape, flags=cv2.INTER_NEAREST
)

if config["telemetry"]["enabled"]:
if config["telemetry"]["enabled"] and telemetry.any_listening():
telemetry.websocket_handler.send_image("topdown", top_view)

yield top_view
Expand Down
23 changes: 15 additions & 8 deletions src/telemetry/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ def __init__(self) -> None:
self.__app.mount("/js", StaticFiles(directory=get_path("static/js")), name="static")
self.__app.mount("/css", StaticFiles(directory=get_path("static/css")), name="static")

def add_callback_function(self, name: str, func: callable) -> None:
"""Add a callback function.

:param name: The name of the callback function.
:param func: The callback function to add.
"""
self.available_functions[name] = func

def any_listening(self) -> bool:
"""Check if there are any active connections.

:return: Whether there are any active connections.
"""
return self.websocket_handler.any_active()

def start(self) -> None:
"""Start the telemetry server."""
if config["telemetry"]["enabled"]:
Expand All @@ -68,14 +83,6 @@ def __index_route(self) -> HTMLResponse:
html = f.read().replace("$root-url", f"{get_ip()}:{self.__port}")
return HTMLResponse(content=html)

def add_callback_function(self, name: str, func: callable) -> None:
"""Add a callback function.

:param name: The name of the callback function.
:param func: The callback function to add.
"""
self.available_functions[name] = func

def __execute_function(self, name: str) -> Any:
"""Execute a function based on the provided name.

Expand Down
8 changes: 7 additions & 1 deletion src/telemetry/data_stream/websocket_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class WebsocketHandler:
def __init__(self) -> None:
"""Initialize the websocket handler."""
self.websocket_clients = {}
self.websockets_active = {}

def add_socket(self, name: str, websocket: WebSocket, loop: AbstractEventLoop) -> WebsocketDataStream:
"""Add a websocket client to the list of clients.
Expand All @@ -77,6 +76,13 @@ def add_socket(self, name: str, websocket: WebSocket, loop: AbstractEventLoop) -
self.websocket_clients[name].append(WebsocketDataStream(websocket, loop))
return self.websocket_clients[name][-1]

def any_active(self) -> bool:
"""Check if there are any active websockets.

:return: Whether there are any active websockets.
"""
return any(len(clients) > 0 for clients in self.websocket_clients.values())

def send_image(self, name: str, image: np.ndarray) -> None:
"""Send image on channel with the given name.

Expand Down