From 22935fae60b6ab2040596d4a78d1812e2a16589e Mon Sep 17 00:00:00 2001 From: Matheus Date: Fri, 10 Oct 2025 21:52:46 -0300 Subject: refactor:remoção do banco MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 45 ++++++++++------------------------------- mosquitto-config/.gitignore | 1 + mosquitto-config/mosquitto.conf | 3 +++ mqtt_client/.dockerignore | 1 + mqtt_client/Dockerfile | 20 ++++++++++++++++++ mqtt_client/main.py | 15 ++++++++++++-- 6 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 mosquitto-config/.gitignore create mode 100644 mosquitto-config/mosquitto.conf create mode 100644 mqtt_client/.dockerignore create mode 100644 mqtt_client/Dockerfile diff --git a/compose.yaml b/compose.yaml index 69068ce..f56c1d2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -9,40 +9,17 @@ # Lista de serviços services: - # Não tem influxdb versão 3 nos repositórios do debian 13 (meu servidor), - # então vamos usar um container docker. - influxdb: - # Esta imagem é fornecida pelos desenvolvedores do influxdb. - image: influxdb:3-core - # Este é o nome do container quando for criado. - container_name: influxdb - user: "${INFLUXDB_UID}:${INFLUXDB_GID}" - # Lista de portas que o container expõe para o host. Pense como se fosse um - # firewall, se a porta não estiver aqui, o host não tem acesso. A porta - # 8181 é padrão para o influxdb versão 3. + mosquitto: + image: "eclipse-mosquitto:2.0.22" ports: - - 8181:8181 - # O comando que será executado quando iniciarmos o container. Consulte o manual do - # influxdb para entender a sintaxe. - command: - - influxdb3 - - serve - - --node-id=node0 - - --object-store=file - - --data-dir=/var/lib/influxdb3/data - - --plugin-dir=/var/lib/influxdb3/plugins - - --admin-token-file=/etc/influxdb3/admin-token.json - # Quando desligarmos o container, todos os dados somem. - # Aqui mapeamos alguns diretórios do container para diretórios do host - # para não perdermos dados. + - "127.0.0.1:1883:1883" volumes: - type: bind - source: $INFLUXDB_CONFIG_DIR - target: /etc/influxdb3 - read_only: true - - type: bind - source: ${INFLUXDB_DATA_DIR} - target: /var/lib/influxdb3/data - - type: bind - source: ${INFLUXDB_PLUGINS_DIR} - target: /var/lib/influxdb3/plugins + source: ./mosquitto-config/ + target: /mosquitto/config/ + mqtt-client: + build: + context: ./mqtt_client/ + network_mode: "service:mosquitto" + depends_on: + - mosquitto diff --git a/mosquitto-config/.gitignore b/mosquitto-config/.gitignore new file mode 100644 index 0000000..b02cae3 --- /dev/null +++ b/mosquitto-config/.gitignore @@ -0,0 +1 @@ +pass-file diff --git a/mosquitto-config/mosquitto.conf b/mosquitto-config/mosquitto.conf new file mode 100644 index 0000000..f913973 --- /dev/null +++ b/mosquitto-config/mosquitto.conf @@ -0,0 +1,3 @@ +listener 1883 0.0.0.0 +allow_anonymous false +password_file /mosquitto/config/pass-file 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() -- cgit v1.2.3