2024-09-10 17:17:49 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-09-18 21:47:14 +02:00
|
|
|
|
2024-03-31 13:00:01 +02:00
|
|
|
import os
|
2024-09-18 21:47:14 +02:00
|
|
|
|
2024-03-29 08:27:39 +01:00
|
|
|
import matplotlib
|
2024-09-18 21:47:14 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
matplotlib.use(
|
|
|
|
"Agg"
|
|
|
|
) # Sets the Matplotlib backend to 'Agg' for rendering plots in environments without a display
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
import pandas as pd
|
2024-07-30 09:22:55 +02:00
|
|
|
from config import *
|
2024-10-03 11:05:44 +02:00
|
|
|
from flask import Flask, jsonify, redirect, request, send_from_directory, url_for
|
|
|
|
|
2024-09-18 21:47:14 +02:00
|
|
|
from modules.class_akku import *
|
|
|
|
from modules.class_ems import *
|
|
|
|
from modules.class_heatpump import *
|
|
|
|
from modules.class_load import *
|
|
|
|
from modules.class_load_container import *
|
|
|
|
from modules.class_load_corrector import *
|
|
|
|
from modules.class_optimize import *
|
|
|
|
from modules.class_pv_forecast import *
|
|
|
|
from modules.class_soc_calc import *
|
|
|
|
from modules.class_sommerzeit import *
|
|
|
|
from modules.class_strompreis import *
|
|
|
|
from modules.visualize import *
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-04-02 16:46:16 +02:00
|
|
|
app = Flask(__name__)
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
opt_class = optimization_problem(
|
|
|
|
prediction_hours=prediction_hours, strafe=10, optimization_hours=optimization_hours
|
|
|
|
)
|
2024-07-30 09:22:55 +02:00
|
|
|
|
2024-09-08 10:28:54 +02:00
|
|
|
# @app.route('/last_correction', methods=['GET'])
|
|
|
|
# def flask_last_correction():
|
2024-09-20 11:09:54 +02:00
|
|
|
# if request.method == 'GET':
|
|
|
|
# year_energy = float(request.args.get("year_energy"))
|
|
|
|
# date_now, date = get_start_enddate(prediction_hours, startdate=datetime.now().date())
|
|
|
|
# ###############
|
|
|
|
# # Load Forecast
|
|
|
|
# ###############
|
|
|
|
# lf = LoadForecast(filepath=r'load_profiles.npz', year_energy=year_energy)
|
|
|
|
# leistung_haushalt = lf.get_stats_for_date_range(date_now, date)[0] # Only the expected value!
|
|
|
|
#
|
|
|
|
# gesamtlast = Gesamtlast(prediction_hours=prediction_hours)
|
|
|
|
# gesamtlast.hinzufuegen("Haushalt", leistung_haushalt)
|
|
|
|
# # ###############
|
|
|
|
# # Heat Pump (WP)
|
|
|
|
# # ##############
|
|
|
|
# # leistung_wp = wp.simulate_24h(temperature_forecast)
|
|
|
|
# # gesamtlast.hinzufuegen("Heatpump", leistung_wp)
|
|
|
|
# last = gesamtlast.gesamtlast_berechnen()
|
|
|
|
# print(last)
|
|
|
|
# return jsonify(last.tolist())
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
@app.route("/soc", methods=["GET"])
|
2024-05-01 10:02:16 +02:00
|
|
|
def flask_soc():
|
2024-09-20 11:09:54 +02:00
|
|
|
# MariaDB connection details
|
2024-07-30 09:22:55 +02:00
|
|
|
config = db_config
|
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Set parameters for SOC (State of Charge) calculation
|
2024-07-30 09:22:55 +02:00
|
|
|
voltage_high_threshold = 55.4 # 100% SoC
|
|
|
|
voltage_low_threshold = 46.5 # 0% SoC
|
2024-09-20 11:09:54 +02:00
|
|
|
current_low_threshold = 2 # Low current threshold for both states
|
|
|
|
gap = 30 # Time gap in minutes for grouping maxima/minima
|
|
|
|
bat_capacity = 33 * 1000 / 48 # Battery capacity in watt-hours
|
2024-07-30 09:22:55 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Define the reference time point (3 weeks ago)
|
2024-10-03 11:05:44 +02:00
|
|
|
zeitpunkt_x = (datetime.now() - timedelta(weeks=3)).strftime("%Y-%m-%d %H:%M:%S")
|
2024-07-30 09:22:55 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Instantiate BatteryDataProcessor and perform calculations
|
2024-10-03 11:05:44 +02:00
|
|
|
processor = BatteryDataProcessor(
|
|
|
|
config,
|
|
|
|
voltage_high_threshold,
|
|
|
|
voltage_low_threshold,
|
|
|
|
current_low_threshold,
|
|
|
|
gap,
|
|
|
|
bat_capacity,
|
|
|
|
)
|
2024-07-30 09:22:55 +02:00
|
|
|
processor.connect_db()
|
|
|
|
processor.fetch_data(zeitpunkt_x)
|
|
|
|
processor.process_data()
|
|
|
|
last_points_100_df, last_points_0_df = processor.find_soc_points()
|
2024-10-03 11:05:44 +02:00
|
|
|
soc_df, integration_results = processor.calculate_resetting_soc(
|
|
|
|
last_points_100_df, last_points_0_df
|
|
|
|
)
|
2024-09-20 11:09:54 +02:00
|
|
|
# soh_df = processor.calculate_soh(integration_results) # Optional State of Health calculation
|
|
|
|
processor.update_database_with_soc(soc_df) # Update database with SOC data
|
|
|
|
# processor.plot_data(last_points_100_df, last_points_0_df, soc_df) # Optional data visualization
|
|
|
|
processor.disconnect_db() # Disconnect from the database
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-07-30 09:22:55 +02:00
|
|
|
return jsonify("Done")
|
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
@app.route("/strompreis", methods=["GET"])
|
2024-07-30 09:22:55 +02:00
|
|
|
def flask_strompreis():
|
2024-09-20 11:09:54 +02:00
|
|
|
# Get the current date and the end date based on prediction hours
|
2024-10-03 11:05:44 +02:00
|
|
|
date_now, date = get_start_enddate(
|
|
|
|
prediction_hours, startdate=datetime.now().date()
|
|
|
|
)
|
|
|
|
filepath = os.path.join(
|
|
|
|
r"test_data", r"strompreise_akkudokAPI.json"
|
|
|
|
) # Adjust the path to the JSON file
|
|
|
|
price_forecast = HourlyElectricityPriceForecast(
|
|
|
|
source=f"https://api.akkudoktor.net/prices?start={date_now}&end={date}",
|
|
|
|
prediction_hours=prediction_hours,
|
|
|
|
)
|
|
|
|
specific_date_prices = price_forecast.get_price_for_daterange(
|
|
|
|
date_now, date
|
|
|
|
) # Fetch prices for the specified date range
|
2024-09-20 11:09:54 +02:00
|
|
|
return jsonify(specific_date_prices.tolist())
|
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Endpoint to handle total load calculation based on the latest measured data
|
2024-10-03 11:05:44 +02:00
|
|
|
@app.route("/gesamtlast", methods=["POST"])
|
2024-09-08 10:28:54 +02:00
|
|
|
def flask_gesamtlast():
|
2024-09-20 11:09:54 +02:00
|
|
|
# Retrieve data from the JSON body
|
2024-09-08 10:28:54 +02:00
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
# Extract year_energy and prediction_hours from the request JSON
|
|
|
|
year_energy = float(data.get("year_energy"))
|
2024-10-03 11:05:44 +02:00
|
|
|
prediction_hours = int(
|
|
|
|
data.get("hours", 48)
|
|
|
|
) # Default to 48 hours if not specified
|
2024-09-08 10:28:54 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Measured data in JSON format
|
2024-09-08 10:28:54 +02:00
|
|
|
measured_data_json = data.get("measured_data")
|
|
|
|
measured_data = pd.DataFrame(measured_data_json)
|
2024-10-03 11:05:44 +02:00
|
|
|
measured_data["time"] = pd.to_datetime(measured_data["time"])
|
2024-09-08 10:28:54 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Ensure datetime has timezone info for accurate calculations
|
2024-10-03 11:05:44 +02:00
|
|
|
if measured_data["time"].dt.tz is None:
|
|
|
|
measured_data["time"] = measured_data["time"].dt.tz_localize("Europe/Berlin")
|
2024-09-08 10:28:54 +02:00
|
|
|
else:
|
2024-10-03 11:05:44 +02:00
|
|
|
measured_data["time"] = measured_data["time"].dt.tz_convert("Europe/Berlin")
|
2024-09-08 10:28:54 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Remove timezone info after conversion to simplify further processing
|
2024-10-03 11:05:44 +02:00
|
|
|
measured_data["time"] = measured_data["time"].dt.tz_localize(None)
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-09-08 10:28:54 +02:00
|
|
|
# Instantiate LoadForecast and generate forecast data
|
2024-10-03 11:05:44 +02:00
|
|
|
lf = LoadForecast(filepath=r"load_profiles.npz", year_energy=year_energy)
|
2024-09-08 10:28:54 +02:00
|
|
|
forecast_list = []
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Generate daily forecasts for the date range based on measured data
|
2024-10-03 11:05:44 +02:00
|
|
|
for single_date in pd.date_range(
|
|
|
|
measured_data["time"].min().date(), measured_data["time"].max().date()
|
|
|
|
):
|
|
|
|
date_str = single_date.strftime("%Y-%m-%d")
|
2024-09-08 10:28:54 +02:00
|
|
|
daily_forecast = lf.get_daily_stats(date_str)
|
|
|
|
mean_values = daily_forecast[0]
|
|
|
|
hours = [single_date + pd.Timedelta(hours=i) for i in range(24)]
|
2024-10-03 11:05:44 +02:00
|
|
|
daily_forecast_df = pd.DataFrame({"time": hours, "Last Pred": mean_values})
|
2024-09-08 10:28:54 +02:00
|
|
|
forecast_list.append(daily_forecast_df)
|
|
|
|
|
|
|
|
# Concatenate all daily forecasts into a single DataFrame
|
|
|
|
predicted_data = pd.concat(forecast_list, ignore_index=True)
|
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Create LoadPredictionAdjuster instance to adjust the predictions based on measured data
|
|
|
|
adjuster = LoadPredictionAdjuster(measured_data, predicted_data, lf)
|
|
|
|
adjuster.calculate_weighted_mean() # Calculate weighted mean for adjustment
|
|
|
|
adjuster.adjust_predictions() # Adjust predictions based on measured data
|
2024-10-03 11:05:44 +02:00
|
|
|
future_predictions = adjuster.predict_next_hours(
|
|
|
|
prediction_hours
|
|
|
|
) # Predict future load
|
2024-09-08 10:28:54 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Extract household power predictions
|
2024-10-03 11:05:44 +02:00
|
|
|
leistung_haushalt = future_predictions["Adjusted Pred"].values
|
2024-09-17 14:36:43 +02:00
|
|
|
gesamtlast = Gesamtlast(prediction_hours=prediction_hours)
|
2024-10-03 11:05:44 +02:00
|
|
|
gesamtlast.hinzufuegen(
|
|
|
|
"Haushalt", leistung_haushalt
|
|
|
|
) # Add household load to total load calculation
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-09-08 10:28:54 +02:00
|
|
|
# Calculate the total load
|
2024-09-20 11:09:54 +02:00
|
|
|
last = gesamtlast.gesamtlast_berechnen() # Compute total load
|
2024-09-08 10:28:54 +02:00
|
|
|
return jsonify(last.tolist())
|
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-08 10:28:54 +02:00
|
|
|
# @app.route('/gesamtlast', methods=['GET'])
|
|
|
|
# def flask_gesamtlast():
|
2024-09-20 11:09:54 +02:00
|
|
|
# if request.method == 'GET':
|
|
|
|
# year_energy = float(request.args.get("year_energy")) # Get annual energy value from query parameters
|
|
|
|
# prediction_hours = int(request.args.get("hours", 48)) # Default to 48 hours if not specified
|
|
|
|
# date_now = datetime.now() # Get the current date and time
|
|
|
|
# end_date = (date_now + timedelta(hours=prediction_hours)).strftime('%Y-%m-%d %H:%M:%S') # Calculate end date based on prediction hours
|
|
|
|
|
|
|
|
# ###############
|
|
|
|
# # Load Forecast
|
|
|
|
# ###############
|
|
|
|
# # Instantiate LastEstimator to retrieve measured data
|
|
|
|
# estimator = LastEstimator()
|
|
|
|
# start_date = (date_now - timedelta(days=60)).strftime('%Y-%m-%d') # Start date: last 60 days
|
|
|
|
# end_date = date_now.strftime('%Y-%m-%d') # Current date
|
|
|
|
|
|
|
|
# last_df = estimator.get_last(start_date, end_date) # Get last load data
|
|
|
|
|
|
|
|
# selected_columns = last_df[['timestamp', 'Last']] # Select relevant columns
|
|
|
|
# selected_columns['time'] = pd.to_datetime(selected_columns['timestamp']).dt.floor('H') # Floor timestamps to the nearest hour
|
|
|
|
# selected_columns['Last'] = pd.to_numeric(selected_columns['Last'], errors='coerce') # Convert 'Last' to numeric, coerce errors
|
|
|
|
# cleaned_data = selected_columns.dropna() # Clean data by dropping NaN values
|
|
|
|
|
|
|
|
# # Instantiate LoadForecast
|
|
|
|
# lf = LoadForecast(filepath=r'load_profiles.npz', year_energy=year_energy)
|
|
|
|
|
|
|
|
# # Generate forecast data
|
|
|
|
# forecast_list = [] # List to hold daily forecasts
|
|
|
|
# for single_date in pd.date_range(cleaned_data['time'].min().date(), cleaned_data['time'].max().date()): # Iterate over date range
|
|
|
|
# date_str = single_date.strftime('%Y-%m-%d') # Format date
|
|
|
|
# daily_forecast = lf.get_daily_stats(date_str) # Get daily stats from LoadForecast
|
|
|
|
# mean_values = daily_forecast[0] # Extract mean values
|
|
|
|
# hours = [single_date + pd.Timedelta(hours=i) for i in range(24)] # Generate hours for the day
|
|
|
|
# daily_forecast_df = pd.DataFrame({'time': hours, 'Last Pred': mean_values}) # Create DataFrame for daily forecast
|
|
|
|
# forecast_list.append(daily_forecast_df) # Append to the list
|
|
|
|
|
|
|
|
# forecast_df = pd.concat(forecast_list, ignore_index=True) # Concatenate all daily forecasts
|
|
|
|
|
|
|
|
# # Create LoadPredictionAdjuster instance
|
|
|
|
# adjuster = LoadPredictionAdjuster(cleaned_data, forecast_df, lf)
|
|
|
|
# adjuster.calculate_weighted_mean() # Calculate weighted mean for adjustments
|
|
|
|
# adjuster.adjust_predictions() # Adjust predictions based on measured data
|
|
|
|
|
|
|
|
# # Predict the next hours
|
|
|
|
# future_predictions = adjuster.predict_next_hours(prediction_hours) # Predict future load
|
|
|
|
|
|
|
|
# leistung_haushalt = future_predictions['Adjusted Pred'].values # Extract household power predictions
|
|
|
|
|
|
|
|
# gesamtlast = Gesamtlast(prediction_hours=prediction_hours) # Create Gesamtlast instance
|
|
|
|
# gesamtlast.hinzufuegen("Haushalt", leistung_haushalt) # Add household load to total load calculation
|
|
|
|
|
|
|
|
# # ###############
|
|
|
|
# # # WP (Heat Pump)
|
|
|
|
# # ##############
|
|
|
|
# # leistung_wp = wp.simulate_24h(temperature_forecast) # Simulate heat pump load for 24 hours
|
|
|
|
# # gesamtlast.hinzufuegen("Heatpump", leistung_wp) # Add heat pump load to total load calculation
|
|
|
|
|
|
|
|
# last = gesamtlast.gesamtlast_berechnen() # Calculate total load
|
|
|
|
# print(last) # Output total load
|
|
|
|
# return jsonify(last.tolist()) # Return total load as JSON
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
@app.route("/gesamtlast_simple", methods=["GET"])
|
2024-09-05 14:20:25 +02:00
|
|
|
def flask_gesamtlast_simple():
|
2024-10-03 11:05:44 +02:00
|
|
|
if request.method == "GET":
|
|
|
|
year_energy = float(
|
|
|
|
request.args.get("year_energy")
|
|
|
|
) # Get annual energy value from query parameters
|
|
|
|
date_now, date = get_start_enddate(
|
|
|
|
prediction_hours, startdate=datetime.now().date()
|
|
|
|
) # Get the current date and prediction end date
|
2024-09-20 11:09:54 +02:00
|
|
|
|
2024-09-05 14:16:43 +02:00
|
|
|
###############
|
|
|
|
# Load Forecast
|
|
|
|
###############
|
2024-10-03 11:05:44 +02:00
|
|
|
lf = LoadForecast(
|
|
|
|
filepath=r"load_profiles.npz", year_energy=year_energy
|
|
|
|
) # Instantiate LoadForecast with specified parameters
|
|
|
|
leistung_haushalt = lf.get_stats_for_date_range(date_now, date)[
|
|
|
|
0
|
|
|
|
] # Get expected household load for the date range
|
|
|
|
|
|
|
|
gesamtlast = Gesamtlast(
|
|
|
|
prediction_hours=prediction_hours
|
|
|
|
) # Create Gesamtlast instance
|
|
|
|
gesamtlast.hinzufuegen(
|
|
|
|
"Haushalt", leistung_haushalt
|
|
|
|
) # Add household load to total load calculation
|
2024-09-05 14:16:43 +02:00
|
|
|
|
|
|
|
# ###############
|
2024-09-20 11:09:54 +02:00
|
|
|
# # WP (Heat Pump)
|
2024-09-05 14:16:43 +02:00
|
|
|
# ##############
|
2024-09-20 11:09:54 +02:00
|
|
|
# leistung_wp = wp.simulate_24h(temperature_forecast) # Simulate heat pump load for 24 hours
|
|
|
|
# gesamtlast.hinzufuegen("Heatpump", leistung_wp) # Add heat pump load to total load calculation
|
|
|
|
|
|
|
|
last = gesamtlast.gesamtlast_berechnen() # Calculate total load
|
|
|
|
print(last) # Output total load
|
|
|
|
return jsonify(last.tolist()) # Return total load as JSON
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-08-01 13:47:46 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
@app.route("/pvforecast", methods=["GET"])
|
2024-07-30 14:01:18 +02:00
|
|
|
def flask_pvprognose():
|
2024-10-03 11:05:44 +02:00
|
|
|
if request.method == "GET":
|
2024-09-20 11:09:54 +02:00
|
|
|
# Retrieve URL and AC power measurement from query parameters
|
2024-07-30 14:01:18 +02:00
|
|
|
url = request.args.get("url")
|
|
|
|
ac_power_measurement = request.args.get("ac_power_measurement")
|
2024-10-03 11:05:44 +02:00
|
|
|
date_now, date = get_start_enddate(
|
|
|
|
prediction_hours, startdate=datetime.now().date()
|
|
|
|
)
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-07-30 14:01:18 +02:00
|
|
|
###############
|
|
|
|
# PV Forecast
|
|
|
|
###############
|
2024-10-03 11:05:44 +02:00
|
|
|
PVforecast = PVForecast(
|
|
|
|
prediction_hours=prediction_hours, url=url
|
|
|
|
) # Instantiate PVForecast with given parameters
|
|
|
|
if isfloat(
|
|
|
|
ac_power_measurement
|
|
|
|
): # Check if the AC power measurement is a valid float
|
|
|
|
PVforecast.update_ac_power_measurement(
|
|
|
|
date_time=datetime.now(),
|
|
|
|
ac_power_measurement=float(ac_power_measurement),
|
|
|
|
) # Update measurement
|
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Get PV forecast and temperature forecast for the specified date range
|
|
|
|
pv_forecast = PVforecast.get_pv_forecast_for_date_range(date_now, date)
|
|
|
|
temperature_forecast = PVforecast.get_temperature_for_date_range(date_now, date)
|
|
|
|
|
|
|
|
# Return both forecasts as a JSON response
|
2024-10-03 11:05:44 +02:00
|
|
|
ret = {
|
|
|
|
"temperature": temperature_forecast.tolist(),
|
|
|
|
"pvpower": pv_forecast.tolist(),
|
|
|
|
}
|
2024-07-30 14:01:18 +02:00
|
|
|
return jsonify(ret)
|
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
@app.route("/optimize", methods=["POST"])
|
2024-05-01 10:02:16 +02:00
|
|
|
def flask_optimize():
|
2024-10-03 11:05:44 +02:00
|
|
|
if request.method == "POST":
|
2024-09-30 08:37:48 +02:00
|
|
|
from datetime import datetime
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Retrieve optimization parameters from the request JSON
|
2024-03-28 08:15:17 +01:00
|
|
|
parameter = request.json
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Check for required parameters
|
2024-10-03 11:05:44 +02:00
|
|
|
required_parameters = [
|
|
|
|
"preis_euro_pro_wh_akku",
|
|
|
|
"strompreis_euro_pro_wh",
|
|
|
|
"gesamtlast",
|
|
|
|
"pv_akku_cap",
|
|
|
|
"einspeiseverguetung_euro_pro_wh",
|
|
|
|
"pv_forecast",
|
|
|
|
"temperature_forecast",
|
|
|
|
"eauto_min_soc",
|
|
|
|
"eauto_cap",
|
|
|
|
"eauto_charge_efficiency",
|
|
|
|
"eauto_charge_power",
|
|
|
|
"eauto_soc",
|
|
|
|
"pv_soc",
|
|
|
|
"start_solution",
|
|
|
|
"haushaltsgeraet_dauer",
|
|
|
|
"haushaltsgeraet_wh",
|
|
|
|
]
|
2024-09-20 11:09:54 +02:00
|
|
|
# Identify any missing parameters
|
|
|
|
missing_params = [p for p in required_parameters if p not in parameter]
|
|
|
|
if missing_params:
|
2024-10-03 11:05:44 +02:00
|
|
|
return jsonify(
|
|
|
|
{"error": f"Missing parameter: {', '.join(missing_params)}"}
|
|
|
|
), 400 # Return error for missing parameters
|
2024-09-17 14:36:43 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Perform optimization simulation
|
2024-10-03 11:05:44 +02:00
|
|
|
result = opt_class.optimierung_ems(
|
|
|
|
parameter=parameter, start_hour=datetime.now().hour
|
|
|
|
)
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
return jsonify(result) # Return optimization results as JSON
|
2024-05-01 10:02:16 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
@app.route("/visualisierungsergebnisse.pdf")
|
2024-03-31 13:00:01 +02:00
|
|
|
def get_pdf():
|
2024-09-20 11:09:54 +02:00
|
|
|
# Endpoint to serve the generated PDF with visualization results
|
2024-10-03 11:05:44 +02:00
|
|
|
return send_from_directory(
|
|
|
|
"", "visualisierungsergebnisse.pdf"
|
|
|
|
) # Adjust the directory if needed
|
|
|
|
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-09-10 21:57:57 +02:00
|
|
|
@app.route("/site-map")
|
|
|
|
def site_map():
|
2024-09-20 11:09:54 +02:00
|
|
|
# Function to generate a site map of valid routes in the application
|
2024-09-10 21:57:57 +02:00
|
|
|
def print_links(links):
|
|
|
|
content = "<h1>Valid routes</h1><ul>"
|
|
|
|
for link in links:
|
2024-09-20 11:09:54 +02:00
|
|
|
content += f"<li><a href='{link}'>{link}</a></li>"
|
|
|
|
content += "</ul>"
|
2024-09-10 21:57:57 +02:00
|
|
|
return content
|
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Check if the route has no empty parameters
|
2024-09-10 21:57:57 +02:00
|
|
|
def has_no_empty_params(rule):
|
|
|
|
defaults = rule.defaults if rule.defaults is not None else ()
|
|
|
|
arguments = rule.arguments if rule.arguments is not None else ()
|
|
|
|
return len(defaults) >= len(arguments)
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 11:09:54 +02:00
|
|
|
# Collect all valid GET routes without empty parameters
|
2024-09-10 21:57:57 +02:00
|
|
|
links = []
|
|
|
|
for rule in app.url_map.iter_rules():
|
|
|
|
if "GET" in rule.methods and has_no_empty_params(rule):
|
|
|
|
url = url_for(rule.endpoint, **(rule.defaults or {}))
|
|
|
|
links.append(url)
|
2024-10-03 11:05:44 +02:00
|
|
|
return print_links(sorted(links)) # Return the sorted links as HTML
|
2024-09-20 11:09:54 +02:00
|
|
|
|
2024-09-10 21:57:57 +02:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
@app.route("/")
|
2024-09-10 21:57:57 +02:00
|
|
|
def root():
|
2024-09-20 11:09:54 +02:00
|
|
|
# Redirect the root URL to the site map
|
2024-09-10 21:57:57 +02:00
|
|
|
return redirect("/site-map", code=302)
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-09-10 17:17:49 +02:00
|
|
|
try:
|
2024-09-20 11:09:54 +02:00
|
|
|
# Set host and port from environment variables or defaults
|
|
|
|
host = os.getenv("FLASK_RUN_HOST", "0.0.0.0")
|
2024-09-18 09:09:42 +02:00
|
|
|
port = os.getenv("FLASK_RUN_PORT", 8503)
|
2024-09-20 11:09:54 +02:00
|
|
|
app.run(debug=True, host=host, port=port) # Run the Flask application
|
|
|
|
except Exception as e:
|
2024-10-03 11:05:44 +02:00
|
|
|
print(
|
|
|
|
f"Could not bind to host {host}:{port}. Error: {e}"
|
|
|
|
) # Error handling for binding issues
|
2024-03-28 08:15:17 +01:00
|
|
|
|
2024-07-30 14:01:18 +02:00
|
|
|
# PV Forecast:
|
|
|
|
# object {
|
|
|
|
# pvpower: array[48]
|
|
|
|
# temperature: array[48]
|
2024-09-10 17:17:49 +02:00
|
|
|
# }
|