From 52a4fe2625b63683fcdcd74f704e49aa65cb0dd8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Sun, 26 Oct 2025 16:09:12 -0300 Subject: database connection tested successfully - tables readed on pgadmin --- mqtt_client/database.py | 10 ++++++++++ mqtt_client/main.py | 13 ++++++++++--- mqtt_client/requirements.txt | 4 +++- 3 files changed, 23 insertions(+), 4 deletions(-) (limited to 'mqtt_client') diff --git a/mqtt_client/database.py b/mqtt_client/database.py index e69de29..c0691a1 100644 --- a/mqtt_client/database.py +++ b/mqtt_client/database.py @@ -0,0 +1,10 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +import os + +DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://root:public@postgres:5432/control_station") + +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() diff --git a/mqtt_client/main.py b/mqtt_client/main.py index dad09b5..109ca49 100644 --- a/mqtt_client/main.py +++ b/mqtt_client/main.py @@ -2,8 +2,15 @@ import paho.mqtt.client as mqtt from paho.mqtt.enums import MQTTProtocolVersion import json from time import sleep +from database import engine +from models import Base import os +print("Creating ORM SQL Tables..") +Base.metadata.create_all(bind=engine) +print("Tables created successfully.") + + def on_connect(client, userdata, flags, reason_code, properties): print(f"Conectado: {reason_code}") # Me inscrevo em todos os tópicos sobre clima @@ -13,9 +20,9 @@ def on_connect(client, userdata, flags, reason_code, properties): def on_message(client, userdata, msg): payload = json.loads(msg.payload) print(msg.topic) - print(f"Value: {payload["value"]}") - print(f"Unit: {payload["unit"]}") - print(f"Timestamp: {payload["timestamp"]}") + print(f"Value: {payload['value']}") + print(f"Unit: {payload['unit']}") + print(f"Timestamp: {payload['timestamp']}") try: user_name = os.environ["MQTT_CLIENT_USER"] diff --git a/mqtt_client/requirements.txt b/mqtt_client/requirements.txt index 96a2716..1521b2d 100644 --- a/mqtt_client/requirements.txt +++ b/mqtt_client/requirements.txt @@ -1 +1,3 @@ -paho-mqtt==2.1.0 +sqlalchemy +psycopg2-binary +paho-mqtt -- cgit v1.2.3