From 4c82687640b3bbcdfd817970523cb7012968ee20 Mon Sep 17 00:00:00 2001 From: "peter.fong" Date: Fri, 7 Feb 2025 10:49:13 +0000 Subject: [PATCH] refacteree forecast --- WeatherForecast/cronfile | 2 +- WeatherForecast/main.py | 33 ++++---- WeatherForecast/script_getforecast.py | 106 -------------------------- energie/store_energy_usage.py | 7 +- 4 files changed, 23 insertions(+), 125 deletions(-) delete mode 100644 WeatherForecast/script_getforecast.py diff --git a/WeatherForecast/cronfile b/WeatherForecast/cronfile index 08c9a50..dfe2729 100644 --- a/WeatherForecast/cronfile +++ b/WeatherForecast/cronfile @@ -1 +1 @@ -0 13 * * * /usr/local/bin/python /app/script_getforecast.py >> /var/log/cron.log 2>&1 +0 13 * * * /usr/local/bin/python /app/main.py >> /var/log/cron.log 2>&1 diff --git a/WeatherForecast/main.py b/WeatherForecast/main.py index 3e5d2e1..f08d206 100644 --- a/WeatherForecast/main.py +++ b/WeatherForecast/main.py @@ -21,35 +21,35 @@ def initialize_app(): API_KEY = os.getenv('OPENWEATHER_API_KEY') LOCATION = os.getenv('LOCATION', 'Rijswijk') - db_config = { - 'host': os.getenv('DB_HOST'), - 'port': os.getenv('DB_PORT'), - 'user': os.getenv('DB_USER'), - 'password': os.getenv('DB_PASSWORD'), - 'database': os.getenv('DB_NAME') + MYSQL_config = { + 'host': os.getenv('MYSQL_HOST'), + 'port': os.getenv('MYSQL_PORT'), + 'user': os.getenv('MYSQL_USER'), + 'password': os.getenv('MYSQL_PASSWORD'), + 'database': os.getenv('MYSQL_NAME') } - return API_KEY, LOCATION, db_config, logger + return API_KEY, LOCATION, MYSQL_config, logger # Database functionaliteit -def handle_database_operations(db_config, logger): +def handle_database_operations(MYSQL_config, logger): try: - db_connection = create_db_connection(db_config, logger) + MYSQL_connection = create_db_connection(MYSQL_config, logger) except Exception as e: logger.error(f"Failed to connect to the database: {e}") raise - cursor = db_connection.cursor() + cursor = MYSQL_connection.cursor() try: create_table_if_not_exists(cursor, logger) except Exception as e: logger.error(f"Error creating table: {e}") - db_connection.close() + MYSQL_connection.close() raise - return db_connection, cursor + return MYSQL_connection, cursor # Weather functionaliteit @@ -95,10 +95,11 @@ def process_forecasts(data, cursor, logger, location): # Hoofdfunctie def main(): - API_KEY, LOCATION, db_config, logger = initialize_app() + API_KEY, LOCATION, MYSQL_config, logger = initialize_app() try: - db_connection, cursor = handle_database_operations(db_config, logger) + MYSQL_connection, cursor = handle_database_operations( + MYSQL_config, logger) except Exception as e: logger.error(f"Error during database operations: {e}") return @@ -117,11 +118,11 @@ def main(): return logger.info("Committing changes to the database...") - db_connection.commit() + MYSQL_connection.commit() logger.info("Changes committed successfully.") cursor.close() - db_connection.close() + MYSQL_connection.close() logger.info("Database connection closed.") diff --git a/WeatherForecast/script_getforecast.py b/WeatherForecast/script_getforecast.py deleted file mode 100644 index 3ee60ba..0000000 --- a/WeatherForecast/script_getforecast.py +++ /dev/null @@ -1,106 +0,0 @@ -import requests -import datetime -import mysql.connector -from dotenv import load_dotenv -import os - -# Laad de .env bestand -print("Loading environment variables from .env...") -load_dotenv() - -# Haal de API-sleutel uit de omgevingsvariabelen -api_key = os.getenv('OPENWEATHERMAP_API_KEY') -# Haal de MySQL-gegevens uit de omgevingsvariabelen -db_host = os.getenv('DB_HOST') -db_user = os.getenv('DB_USER') -db_password = os.getenv('DB_PASSWORD') -db_database = os.getenv('DB_DATABASE') -# Haal de locatie uit de omgevingsvariabelen -location = os.getenv('LOCATION') - -print(f"Using OpenWeatherMap API key: {api_key}") -print(f"Using location: {location}") - -# URL van de OpenWeatherMap API voor de forecast -url = f'http://api.openweathermap.org/data/2.5/forecast?q={location}&units=metric&cnt=7&appid={api_key}' - -print(f"Fetching weather forecast from OpenWeatherMap for {location}...") - -# Haal de data op -response = requests.get(url) -if response.status_code == 200: - print("Data successfully fetched from OpenWeatherMap API.") - data = response.json() -else: - print(f"Error fetching data: {response.status_code}") - data = {} - -# Verbinding maken met MySQL-database -print("Connecting to MySQL database...") -db_connection = mysql.connector.connect( - host=db_host, - user=db_user, - password=db_password, - database=db_database -) -cursor = db_connection.cursor() - -# Maak de tabel aan als deze nog niet bestaat -print("Ensuring the forecast_data table exists...") -create_table_query = """ -CREATE TABLE IF NOT EXISTS forecast_data ( - id INT AUTO_INCREMENT PRIMARY KEY, - utc_datetime DATETIME UNIQUE, - temperature FLOAT, - weather_description VARCHAR(255), - wind_speed FLOAT, - rain FLOAT -); -""" -cursor.execute(create_table_query) -print("Table `forecast_data` is ready.") - -# Toon de forecast en sla deze op in de database -print(f"Processing forecast data for {len(data.get('list', []))} entries...") - -for entry in data.get('list', []): - # Converteer de tijd naar UTC - utc_dt = datetime.datetime.utcfromtimestamp(entry['dt']) - temp = entry['main']['temp'] - weather = entry['weather'][0]['description'] - wind_speed = entry['wind']['speed'] - # Regensomstandigheden (indien aanwezig) - rain = entry.get('rain', {}).get('3h', 0) - - # Print de data - print(f"\nProcessing data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')} UTC:") - print(f"Temperature: {temp}°C") - print(f"Weather: {weather.capitalize()}") - print(f"Wind Speed: {wind_speed} m/s") - print(f"Rain: {rain} mm") - - # Voeg de data toe aan de database of update als het record al bestaat - query = """ - INSERT INTO forecast_data (utc_datetime, temperature, weather_description, wind_speed, rain) - VALUES (%s, %s, %s, %s, %s) - ON DUPLICATE KEY UPDATE - temperature = VALUES(temperature), - weather_description = VALUES(weather_description), - wind_speed = VALUES(wind_speed), - rain = VALUES(rain); - """ - - print( - f"Executing query to insert/update data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')}...") - cursor.execute(query, (utc_dt, temp, weather, wind_speed, rain)) - print( - f"Data for {utc_dt.strftime('%Y-%m-%d %H:%M:%S')} successfully inserted/updated.") - -# Commit de veranderingen en sluit de verbinding -print("Committing changes to the database...") -db_connection.commit() -print("Changes committed successfully.") - -cursor.close() -db_connection.close() -print("Database connection closed.") diff --git a/energie/store_energy_usage.py b/energie/store_energy_usage.py index 41387ed..46dc269 100644 --- a/energie/store_energy_usage.py +++ b/energie/store_energy_usage.py @@ -4,9 +4,12 @@ import os from datetime import datetime from dotenv import load_dotenv -# Laad configuratie uit .env bestand -load_dotenv() +# Geef het absolute pad op naar je .env bestand +env_path = "./.env" # Pas dit pad aan! + +# Laad de .env bestand expliciet +load_dotenv(env_path) # Configuratievariabelen uit .env bestand ENERGY_USAGE_URL = os.getenv("ENERGY_USAGE_URL")