From ec0e8e350423572ddbf4d812c2940ddfc5b9a4c7 Mon Sep 17 00:00:00 2001 From: Kasper Date: Fri, 14 Nov 2025 14:15:00 +0100 Subject: [PATCH] build: add Docker container Some changes (like `asound.conf`) are not portable and only work on my machine. This will be fixed in the future. ref: N25B-284 --- .dockerignore | 9 ++++++++ Dockerfile | 12 +++++++++++ asound.conf | 9 ++++++++ install_deps.sh | 4 ++++ .../endpoints/main_receiver.py | 5 ++++- src/robot_interface/endpoints/socket_base.py | 21 ++++++++----------- 6 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 asound.conf create mode 100755 install_deps.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..03d8492 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.git +.githooks/ +.idea/ +.venv/ +test/ +typings/ +.dockerignore +.gitignore +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f612f12 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:2.7.18-alpine + +WORKDIR /app + +COPY install_deps.sh requirements.txt ./ +RUN ./install_deps.sh + +COPY . . + +ENV PYTHONPATH=src + +CMD [ "python2", "-m", "robot_interface.main" ] diff --git a/asound.conf b/asound.conf new file mode 100644 index 0000000..aa740f7 --- /dev/null +++ b/asound.conf @@ -0,0 +1,9 @@ +pcm.!default { + type hw + card 2 +} + +ctl.!default { + type hw + card 2 +} diff --git a/install_deps.sh b/install_deps.sh new file mode 100755 index 0000000..d337d35 --- /dev/null +++ b/install_deps.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +apk add portaudio-dev libzmq gcc musl-dev g++ alsa-utils + +pip install -r requirements.txt diff --git a/src/robot_interface/endpoints/main_receiver.py b/src/robot_interface/endpoints/main_receiver.py index 0ce9711..48458dd 100644 --- a/src/robot_interface/endpoints/main_receiver.py +++ b/src/robot_interface/endpoints/main_receiver.py @@ -45,7 +45,10 @@ class MainReceiver(ReceiverBase): if message["endpoint"] == "negotiate/ports": return MainReceiver._handle_port_negotiation(message) - return {"endpoint": "negotiate/error", "data": "The requested endpoint is not implemented."} + return { + "endpoint": "negotiate/error", + "data": "The requested endpoint is not implemented.", + } def handle_message(self, message): if message["endpoint"] == "ping": diff --git a/src/robot_interface/endpoints/socket_base.py b/src/robot_interface/endpoints/socket_base.py index d08c360..235296a 100644 --- a/src/robot_interface/endpoints/socket_base.py +++ b/src/robot_interface/endpoints/socket_base.py @@ -1,6 +1,5 @@ from abc import ABCMeta - -import zmq +import os class SocketBase(object): @@ -19,7 +18,7 @@ class SocketBase(object): self.socket = None # Set later by `create_socket` self.bound = None # Set later by `create_socket` - def create_socket(self, zmq_context, socket_type, port, options=[], bind=True): + def create_socket(self, zmq_context, socket_type, port, options=[], bind=False): """ Create a ZeroMQ socket. @@ -43,17 +42,19 @@ class SocketBase(object): self.socket = zmq_context.socket(socket_type) for option, arg in options: - self.socket.setsockopt(option,arg) + self.socket.setsockopt(option, arg) self.bound = bind + host = os.environ.get("CB_HOST", "localhost") if bind: - self.socket.bind("tcp://*:{}".format(port)) + self.socket.bind("tcp://{}:{}".format(host, port)) else: - self.socket.connect("tcp://localhost:{}".format(port)) + self.socket.connect("tcp://{}:{}".format(host, port)) def close(self): """Close the ZeroMQ socket.""" - if not self.socket: return + if not self.socket: + return self.socket.close() self.socket = None @@ -65,8 +66,4 @@ class SocketBase(object): https://utrechtuniversity.youtrack.cloud/articles/N25B-A-14/RI-CB-Communication#negotiation :rtype: dict """ - return { - "id": self.identifier, - "port": self.port, - "bind": not self.bound - } + return {"id": self.identifier, "port": self.port, "bind": not self.bound}