Skip to content

Commit

Permalink
Bump version of DWD Global Radiation API Server to 1.4.5
Browse files Browse the repository at this point in the history
- Update version number to 1.4.5.
- Add comprehensive support for REST API in a first draft version.
  • Loading branch information
aschmere committed Jun 12, 2024
1 parent 50ba6bb commit 939b26a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
65 changes: 64 additions & 1 deletion dwd_global_rad_api_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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/<name>', 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)
4 changes: 1 addition & 3 deletions dwd_global_rad_api_server/config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 939b26a

Please sign in to comment.