From 82aabe463cf2338dd5387729f26df576fad56656 Mon Sep 17 00:00:00 2001 From: Davide De Tommaso Date: Wed, 15 Dec 2021 11:43:49 +0100 Subject: [PATCH] Fixed seniority with events --- .env | 3 ++ .github/workflows/build.yml | 32 ----------------- Dockerfile | 5 +-- apps/device.yml | 23 ------------- apps/latency_test.yml | 18 ---------- docker-compose.yml | 34 +++++++------------ {apps/lsl => examples}/.env | 1 + examples/test_device.yml | 15 ++++++++ examples/test_latency.yml | 12 +++++++ hidman/__init__.py | 2 +- hidman/core.py | 15 ++++---- workdir/apps/.env | 2 ++ {apps => workdir/apps}/client_server.yml | 19 ++--------- .../apps}/client_server_threads.yml | 18 ++-------- .../apps}/client_server_timeout.yml | 18 ++-------- {apps => workdir/apps/lsl}/.env | 0 {apps => workdir/apps}/lsl/Dockerfile | 0 {apps => workdir/apps}/lsl/lsl-client.py | 0 {apps => workdir/apps}/lsl/lsl.yml | 0 .../examples}/client.py | 0 .../examples}/client_threads.py | 0 .../examples}/client_timeout.py | 0 .../examples}/device.py | 0 .../examples}/latency_test.py | 0 .../examples}/server.py | 0 25 files changed, 65 insertions(+), 152 deletions(-) create mode 100644 .env delete mode 100644 .github/workflows/build.yml delete mode 100644 apps/device.yml delete mode 100644 apps/latency_test.yml rename {apps/lsl => examples}/.env (65%) create mode 100644 examples/test_device.yml create mode 100644 examples/test_latency.yml create mode 100644 workdir/apps/.env rename {apps => workdir/apps}/client_server.yml (52%) rename {apps => workdir/apps}/client_server_threads.yml (53%) rename {apps => workdir/apps}/client_server_timeout.yml (53%) rename {apps => workdir/apps/lsl}/.env (100%) rename {apps => workdir/apps}/lsl/Dockerfile (100%) rename {apps => workdir/apps}/lsl/lsl-client.py (100%) rename {apps => workdir/apps}/lsl/lsl.yml (100%) rename {apps/python-examples => workdir/examples}/client.py (100%) rename {apps/python-examples => workdir/examples}/client_threads.py (100%) rename {apps/python-examples => workdir/examples}/client_timeout.py (100%) rename {apps/python-examples => workdir/examples}/device.py (100%) rename {apps/python-examples => workdir/examples}/latency_test.py (100%) rename {apps/python-examples => workdir/examples}/server.py (100%) diff --git a/.env b/.env new file mode 100644 index 0000000..5ada771 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +PROJECT_NAME=hidman +RELEASE_TAG=v0.9 +DOCKER_SRC=iitschri/ubuntu20.04-docker:2021.12.1 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 527aae8..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,32 +0,0 @@ -# This is a basic workflow to help you get started with Actions - -name: CI - -# Controls when the workflow will run -on: - release: - types: [published] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - build: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - - name: checkout repo - uses: actions/checkout@v2 - - - name: Log in to Docker Hub - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: init build - run: git clone https://github.com/s4hri/xpman && xpman/xpman build distro diff --git a/Dockerfile b/Dockerfile index 9cf1716..7099861 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,13 +3,14 @@ ARG DOCKER_SRC FROM ${DOCKER_SRC} LABEL maintainer="Davide De Tommaso " +USER root + RUN apt-get update RUN apt-get install -y python3-pip +USER docky RUN python3 -m pip install --upgrade pip -WORKDIR /usr/local/src/ - COPY . hidman RUN cd hidman && pip3 install . diff --git a/apps/device.yml b/apps/device.yml deleted file mode 100644 index 7b52fab..0000000 --- a/apps/device.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: '3.7' - -x-hidmanbase: &hidman-base - - volumes: - - type: bind - source: ../ - target: /usr/local/src/hidman - - image: iitschri/hidman:latest - - privileged: true - stdin_open: true - tty: true - network_mode: host - -services: - - device_test: - <<: *hidman-base - command: python3 /usr/local/src/hidman/apps/python-examples/device.py - devices: - - ${HIDMAN_DEVICE}:/dev/input/hidman0 diff --git a/apps/latency_test.yml b/apps/latency_test.yml deleted file mode 100644 index 322c6db..0000000 --- a/apps/latency_test.yml +++ /dev/null @@ -1,18 +0,0 @@ -version: '3.7' - -services: - - test-latency: - image: iitschri/hidman:latest - - command: python3 /usr/local/src/hidman/apps/python-examples/latency_test.py - - privileged: true - stdin_open: true - tty: true - network_mode: host - - volumes: - - type: bind - source: ../ - target: /usr/local/src/hidman diff --git a/docker-compose.yml b/docker-compose.yml index 9ca1e3e..e17ab08 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,27 +1,19 @@ -version: '3.7' +version: '3.8' -x-hidmanbase: &hidman-base - - build: - context: . - dockerfile: Dockerfile - args: - DOCKER_SRC: python:3.8 +services: - volumes: - - type: bind - source: ./ - target: /usr/local/src/hidman + main: + image: ${LOCAL_DOCKER_IMAGE} - image: iitschri/hidman:latest + environment: + - PROJECT_NAME=${PROJECT_NAME} - privileged: true - stdin_open: true - tty: true - network_mode: host + volumes: + - type: bind + source: ./ + target: /home/docky/hidman -services: + devices: + - "/dev/input/event18:/dev/input/hidman0" - hidman-dev: - <<: *hidman-base - command: bash + command: terminator diff --git a/apps/lsl/.env b/examples/.env similarity index 65% rename from apps/lsl/.env rename to examples/.env index e7e75bf..7365194 100644 --- a/apps/lsl/.env +++ b/examples/.env @@ -1 +1,2 @@ +RELEASE_TAG=v0.8 HIDMAN_DEVICE=/dev/input/event7 diff --git a/examples/test_device.yml b/examples/test_device.yml new file mode 100644 index 0000000..ae6b612 --- /dev/null +++ b/examples/test_device.yml @@ -0,0 +1,15 @@ +version: '3.7' + +services: + + test-device: + image: iitschri/hidman-docker:${RELEASE_TAG} + command: python3 /home/docky/workdir/examples/device.py + + devices: + - ${HIDMAN_DEVICE}:/dev/input/hidman0 + + volumes: + - type: bind + source: ../workdir + target: /home/docky/workdir diff --git a/examples/test_latency.yml b/examples/test_latency.yml new file mode 100644 index 0000000..5942637 --- /dev/null +++ b/examples/test_latency.yml @@ -0,0 +1,12 @@ +version: '3.7' + +services: + + test-latency: + image: iitschri/hidman-docker:${RELEASE_TAG} + command: python3 /home/docky/workdir/examples/latency_test.py + + volumes: + - type: bind + source: ../workdir + target: /home/docky/workdir diff --git a/hidman/__init__.py b/hidman/__init__.py index 3a4be2c..3deb9c7 100644 --- a/hidman/__init__.py +++ b/hidman/__init__.py @@ -1,6 +1,6 @@ __authors__ = 'Davide De Tommaso' __emails__ = 'davide.detommaso@iit.it' __license__ = 'MIT' -__version__ = '0.8' +__version__ = '0.9' __description__ = 'A Python based HID (Human Interface Device) events manager' __requirements__ = ['pytest>=6.2.4', 'evdev>=1.4.0', 'pyzmq>=22.1.0'] diff --git a/hidman/core.py b/hidman/core.py index 0c0101d..6456e6e 100644 --- a/hidman/core.py +++ b/hidman/core.py @@ -61,14 +61,17 @@ def readOne(self): return 0 #virtual event for testing purposes def waitEvent(self, event_type=None, event_code=None, event_status=None, timeout_ms=None): - t0 = time.perf_counter() + t0 = time.time() + lastReadSeniority = -1 res = None - while res is None: - elapsed_time = (time.perf_counter() - t0)*1000.0 + while res is None or lastReadSeniority < 0: + elapsed_time = (time.time() - t0)*1000.0 if not timeout_ms is None: if timeout_ms <= elapsed_time: break event = self.readOne() + if not event is None: + lastReadSeniority = event.timestamp() - t0 res = HIDEvent.parse(event, event_type, event_code, event_status) time.sleep(HIDDevice.POLLING_TIME_MS/1000.0) return (res, elapsed_time) @@ -116,14 +119,14 @@ def __init__(self, address="ipc://pyboard"): self._socket.setsockopt(zmq.SUBSCRIBE, b'') def waitEvent(self, event_type=None, event_code=None, event_status=None, timeout_ms=None): - t0 = time.perf_counter() + t0 = time.time() if not timeout_ms is None: poller = zmq.Poller() poller.register(self._socket, zmq.POLLIN) res = None while res is None: if not timeout_ms is None: - elapsed_time = (time.perf_counter() - t0)*1000.0 + elapsed_time = (time.time() - t0)*1000.0 if timeout_ms <= elapsed_time: break elif poller.poll(timeout_ms-elapsed_time): @@ -135,7 +138,7 @@ def waitEvent(self, event_type=None, event_code=None, event_status=None, timeout event = self._socket.recv_pyobj() res = HIDEvent.parse(event, event_type, event_code, event_status) - return (res, time.perf_counter()-t0) + return (res, time.time()-t0) def waitKey(self, keyList=None, evstate=None, timeout_ms=None): return self.waitEvent(event_type=ecodes.EV_KEY, event_code=keyList, event_status=evstate, timeout_ms=timeout_ms) diff --git a/workdir/apps/.env b/workdir/apps/.env new file mode 100644 index 0000000..7365194 --- /dev/null +++ b/workdir/apps/.env @@ -0,0 +1,2 @@ +RELEASE_TAG=v0.8 +HIDMAN_DEVICE=/dev/input/event7 diff --git a/apps/client_server.yml b/workdir/apps/client_server.yml similarity index 52% rename from apps/client_server.yml rename to workdir/apps/client_server.yml index be11dc5..b202b32 100644 --- a/apps/client_server.yml +++ b/workdir/apps/client_server.yml @@ -1,29 +1,14 @@ version: '3.7' -x-hidmanbase: &hidman-base - - volumes: - - type: bind - source: ../ - target: /usr/local/src/hidman - - image: iitschri/hidman:latest - - privileged: true - stdin_open: true - tty: true - network_mode: host - services: - hidman-server: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/server.py devices: - ${HIDMAN_DEVICE}:/dev/input/hidman0 hidman-client: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/client.py depends_on: - hidman-server diff --git a/apps/client_server_threads.yml b/workdir/apps/client_server_threads.yml similarity index 53% rename from apps/client_server_threads.yml rename to workdir/apps/client_server_threads.yml index 60952a3..57261ca 100644 --- a/apps/client_server_threads.yml +++ b/workdir/apps/client_server_threads.yml @@ -1,29 +1,15 @@ version: '3.7' -x-hidmanbase: &hidman-base - - volumes: - - type: bind - source: ../ - target: /usr/local/src/hidman - - image: iitschri/hidman:latest - - privileged: true - stdin_open: true - tty: true - network_mode: host - services: hidman-server: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/server.py devices: - ${HIDMAN_DEVICE}:/dev/input/hidman0 hidman-client: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/client_threads.py depends_on: - hidman-server diff --git a/apps/client_server_timeout.yml b/workdir/apps/client_server_timeout.yml similarity index 53% rename from apps/client_server_timeout.yml rename to workdir/apps/client_server_timeout.yml index bce82dd..8250eca 100644 --- a/apps/client_server_timeout.yml +++ b/workdir/apps/client_server_timeout.yml @@ -1,29 +1,15 @@ version: '3.7' -x-hidmanbase: &hidman-base - - volumes: - - type: bind - source: ../ - target: /usr/local/src/hidman - - image: iitschri/hidman:latest - - privileged: true - stdin_open: true - tty: true - network_mode: host - services: hidman-server: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/server.py devices: - ${HIDMAN_DEVICE}:/dev/input/hidman0 hidman-client: - <<: *hidman-base + image: iitschri/hidman:${RELEASE_TAG} command: python3 /usr/local/src/hidman/apps/python-examples/client_timeout.py depends_on: - hidman-server diff --git a/apps/.env b/workdir/apps/lsl/.env similarity index 100% rename from apps/.env rename to workdir/apps/lsl/.env diff --git a/apps/lsl/Dockerfile b/workdir/apps/lsl/Dockerfile similarity index 100% rename from apps/lsl/Dockerfile rename to workdir/apps/lsl/Dockerfile diff --git a/apps/lsl/lsl-client.py b/workdir/apps/lsl/lsl-client.py similarity index 100% rename from apps/lsl/lsl-client.py rename to workdir/apps/lsl/lsl-client.py diff --git a/apps/lsl/lsl.yml b/workdir/apps/lsl/lsl.yml similarity index 100% rename from apps/lsl/lsl.yml rename to workdir/apps/lsl/lsl.yml diff --git a/apps/python-examples/client.py b/workdir/examples/client.py similarity index 100% rename from apps/python-examples/client.py rename to workdir/examples/client.py diff --git a/apps/python-examples/client_threads.py b/workdir/examples/client_threads.py similarity index 100% rename from apps/python-examples/client_threads.py rename to workdir/examples/client_threads.py diff --git a/apps/python-examples/client_timeout.py b/workdir/examples/client_timeout.py similarity index 100% rename from apps/python-examples/client_timeout.py rename to workdir/examples/client_timeout.py diff --git a/apps/python-examples/device.py b/workdir/examples/device.py similarity index 100% rename from apps/python-examples/device.py rename to workdir/examples/device.py diff --git a/apps/python-examples/latency_test.py b/workdir/examples/latency_test.py similarity index 100% rename from apps/python-examples/latency_test.py rename to workdir/examples/latency_test.py diff --git a/apps/python-examples/server.py b/workdir/examples/server.py similarity index 100% rename from apps/python-examples/server.py rename to workdir/examples/server.py