From 6fed254c8033bf4c67b65328474df6807c3b7eb8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Tue, 28 Oct 2025 20:25:17 -0300 Subject: refactor folders schema --- api/Dockerfile | 0 api/__init__.py | 0 api/database.py | 10 ++++++ api/main.py | 0 api/models.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ api/requirements.txt | 0 api/routes/__init__.py | 0 api/routes/locations.py | 0 api/routes/measures.py | 0 api/routes/readings.py | 0 api/routes/sensors.py | 0 api/routes/stations.py | 0 api/schemas/__init__.py | 0 api/schemas/location.py | 0 api/schemas/measures.py | 0 api/schemas/reading.py | 0 api/schemas/sensor.py | 0 api/schemas/station.py | 0 api/services/__init__.py | 0 esp/AS5600.ino | 47 ---------------------------- esp/BH1750.ino | 0 esp/BMP280.ino | 29 ----------------- esp/DHT11.ino | 26 ---------------- esp/LM393.ino | 42 ------------------------- mqtt_client/crud.py | 0 mqtt_client/schemas.py | 0 26 files changed, 91 insertions(+), 144 deletions(-) create mode 100644 api/Dockerfile create mode 100644 api/__init__.py create mode 100644 api/database.py create mode 100644 api/main.py create mode 100644 api/models.py create mode 100644 api/requirements.txt create mode 100644 api/routes/__init__.py create mode 100644 api/routes/locations.py create mode 100644 api/routes/measures.py create mode 100644 api/routes/readings.py create mode 100644 api/routes/sensors.py create mode 100644 api/routes/stations.py create mode 100644 api/schemas/__init__.py create mode 100644 api/schemas/location.py create mode 100644 api/schemas/measures.py create mode 100644 api/schemas/reading.py create mode 100644 api/schemas/sensor.py create mode 100644 api/schemas/station.py create mode 100644 api/services/__init__.py delete mode 100644 esp/AS5600.ino delete mode 100644 esp/BH1750.ino delete mode 100644 esp/BMP280.ino delete mode 100644 esp/DHT11.ino delete mode 100644 esp/LM393.ino delete mode 100644 mqtt_client/crud.py delete mode 100644 mqtt_client/schemas.py diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/database.py b/api/database.py new file mode 100644 index 0000000..c0691a1 --- /dev/null +++ b/api/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/api/main.py b/api/main.py new file mode 100644 index 0000000..e69de29 diff --git a/api/models.py b/api/models.py new file mode 100644 index 0000000..d6bd2ba --- /dev/null +++ b/api/models.py @@ -0,0 +1,81 @@ +from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean +from sqlalchemy.orm import relationship +from sqlalchemy.sql import func +from database import Base + +# ---------------------------- +# Location +# ---------------------------- +class Location(Base): + __tablename__ = "locations" + + id = Column(Integer, primary_key=True, index=True) + street = Column(String(200), nullable=True, index=True) + avenue = Column(String(200), nullable=True, index=True) + zip_code = Column(String(20), nullable=True, index=True) + + # Relationship: One location to many stations + stations = relationship("Station", back_populates="location") + + +# ---------------------------- +# Station +# ---------------------------- +class Station(Base): + __tablename__ = "stations" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String(100), nullable=False, index=True) + location_id = Column(Integer, ForeignKey("locations.id"), nullable=True, index=True) + installation_date = Column(DateTime, server_default=func.now()) + is_active = Column(Boolean, default=True) + + # Relationships + location = relationship("Location", back_populates="stations") + sensor_devices = relationship("SensorDevice", back_populates="station") + + +# ---------------------------- +# SensorDevice +# ---------------------------- +class SensorDevice(Base): + __tablename__ = "sensor_devices" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String(100), nullable=True, index=True) + installation_date = Column(DateTime, server_default=func.now()) + station_id = Column(Integer, ForeignKey("stations.id"), nullable=True, index=True) + is_active = Column(Boolean, default=True) + + # Relationships + station = relationship("Station", back_populates="sensor_devices") + readings = relationship("Reading", back_populates="sensor_device") + + +# ---------------------------- +# Measure +# ---------------------------- +class Measure(Base): + __tablename__ = "measures" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String(50), nullable=True, index=True) + + # Relationships + readings = relationship("Reading", back_populates="measure") + + +# ---------------------------- +# Reading +# ---------------------------- +class Reading(Base): + __tablename__ = "readings" + + id = Column(Integer, primary_key=True, index=True) + sensor_device_id = Column(Integer, ForeignKey("sensor_devices.id"), nullable=True, index=True) + measure_id = Column(Integer, ForeignKey("measures.id"), nullable=True, index=True) + value = Column(Float, nullable=True) + + # Relationships + sensor_device = relationship("SensorDevice", back_populates="readings") + measure = relationship("Measure", back_populates="readings") diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/__init__.py b/api/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/locations.py b/api/routes/locations.py new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/measures.py b/api/routes/measures.py new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/readings.py b/api/routes/readings.py new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/sensors.py b/api/routes/sensors.py new file mode 100644 index 0000000..e69de29 diff --git a/api/routes/stations.py b/api/routes/stations.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/__init__.py b/api/schemas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/location.py b/api/schemas/location.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/measures.py b/api/schemas/measures.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/reading.py b/api/schemas/reading.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/sensor.py b/api/schemas/sensor.py new file mode 100644 index 0000000..e69de29 diff --git a/api/schemas/station.py b/api/schemas/station.py new file mode 100644 index 0000000..e69de29 diff --git a/api/services/__init__.py b/api/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/esp/AS5600.ino b/esp/AS5600.ino deleted file mode 100644 index 233aa13..0000000 --- a/esp/AS5600.ino +++ /dev/null @@ -1,47 +0,0 @@ -#include - -#define AS5600_ADDR 0x36 // endereço I2C do AS5600 -#define RAW_ANGLE_REG 0x0C - -unsigned long lastTime = 0; - -String direcaoCardinal(float angulo) { - if (angulo >= 337.5 || angulo < 22.5) return "Norte"; - else if (angulo < 67.5) return "Nordeste"; - else if (angulo < 112.5) return "Leste"; - else if (angulo < 157.5) return "Sudeste"; - else if (angulo < 202.5) return "Sul"; - else if (angulo < 247.5) return "Sudoeste"; - else if (angulo < 292.5) return "Oeste"; - else return "Noroeste"; -} - -uint16_t readRawAngle() { - Wire.beginTransmission(AS5600_ADDR); - Wire.write(RAW_ANGLE_REG); - Wire.endTransmission(); - Wire.requestFrom(AS5600_ADDR, 2); - uint16_t high = Wire.read(); - uint16_t low = Wire.read(); - return (high << 8) | low; -} - -void setup() { - Serial.begin(115200); - Wire.begin(); // SDA e SCL padrão do ESP32 (21 e 22) - Serial.println("Leitura do AS5600 - Direcao do Vento"); -} - -void loop() { - uint16_t raw = readRawAngle(); - // O AS5600 fornece 12 bits (0–4095) para 0–360° - float angulo = (raw & 0x0FFF) * 360.0 / 4096.0; - - if (millis() - lastTime >= 1000) { - Serial.print("Angulo: "); - Serial.print(angulo, 2); - Serial.print("° | Direcao: "); - Serial.println(direcaoCardinal(angulo)); - lastTime = millis(); - } -} diff --git a/esp/BH1750.ino b/esp/BH1750.ino deleted file mode 100644 index e69de29..0000000 diff --git a/esp/BMP280.ino b/esp/BMP280.ino deleted file mode 100644 index 417edb1..0000000 --- a/esp/BMP280.ino +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include - -Adafruit_BMP280 bmp; // cria o objeto do sensor - -void setup() { - Serial.begin(115200); - Wire.begin(21, 22); // SDA e SCL - Serial.println("Teste do Sensor BMP280 - Pressão e Temperatura"); - - if (!bmp.begin(0x76)) { // endereço I2C padrão - Serial.println("Erro: BMP280 não encontrado!"); - while (1); - } -} - -void loop() { - float temp = bmp.readTemperature(); - float press = bmp.readPressure() / 100.0F; // converte Pa para hPa - - Serial.print("Temperatura: "); - Serial.print(temp); - Serial.print(" °C | Pressão: "); - Serial.print(press); - Serial.println(" hPa"); - - delay(2000); // leitura a cada 2 segundos -} \ No newline at end of file diff --git a/esp/DHT11.ino b/esp/DHT11.ino deleted file mode 100644 index e0f0342..0000000 --- a/esp/DHT11.ino +++ /dev/null @@ -1,26 +0,0 @@ -#include "DHT.h" -#define DHTPIN 32 -#define DHTTYPE DHT11 - -DHT dht(DHTPIN, DHTTYPE); - -void setup() { - Serial.begin(115200); - dht.begin(); - Serial.println("Teste sensor DHT11 Temperatura e humidade"); -} -void loop(){ - float h = dht.readHumidity(); - float t = dht.readTemperature(); - - if (isnan(h) || isnan(t)){ - Serial.println("ERRO de leitura"); - return; - } - Serial.print("Humidade: "); - Serial.print(h); - Serial.print("% | Temperatura:"); - Serial.print(t); - Serial.println(" °C"); - delay(2000); -} \ No newline at end of file diff --git a/esp/LM393.ino b/esp/LM393.ino deleted file mode 100644 index 6e6a5d5..0000000 --- a/esp/LM393.ino +++ /dev/null @@ -1,42 +0,0 @@ -#define SENSOR_PIN 35 // pino de sinal (DO) - -unsigned long lastTime = 0; -unsigned int pulsos = 0; -int estadoAnterior = HIGH; // começa em HIGH (sem interrupção) - -// Configurações do sensor -const int PULSOS_POR_VOLTA = 10; // 10 furos = 10 pulsos por volta -const float FATOR_KMH = 2.4; // km/h por rotação por segundo (ajuste conforme calibração) - -void setup() { - Serial.begin(115200); - pinMode(SENSOR_PIN, INPUT); - Serial.println("Anemômetro LM393 - Velocidade do vento"); -} - -void loop() { - int estadoAtual = digitalRead(SENSOR_PIN); - - // Detecta borda de descida: HIGH → LOW - if (estadoAnterior == HIGH && estadoAtual == LOW) { - pulsos++; - } - - estadoAnterior = estadoAtual; // atualiza para próxima leitura - - // A cada 1 segundo calcula RPM e velocidade - if (millis() - lastTime >= 1000) { - float rps = (float)pulsos / PULSOS_POR_VOLTA; - float rpm = rps * 60.0; - float velocidade = rps * FATOR_KMH; - - Serial.print("RPM: "); - Serial.print(rpm); - Serial.print(" | Velocidade: "); - Serial.print(velocidade); - Serial.println(" km/h"); - - pulsos = 0; - lastTime = millis(); - } -} \ No newline at end of file diff --git a/mqtt_client/crud.py b/mqtt_client/crud.py deleted file mode 100644 index e69de29..0000000 diff --git a/mqtt_client/schemas.py b/mqtt_client/schemas.py deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3