diff --git a/rpi/manager/thymio/manager.py b/rpi/manager/thymio/manager.py index cfba561..355267b 100644 --- a/rpi/manager/thymio/manager.py +++ b/rpi/manager/thymio/manager.py @@ -11,16 +11,16 @@ class ThymioManager(BaseManager): current_mode = 0 last_mode_change_time = 0 - mode_change_delay = 0.2 + mode_change_delay = 0.5 def __init__(self): self.controller = ThymioController() self.logger = logging.getLogger(__name__) self.tasks = [ - # API(self.controller), ObjectCollector(self.controller), FollowLine(self.controller), + API(self.controller), ] self.num_modes = len(self.tasks) diff --git a/rpi/sockets/camera.py b/rpi/sockets/camera.py index ad5d173..9b690bb 100644 --- a/rpi/sockets/camera.py +++ b/rpi/sockets/camera.py @@ -1,2 +1,56 @@ -class CameraServer(): - pass \ No newline at end of file +import asyncio +import websockets +import logging + +from PIL import Image +from io import BytesIO +from vision.camera import Camera + +class CameraServer: + def __init__(self): + self.connected = set() + self.logger = logging.getLogger(__name__) + self.camera = Camera() + + async def send_to_all_clients(self): + while True: + await asyncio.sleep(1/20.0) + + if len(self.connected) == 0: + continue + + img = self.camera.grab_frame_loop() + if img is None: + continue + + with BytesIO() as bytes: + pil_img = Image.fromarray(img) + pil_img.save(bytes, 'jpeg') + message = bytes.getvalue() + + for websocket in list(self.connected): + try: + await websocket.send(message) + except websockets.ConnectionClosed: + self.logger.warning("Client disconnected. Removing from list.") + self.connected.remove(websocket) + + async def handler(self, websocket, path): + self.logger.debug("New connection added.") + self.connected.add(websocket) + + try: + async for message in websocket: + self.logger.debug(f"Receive commands :", message) + except (websockets.ConnectionClosed, Exception) as e: + self.logger.warning(f"Connection closed due to error: {e}") + finally: + self.connected.remove(websocket) + + def run(self): + server_coro = websockets.serve(self.handler, '0.0.0.0', 5678) + + self.loop = asyncio.get_event_loop() + + self.loop.run_until_complete(server_coro) + self.loop.create_task(self.send_to_all_clients()) \ No newline at end of file diff --git a/rpi/tasks/api.py b/rpi/tasks/api.py index 9171a50..5ef748b 100644 --- a/rpi/tasks/api.py +++ b/rpi/tasks/api.py @@ -2,80 +2,24 @@ from controller.base import BaseController from tasks.base import Task -from sockets.input_output import InputOuputServer - -import asyncio -import websockets - -from PIL import Image -from io import BytesIO -from vision.camera import Camera - -class CameraServer: - def __init__(self): - self.connected = set() - self.logger = logging.getLogger(__name__) - self.camera = Camera() - - async def send_to_all_clients(self): - while True: - await asyncio.sleep(1/20.0) - - if len(self.connected) == 0: - continue - - img = self.camera.grab_frame_loop() - if img is None: - continue - - with BytesIO() as bytes: - pil_img = Image.fromarray(img) - pil_img.save(bytes, 'jpeg') - message = bytes.getvalue() - - for websocket in list(self.connected): - try: - await websocket.send(message) - except websockets.ConnectionClosed: - self.logger.warning("Client disconnected. Removing from list.") - self.connected.remove(websocket) - - async def handler(self, websocket, path): - self.logger.debug("New connection added.") - self.connected.add(websocket) - - try: - async for message in websocket: - self.logger.debug(f"Receive commands :", message) - except (websockets.ConnectionClosed, Exception) as e: - self.logger.warning(f"Connection closed due to error: {e}") - finally: - self.connected.remove(websocket) - - def run(self): - server_coro = websockets.serve(self.handler, '0.0.0.0', 5678) - - self.loop = asyncio.get_event_loop() - - self.loop.run_until_complete(server_coro) - self.loop.create_task(self.send_to_all_clients()) - +# from sockets.input_output import InputOuputServer class API(Task): def __init__(self, controller: BaseController): self.controller = controller self.logger = logging.getLogger(__name__) - self.input_output = InputOuputServer(self.controller) - self.input_output.run() + # self.input_output = InputOuputServer(self.controller) + # self.input_output.run() - self.camera_server = CameraServer() - self.camera_server.run() + # self.camera_server = CameraServer() + # self.camera_server.run() - asyncio.get_event_loop().run_forever() + # asyncio.get_event_loop().run_forever() def run(self): self.logger.info("API") def close(self): - self.input_output.close() \ No newline at end of file + # self.input_output.close() + pass \ No newline at end of file