diff options
Diffstat (limited to 'mqtt_client')
| -rw-r--r-- | mqtt_client/.dockerignore | 1 | ||||
| -rw-r--r-- | mqtt_client/Dockerfile | 20 | ||||
| -rw-r--r-- | mqtt_client/main.py | 15 |
3 files changed, 34 insertions, 2 deletions
diff --git a/mqtt_client/.dockerignore b/mqtt_client/.dockerignore new file mode 100644 index 0000000..5ceb386 --- /dev/null +++ b/mqtt_client/.dockerignore @@ -0,0 +1 @@ +venv diff --git a/mqtt_client/Dockerfile b/mqtt_client/Dockerfile new file mode 100644 index 0000000..7733d2b --- /dev/null +++ b/mqtt_client/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.14-slim +ENV PYTHONUNBUFFERED=1 + +WORKDIR /mqtt-client + +RUN useradd \ + --shell /sbin/nologin \ + --uid 10001 \ + --no-create-home \ + mqtt-client-usr + +COPY requirements.txt . + +RUN python -m pip install -r requirements.txt + +USER mqtt-client-usr + +COPY . . + +CMD ["python3", "main.py"] diff --git a/mqtt_client/main.py b/mqtt_client/main.py index 1a3ca8a..78cdca6 100644 --- a/mqtt_client/main.py +++ b/mqtt_client/main.py @@ -1,5 +1,7 @@ import paho.mqtt.client as mqtt +from paho.mqtt.enums import MQTTProtocolVersion import json +from time import sleep def on_connect(client, userdata, flags, reason_code, properties): print(f"Conectado: {reason_code}") @@ -14,10 +16,19 @@ def on_message(client, userdata, msg): print(f"Unit: {payload["unit"]}") print(f"Timestamp: {payload["timestamp"]}") -mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=MQTTProtocolVersion.MQTTv5) mqttc.on_connect = on_connect mqttc.on_message = on_message +mqttc.username_pw_set('tester', 'rosebud') -mqttc.connect("127.0.0.1", 1883, 60) +connected = False + +while(not connected): + try: + mqttc.connect("127.0.0.1", 1883, 60, '', 0, True) + connected = True + except ConnectionRefusedError: + print("Failed to connect. Retrying...") + sleep(5) mqttc.loop_forever() |
