1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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)
unit_code = Column(String(10), nullable=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)
time = Column(DateTime, nullable=True)
# Relationships
sensor_device = relationship("SensorDevice", back_populates="readings")
measure = relationship("Measure", back_populates="readings")
|