From 948aa783241f93bc2350bae5d4011f486d969e80 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Tue, 31 Oct 2023 14:02:46 +0100 Subject: [PATCH] Add testasset --- testasset/Dockerfile | 9 +++++++++ testasset/docker-compose.yml | 16 ++++++++++++++++ testasset/mosquitto.conf | 2 ++ testasset/testasset.py | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 testasset/Dockerfile create mode 100644 testasset/docker-compose.yml create mode 100644 testasset/mosquitto.conf create mode 100644 testasset/testasset.py diff --git a/testasset/Dockerfile b/testasset/Dockerfile new file mode 100644 index 0000000..8da63a8 --- /dev/null +++ b/testasset/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3-alpine +WORKDIR /usr/src/app +ENV AM_I_IN_A_DOCKER_CONTAINER Yes + +RUN pip install --no-cache-dir paho-mqtt + +COPY testasset.py . + +CMD [ "python", "./testasset.py" ] \ No newline at end of file diff --git a/testasset/docker-compose.yml b/testasset/docker-compose.yml new file mode 100644 index 0000000..e492e12 --- /dev/null +++ b/testasset/docker-compose.yml @@ -0,0 +1,16 @@ +version: "3.7" +services: + mqttbroker: + image: eclipse-mosquitto + container_name: mosquitto + ports: + - "1883:1883" + volumes: + - ./mosquitto.conf:/mosquitto/config/mosquitto.conf + + testasset: + build: + context: . + container_name: testasset + depends_on: + - mqttbroker \ No newline at end of file diff --git a/testasset/mosquitto.conf b/testasset/mosquitto.conf new file mode 100644 index 0000000..c7b1710 --- /dev/null +++ b/testasset/mosquitto.conf @@ -0,0 +1,2 @@ +allow_anonymous true +listener 1883 \ No newline at end of file diff --git a/testasset/testasset.py b/testasset/testasset.py new file mode 100644 index 0000000..8dcccc4 --- /dev/null +++ b/testasset/testasset.py @@ -0,0 +1,21 @@ +from paho.mqtt import client as mqtt_client +import time +from datetime import datetime +from os import environ + +is_docker = environ.get('AM_I_IN_A_DOCKER_CONTAINER', False) +broker = "mqttbroker" if is_docker else "localhost" + +client = mqtt_client.Client("testasset") + +# Wait for broker +if is_docker: time.sleep(3) + +client.connect(broker, 1883, 60) + +# Send the current datetime stamp to the topic "testopic/testparam1" and "testopic/testparam2" every 1 second +while True: + now = datetime.now() + client.publish("testopic/testparam1", str(now)) + client.publish("testopic/testparam2", str(now)) + time.sleep(1) \ No newline at end of file