Skip to content

Commit

Permalink
Merge pull request #24 from luftdaten-at/add-source-to-station-model
Browse files Browse the repository at this point in the history
Add source to station model
  • Loading branch information
silvioheinze authored Oct 5, 2024
2 parents a5ef23c + aa54984 commit f6f3475
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
27 changes: 25 additions & 2 deletions code/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def get_name(cls, dimension_id: int) -> str:
"""
return cls._names.get(dimension_id, "Unknown")


class SensorModel():
SEN5X = 1
BMP280 = 2
Expand Down Expand Up @@ -109,10 +110,32 @@ class SensorModel():
@classmethod
def get_sensor_name(cls, sensor_model):
return cls._names.get(sensor_model, "Unknown Sensor")



class LdProduct():
AIR_AROUND = 1
AIR_CUBE = 2
AIR_STATION = 3
AIR_BADGE = 4
AIR_BIKE = 5
AIR_BIKE = 5


class Source():
LD = 1
LDTTN = 2
SC = 3

_names = {
LD: "Luftdaten.at",
LDTTN: "Luftdaten.at TTN LoRaWAN",
SC: "sensor.community"
}

@classmethod
def get_name(cls, source_id: int) -> str:
"""
Gibt den Namen der angegebenen Source zurück.
:param source_id: Die ID der Source
:return: Der zugehörige Name oder 'Unknown', wenn kein Name vorhanden ist
"""
return cls._names.get(source_id, "Unknown")
7 changes: 4 additions & 3 deletions code/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Station(Base):
firmware = Column(String)
apikey = Column(String)
last_active = Column(DateTime)
source = Column(Integer)
# Relationships:
location_id = Column(Integer, ForeignKey('locations.id'))
location = relationship("Location", back_populates="stations")
Expand Down Expand Up @@ -103,6 +104,6 @@ class HourlyAverages(Base):
station_id = Column(Integer, ForeignKey('stations.id'))
station = relationship("Station")
avg_value = Column(Float)
sensor_model = Column(Integer) # Referenz auf den Sensortyp
dimension = Column(Integer) # Referenz auf die Dimension des Messwertes
timestamp = Column(DateTime) # Zeitstempel für die Stunde
sensor_model = Column(Integer)
dimension = Column(Integer)
timestamp = Column(DateTime)
25 changes: 19 additions & 6 deletions code/routers/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from database import get_db
from models import Station, Measurement, Values
import json

from models import Station, Location, Measurement, Values
from schemas import StationDataCreate, SensorsCreate
from utils import get_or_create_location, download_csv
import json
from tasks import calculate_hourly_average

router = APIRouter()
Expand Down Expand Up @@ -132,23 +133,35 @@ async def create_station_data(
db_station = db.query(Station).filter(Station.device == station.device).first()

if db_station is None:
# Wenn die Station noch nicht existiert, erstelle eine neue Location und Station
new_location = get_or_create_location(db, station.location.lat, station.location.lon, float(station.location.height))
# Neue Station und neue Location anlegen
new_location = Location(
lat=station.location.lat,
lon=station.location.lon,
height=float(station.location.height)
)
db.add(new_location)
db.commit()
db.refresh(new_location)

# Neue Station anlegen und das source-Feld überprüfen (Standardwert ist 1)
db_station = Station(
device=station.device,
firmware=station.firmware,
apikey=station.apikey,
location_id=new_location.id,
last_active=station.time
last_active=station.time,
source=station.source if station.source is not None else 1
)
db.add(db_station)
db.commit()
db.refresh(db_station)
else:
# Station existiert, API-Schlüssel überprüfen
if db_station.apikey != station.apikey:
raise HTTPException(status_code=401, detail="Invalid API key")
raise HTTPException(
status_code=401,
detail="Invalid API key"
)

updated = False

Expand Down
1 change: 1 addition & 0 deletions code/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StationDataCreate(BaseModel):
firmware: str
apikey: str
location: LocationCreate
source: Optional[int] = 1


class SensorDataCreate(BaseModel):
Expand Down

0 comments on commit f6f3475

Please sign in to comment.