Skip to content

Commit

Permalink
fix - Activate API
Browse files Browse the repository at this point in the history
- Pass the mode changed to 0.2 -> 0.5
- Remove all input_output
- PUT API in the last (Object Collector take long time to initialize)
  • Loading branch information
EtienneSchmitz committed Sep 4, 2023
1 parent 004429d commit 358a2da
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 68 deletions.
4 changes: 2 additions & 2 deletions rpi/manager/thymio/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 56 additions & 2 deletions rpi/sockets/camera.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
class CameraServer():
pass
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())
72 changes: 8 additions & 64 deletions rpi/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
# self.input_output.close()
pass

0 comments on commit 358a2da

Please sign in to comment.