Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post station status #28

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion code/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,12 @@ class HourlyAverages(Base):
avg_value = Column(Float)
sensor_model = Column(Integer)
dimension = Column(Integer)
timestamp = Column(DateTime)
timestamp = Column(DateTime)


class StationStatus(Base):
id = Column(Integer, primary_key=True, index=True)
Column(Integer, ForeignKey('stations.id'))
timestamp = Column(DateTime)
level = Column(Integer)
message = Column(String)
143 changes: 87 additions & 56 deletions code/routers/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,81 @@
import io

from models import Station, Location, Measurement, Values
from schemas import StationDataCreate, SensorsCreate
from schemas import StationDataCreate, SensorsCreate, StationStatus
from utils import get_or_create_location, download_csv
from services.hourly_average import calculate_hourly_average


def validate_station_info(f):
async def wrapper(
station: StationDataCreate,
sensors: SensorsCreate,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)
):
# Prüfen, ob die Station bereits existiert
db_station = db.query(Station).filter(Station.device == station.device).first()

if db_station is None:
# 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,
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"
)

updated = False

# Überprüfen, ob Location aktualisiert werden muss
if db_station.location is None or (
db_station.location.lat != station.location.lat or
db_station.location.lon != station.location.lon or
db_station.location.height != float(station.location.height)
):
new_location = get_or_create_location(db, station.location.lat, station.location.lon, float(station.location.height))
db_station.location_id = new_location.id
updated = True

if db_station.firmware != station.firmware:
db_station.firmware = station.firmware
updated = True

if updated:
db.commit()

return await f(
station = station,
sensors = sensors,
background_tasks = background_tasks,
db_station = db_station,
db = db
)
return wrapper


router = APIRouter()


Expand Down Expand Up @@ -122,68 +193,28 @@ async def get_current_station_data(
return Response(content=content, media_type=media_type)


@router.post("/status", tags=["station"])
@validate_station_info
async def create_station_status(
station: StationDataCreate,
status: StationStatus,
db_station: Station
):

return {"status": "success"}


@router.post("/data", tags=["station"])
@validate_station_info
async def create_station_data(
station: StationDataCreate,
sensors: SensorsCreate,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)
db_station: Station,
db: Session = Depends(get_db),
):
# Empfangszeit des Requests erfassen
time_received = datetime.now()

# Prüfen, ob die Station bereits existiert
db_station = db.query(Station).filter(Station.device == station.device).first()

if db_station is None:
# 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,
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"
)

updated = False

# Überprüfen, ob Location aktualisiert werden muss
if db_station.location is None or (
db_station.location.lat != station.location.lat or
db_station.location.lon != station.location.lon or
db_station.location.height != float(station.location.height)
):
new_location = get_or_create_location(db, station.location.lat, station.location.lon, float(station.location.height))
db_station.location_id = new_location.id
updated = True

if db_station.firmware != station.firmware:
db_station.firmware = station.firmware
updated = True

if updated:
db.commit()
time_received = datetime.now()

# Durch alle Sensoren iterieren
for sensor_id, sensor_data in sensors.root.items():
Expand Down
8 changes: 7 additions & 1 deletion code/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ class SensorDataCreate(BaseModel):


class SensorsCreate(RootModel[Dict[int, SensorDataCreate]]):
pass
pass


class StationStatusCreate(BaseModel):
time: datetime
level: int
message: str