diff --git a/code/enums.py b/code/enums.py index 889f532..da5d11c 100644 --- a/code/enums.py +++ b/code/enums.py @@ -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 @@ -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 \ No newline at end of file + 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") diff --git a/code/models.py b/code/models.py index 5f7b088..303d5cd 100644 --- a/code/models.py +++ b/code/models.py @@ -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") @@ -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 \ No newline at end of file + sensor_model = Column(Integer) + dimension = Column(Integer) + timestamp = Column(DateTime) \ No newline at end of file diff --git a/code/routers/station.py b/code/routers/station.py index 0818f57..c971368 100644 --- a/code/routers/station.py +++ b/code/routers/station.py @@ -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() @@ -132,15 +133,24 @@ 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() @@ -148,7 +158,10 @@ async def create_station_data( 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 diff --git a/code/schemas.py b/code/schemas.py index 759b8a3..276aa32 100644 --- a/code/schemas.py +++ b/code/schemas.py @@ -20,6 +20,7 @@ class StationDataCreate(BaseModel): firmware: str apikey: str location: LocationCreate + source: Optional[int] = 1 class SensorDataCreate(BaseModel):