diff --git a/dwd_global_rad_api_server/app.py b/dwd_global_rad_api_server/app.py index edb871e..d902abd 100644 --- a/dwd_global_rad_api_server/app.py +++ b/dwd_global_rad_api_server/app.py @@ -5,9 +5,12 @@ import xarray as xr import main import logging +import dwd_global_radiation as dgr app = Flask(__name__) +# Instantiate main object +objGlobalRadiation = dgr.GlobalRadiation() # Configure logging logging.basicConfig(level=logging.DEBUG) @@ -35,7 +38,67 @@ def process(): except Exception as e: app.logger.error(f"Error processing request: {e}") return jsonify({'error': str(e)}), 500 +# Instantiate main object +objGlobalRadiation = dgr.GlobalRadiation() +@app.route('/locations', methods=['GET']) +def get_locations(): + locations = objGlobalRadiation.locations + if not locations: + return jsonify({'error': 'No locations found'}), 404 + + serializable_locations = [loc.to_dict() for loc in locations] + print(json.dumps(serializable_locations, indent=2)) # Pretty-print the JSON data + return jsonify(serializable_locations) + +@app.route('/locations', methods=['POST']) +def add_location(): + data = request.json + name = data['name'] + latitude = data['latitude'] + longitude = data['longitude'] + try: + # Call the add_location method from the GlobalRadiation class + objGlobalRadiation.add_location(name=name, latitude=latitude, longitude=longitude) + return jsonify({'status': 'Location added successfully'}), 200 + except ValueError as e: + # Handle the exception and return a 400 Bad Request response + return jsonify({'error': str(e)}), 400 + +@app.route('/locations/', methods=['GET']) +def get_location_by_name(name): + """Fetch a specific location by name.""" + location = objGlobalRadiation.get_location_by_name(name) + if location is None: + return jsonify({'error': 'Location not found'}), 404 + + return jsonify(location.to_dict()) + +@app.route('/forecasts', methods=['GET']) +def fetch_forecasts(): + """Check if forecast data is available.""" + # Fetch the forecast data from the DWD servers + objGlobalRadiation.fetch_forecasts() + + forecast_data = objGlobalRadiation.forecast_data + if forecast_data is None: + return jsonify({'error': 'Forecast data not found'}), 404 + + return jsonify({'message': 'Forecast data is available'}), 200 + +@app.route('/measurements', methods=['GET']) +def fetch_measurements(): + """Fetch the measurement data for a specific location by name.""" + + # Get the 'hours' query parameter from the request, default to 3 if not provided + hours = request.args.get('hours', default=3, type=int) + # Fetch the measurement data from the DWD servers + objGlobalRadiation.fetch_measurements(max_hour_age_of_measurement=hours) + + measurement_data = objGlobalRadiation.measurement_data + if measurement_data is None: + return jsonify({'error': 'Forecast data not found'}), 404 + + return jsonify({'message': 'Measurement data is available'}), 200 if __name__ == '__main__': - app.logger.debug("Starting Flask app") app.run(host='0.0.0.0', port=5001) diff --git a/dwd_global_rad_api_server/config.yaml b/dwd_global_rad_api_server/config.yaml index bf99efd..2642f00 100644 --- a/dwd_global_rad_api_server/config.yaml +++ b/dwd_global_rad_api_server/config.yaml @@ -1,14 +1,12 @@ # https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config name: DWD Global Rad Api Server -version: "1.4.4" +version: "1.4.5" slug: dwd_global_rad_api_server description: DWD Global Rad Api Server arch: - - armhf - armv7 - aarch64 - amd64 - - i386 image: "aschmere/dwd_global_rad_api_server-{arch}" init: false ports: