mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-04-19 08:55:15 +00:00
Update server_load_profile.py
- general clean up - constants pulled to the top - improved error handeling
This commit is contained in:
parent
1f01455825
commit
ec996fe410
@ -6,44 +6,53 @@ from pprint import pprint
|
|||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
DATE_FORMAT = '%Y-%m-%d'
|
||||||
|
EXPECTED_ARRAY_SHAPE = (2, 24)
|
||||||
|
FILEPATH = r'.\load_profiles.npz'
|
||||||
|
|
||||||
|
def get_load_forecast(year_energy):
|
||||||
|
"""Initialize LoadForecast with the given year_energy."""
|
||||||
|
return cl.LoadForecast(filepath=FILEPATH, year_energy=float(year_energy))
|
||||||
|
|
||||||
|
def validate_date(date_str):
|
||||||
|
"""Validate the date string and return a datetime object."""
|
||||||
|
try:
|
||||||
|
return datetime.strptime(date_str, DATE_FORMAT)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError("Date is not in the correct format. Expected format: YYYY-MM-DD.")
|
||||||
|
|
||||||
@app.route('/getdata', methods=['GET'])
|
@app.route('/getdata', methods=['GET'])
|
||||||
def get_data():
|
def get_data():
|
||||||
# Hole das Datum aus den Query-Parametern
|
# Retrieve the date and year_energy from query parameters
|
||||||
date_str = request.args.get('date')
|
date_str = request.args.get('date')
|
||||||
year_energy = request.args.get('year_energy')
|
year_energy = request.args.get('year_energy')
|
||||||
|
|
||||||
try:
|
|
||||||
# Konvertiere das Datum in ein datetime-Objekt
|
|
||||||
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
|
|
||||||
filepath = r'.\load_profiles.npz' # Pfad zur JSON-Datei anpassen
|
|
||||||
lf = cl.LoadForecast(filepath=filepath, year_energy=float(year_energy))
|
|
||||||
specific_date_prices = lf.get_daily_stats('2024-02-16')
|
|
||||||
|
|
||||||
|
if not date_str or not year_energy:
|
||||||
# Berechne den Tag des Jahres
|
return jsonify({"error": "Missing 'date' or 'year_energy' query parameter."}), 400
|
||||||
#day_of_year = date_obj.timetuple().tm_yday
|
|
||||||
|
try:
|
||||||
# Konvertiere den Tag des Jahres in einen String, falls die Schlüssel als Strings gespeichert sind
|
# Validate and convert the date
|
||||||
#day_key = int(day_of_year)
|
date_obj = validate_date(date_str)
|
||||||
#print(day_key)
|
lf = get_load_forecast(year_energy)
|
||||||
# Überprüfe, ob der Tag im Jahr in den Daten vorhanden ist
|
|
||||||
|
# Get daily statistics for the requested date
|
||||||
array_list = lf.get_daily_stats(date_str)
|
array_list = lf.get_daily_stats(date_str)
|
||||||
pprint(array_list)
|
pprint(array_list)
|
||||||
pprint(array_list.shape)
|
pprint(array_list.shape)
|
||||||
if array_list.shape == (2,24):
|
|
||||||
#if day_key < len(load_profiles_exp):
|
# Check if the shape of the array is valid
|
||||||
# Konvertiere das Array in eine Liste für die JSON-Antwort
|
if array_list.shape == EXPECTED_ARRAY_SHAPE:
|
||||||
#((load_profiles_exp_l[day_key]).tolist(),(load_profiles_std_l)[day_key].tolist())
|
|
||||||
|
|
||||||
return jsonify({date_str: array_list.tolist()})
|
return jsonify({date_str: array_list.tolist()})
|
||||||
else:
|
else:
|
||||||
return jsonify({"error": "Datum nicht gefunden"}), 404
|
return jsonify({"error": "Data not found for the given date."}), 404
|
||||||
except ValueError:
|
|
||||||
# Wenn das Datum nicht im richtigen Format ist oder ungültig ist
|
except ValueError as e:
|
||||||
return jsonify({"error": "Ungültiges Datum"}), 400
|
# Return a descriptive error message for date validation issues
|
||||||
|
return jsonify({"error": str(e)}), 400
|
||||||
|
except Exception:
|
||||||
|
# Return a generic error message for unexpected errors
|
||||||
|
return jsonify({"error": "An unexpected error occurred."}), 500
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user