Add testasset

This commit is contained in:
Daniel Kluge
2023-10-31 14:02:46 +01:00
parent 5e58f84b38
commit 948aa78324
4 changed files with 48 additions and 0 deletions
+9
View File
@@ -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" ]
+16
View File
@@ -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
+2
View File
@@ -0,0 +1,2 @@
allow_anonymous true
listener 1883
+21
View File
@@ -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)