diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-10-31 22:44:37 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-10-31 22:44:37 -0300 |
| commit | d24056dfa246d6ecf67447fa65eb0cdd67942df3 (patch) | |
| tree | 70e25f610ab41d8028114fd167b0c2ef51e34a1a | |
| parent | 032a77c0b1a2e0e2c247f511c0dc6d2555e691fb (diff) | |
Adicionado campos no banco de dados
| -rw-r--r-- | api/models.py | 2 | ||||
| -rw-r--r-- | mqtt_client/main.py | 3 | ||||
| -rw-r--r-- | mqtt_client/models.py | 2 |
3 files changed, 6 insertions, 1 deletions
diff --git a/api/models.py b/api/models.py index d6bd2ba..98549bc 100644 --- a/api/models.py +++ b/api/models.py @@ -60,6 +60,7 @@ class Measure(Base): id = Column(Integer, primary_key=True, index=True) name = Column(String(50), nullable=True, index=True) + unit_code = Column(String(10), nullable=True) # Relationships readings = relationship("Reading", back_populates="measure") @@ -75,6 +76,7 @@ class Reading(Base): 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) + time = Column(DateTime, nullable=True) # Relationships sensor_device = relationship("SensorDevice", back_populates="readings") diff --git a/mqtt_client/main.py b/mqtt_client/main.py index f460520..0ba9f10 100644 --- a/mqtt_client/main.py +++ b/mqtt_client/main.py @@ -6,6 +6,7 @@ from database import engine, SessionLocal from models import Base, Reading import os from time import sleep +from datetime import datetime print("Sleeping for 10 seconds to wait for db setup...") sleep(10) @@ -64,7 +65,7 @@ def on_message(client, userdata, msg): session.begin() for reading in readings: print(reading) - session.add(Reading(sensor_device_id=stationId, measure_id=measure, value=reading['value'])) + session.add(Reading(sensor_device_id=stationId, measure_id=measure, value=reading['value'], time=datetime.fromtimestamp(reading['timestamp'],))) session.commit() session.close() diff --git a/mqtt_client/models.py b/mqtt_client/models.py index d6bd2ba..98549bc 100644 --- a/mqtt_client/models.py +++ b/mqtt_client/models.py @@ -60,6 +60,7 @@ class Measure(Base): id = Column(Integer, primary_key=True, index=True) name = Column(String(50), nullable=True, index=True) + unit_code = Column(String(10), nullable=True) # Relationships readings = relationship("Reading", back_populates="measure") @@ -75,6 +76,7 @@ class Reading(Base): 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) + time = Column(DateTime, nullable=True) # Relationships sensor_device = relationship("SensorDevice", back_populates="readings") |
