beginn new version webservice

This commit is contained in:
Michi 2025-03-26 11:17:10 +01:00
parent a38e1678a6
commit 831ce1dbc8
4 changed files with 237 additions and 87 deletions

View file

@ -1,94 +1,47 @@
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy import Column, Integer, Float, String, DateTime, create_engine, ForeignKey
from sqlalchemy.orm import sessionmaker, declarative_base, Session, relationship
# Webservice
# INP21b - Timo Weber & Michael von Ah
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import psycopg2
import dbfunctions as db
import requests, secrets, hashlib, re, string, random, os
from datetime import datetime, timedelta, date, time
import json
from datetime import datetime
import os
DATABASE_URL = os.getenv("DB_CONNECTION_STRING", "postgresql://user:password@localhost/sensordb")
Base = declarative_base()
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
""" class apiFunctions:
def __init__(self) -> None:
self.alternativeImage = "https://cdn.ep.neodym.dev/media/20240505-halloween.jpeg"
app = FastAPI()
def getAttractionList(self):
query = 'SELECT "name", "id", "imageurl", "description" FROM "attraction" ORDER BY "name";'
attractions = db.executeQuery(query)
if attractions:
return attractions
else:
raise Exception("Cannot generate list of attractions. Request invalid") """
# Neue Clients-Tabelle
class Client(Base):
__tablename__ = "clients"
#### API ####
app = FastAPI(
title="M241-M245-BBZW-Horizion",
description="BBZW-Horizon",
summary="BBZW-Horizon",
version="0.0.1"
)
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True, index=True)
class Session(BaseModel):
username: str = None
token: str = None
message: str = None
timestamp: datetime = datetime.now()
class SensorData(Base):
__tablename__ = "sensor_data"
id = Column(Integer, primary_key=True, index=True)
clientid = Column(Integer, ForeignKey("clients.id"), index=True)
timestamp = Column(DateTime, default=datetime.utcnow)
temperature = Column(Float)
humidity = Column(Float)
pressure = Column(Float)
voc = Column(Float)
gas = Column(Float)
client = relationship("Client")
class SessionToken(Base):
__tablename__ = "sessions"
id = Column(Integer, primary_key=True, index=True)
token = Column(String, unique=True, index=True)
validuntil = Column(DateTime)
userid = Column(Integer, ForeignKey("user.id"))
def get_db():
db = SessionLocal()
@app.post("/account/new-session", tags=["account"])
async def initNewSessionApi(username: str, password: str) -> Session:
try:
yield db
finally:
db.close()
def verify_token(token: str, db: Session):
session = db.query(SessionToken).filter(SessionToken.token == token).first()
if not session or session.validuntil < datetime.utcnow():
raise HTTPException(status_code=401, detail="Invalid or expired token")
return session
@app.post("/sensor-data/")
def receive_sensor_data(
token: str,
clientname: str, # Ändert clientid zu clientname
temperature: float,
humidity: float,
pressure: float,
voc: float,
gas: float,
db: Session = Depends(get_db)
):
verify_token(token, db)
# Suche die Client-ID anhand des Client-Namens
client = db.query(Client).filter(Client.name == clientname).first()
if not client:
raise HTTPException(status_code=404, detail="Client not found")
sensor_data = SensorData(
clientid=client.id, # Verwende die gefundene ID
temperature=temperature,
humidity=humidity,
pressure=pressure,
voc=voc,
gas=gas
)
db.add(sensor_data)
db.commit()
db.refresh(sensor_data)
return {"message": "Data received", "id": sensor_data.id}
@app.get("/sensor-data/")
def get_sensor_data(db: Session = Depends(get_db)):
data = db.query(SensorData).all()
return data
# Erstelle die Tabellen, falls sie noch nicht existieren
Base.metadata.create_all(bind=engine)
return Session(username="username", token="sessionToken", message="Session initiated successfully")
except Exception as error:
raise HTTPException(status_code=401, detail=f"{error}")