Migrate from Flask to FastAPI (#163)

* Migrate from Flask to FastAPI

 * FastAPI migration:
    - Use pydantic model classes as input parameters to the
      data/calculation classes.
    - Interface field names changed to constructor parameter names (for
      simplicity only during transition, should be updated in a followup
      PR).
    - Add basic interface requirements (e.g. some values > 0, etc.).
 * Update tests for new data format.
 * Python requirement down to 3.9 (TypeGuard no longer needed)
 * Makefile: Add helpful targets (e.g. development server with reload)

* Move API doc from README to pydantic model classes (swagger)

 * Link to swagger.io with own openapi.yml.
 * Commit openapi.json and check with pytest for changes so the
   documentation is always up-to-date.

* Streamline docker

* FastAPI: Run startup action on dev server

 * Fix config for /strompreis, endpoint still broken however.

* test_openapi: Compare against docs/.../openapi.json

* Move fastapi to server/ submodule

 * See #187 for new repository structure.
This commit is contained in:
Dominique Lasserre
2024-11-15 22:27:25 +01:00
committed by GitHub
parent ed3226e522
commit f61665669f
38 changed files with 997 additions and 1331 deletions

View File

@@ -1,10 +1,16 @@
import numpy as np
import pytest
from akkudoktoreos.class_akku import PVAkku
from akkudoktoreos.class_ems import EnergieManagementSystem
from akkudoktoreos.class_haushaltsgeraet import Haushaltsgeraet
from akkudoktoreos.class_inverter import Wechselrichter
from akkudoktoreos.class_akku import EAutoParameters, PVAkku, PVAkkuParameters
from akkudoktoreos.class_ems import (
EnergieManagementSystem,
EnergieManagementSystemParameters,
)
from akkudoktoreos.class_haushaltsgeraet import (
Haushaltsgeraet,
HaushaltsgeraetParameters,
)
from akkudoktoreos.class_inverter import Wechselrichter, WechselrichterParameters
from akkudoktoreos.config import AppConfig
prediction_hours = 48
@@ -17,21 +23,30 @@ start_hour = 1
def create_ems_instance(tmp_config: AppConfig) -> EnergieManagementSystem:
"""Fixture to create an EnergieManagementSystem instance with given test parameters."""
# Initialize the battery and the inverter
akku = PVAkku(kapazitaet_wh=5000, start_soc_prozent=80, hours=48, min_soc_prozent=10)
akku = PVAkku(
PVAkkuParameters(kapazitaet_wh=5000, start_soc_prozent=80, min_soc_prozent=10),
hours=prediction_hours,
)
akku.reset()
wechselrichter = Wechselrichter(10000, akku)
wechselrichter = Wechselrichter(WechselrichterParameters(max_leistung_wh=10000), akku)
# Household device (currently not used, set to None)
home_appliance = Haushaltsgeraet(
HaushaltsgeraetParameters(
verbrauch_wh=2000,
dauer_h=2,
),
hours=prediction_hours,
verbrauch_wh=2000,
dauer_h=2,
)
home_appliance.set_startzeitpunkt(2)
# Example initialization of electric car battery
eauto = PVAkku(kapazitaet_wh=26400, start_soc_prozent=10, hours=48, min_soc_prozent=10)
eauto.set_charge_per_hour(np.full(48, 1))
eauto = PVAkku(
EAutoParameters(kapazitaet_wh=26400, start_soc_prozent=10, min_soc_prozent=10),
hours=prediction_hours,
)
eauto.set_charge_per_hour(np.full(prediction_hours, 1))
# Parameters based on previous example data
pv_prognose_wh = [
0,
@@ -135,7 +150,8 @@ def create_ems_instance(tmp_config: AppConfig) -> EnergieManagementSystem:
0.0002780,
]
einspeiseverguetung_euro_pro_wh = [0.00007] * len(strompreis_euro_pro_wh)
einspeiseverguetung_euro_pro_wh = 0.00007
preis_euro_pro_wh_akku = 0.0001
gesamtlast = [
676.71,
@@ -190,12 +206,15 @@ def create_ems_instance(tmp_config: AppConfig) -> EnergieManagementSystem:
# Initialize the energy management system with the respective parameters
ems = EnergieManagementSystem(
config=tmp_config.eos,
pv_prognose_wh=pv_prognose_wh,
strompreis_euro_pro_wh=strompreis_euro_pro_wh,
einspeiseverguetung_euro_pro_wh=einspeiseverguetung_euro_pro_wh,
tmp_config.eos,
EnergieManagementSystemParameters(
pv_prognose_wh=pv_prognose_wh,
strompreis_euro_pro_wh=strompreis_euro_pro_wh,
einspeiseverguetung_euro_pro_wh=einspeiseverguetung_euro_pro_wh,
preis_euro_pro_wh_akku=preis_euro_pro_wh_akku,
gesamtlast=gesamtlast,
),
eauto=eauto,
gesamtlast=gesamtlast,
haushaltsgeraet=home_appliance,
wechselrichter=wechselrichter,
)
@@ -249,7 +268,7 @@ def test_simulation(create_ems_instance):
"akku_soc_pro_stunde",
"Einnahmen_Euro_pro_Stunde",
"Gesamtbilanz_Euro",
"E-Auto_SoC_pro_Stunde",
"EAuto_SoC_pro_Stunde",
"Gesamteinnahmen_Euro",
"Gesamtkosten_Euro",
"Verluste_Pro_Stunde",