Weer erbij!
This commit is contained in:
79
app.py
79
app.py
@@ -1,58 +1,6 @@
|
||||
# from flask import Flask, jsonify
|
||||
# import mysql.connector
|
||||
|
||||
# app = Flask(__name__)
|
||||
|
||||
# def database_connect():
|
||||
# try:
|
||||
# connection = mysql.connector.connect(
|
||||
# host='localhost',
|
||||
# user='root',
|
||||
# password='',
|
||||
# database='goodgarden'
|
||||
# )
|
||||
# return connection
|
||||
# except Exception as e:
|
||||
# print("Database connection failed:", e)
|
||||
# return None
|
||||
|
||||
# # Function to get data from the MySQL database
|
||||
# def get_database_data():
|
||||
|
||||
# mydb = database_connect()
|
||||
|
||||
# if mydb and mydb.is_connected():
|
||||
|
||||
# cursor = mydb.cursor()
|
||||
|
||||
# # Query to retrieve the latest battery voltage data
|
||||
# query = "SELECT label, last_seen, last_battery_voltage, device_id FROM devices"
|
||||
# cursor.execute(query)
|
||||
# battery_data = cursor.fetchall()
|
||||
# mydb.close()
|
||||
# return battery_data
|
||||
|
||||
# @app.route('/', methods=['GET'])
|
||||
# def get_data():
|
||||
# battery_data_322 = get_database_data()
|
||||
|
||||
# if battery_data_322 is None:
|
||||
# return jsonify({"error": "Failed to fetch data from database"})
|
||||
|
||||
# data_dict = {
|
||||
# "label": battery_data_322[0],
|
||||
# "last_seen": battery_data_322[1],
|
||||
# "last_battery_voltage": battery_data_322[2],
|
||||
# "device_id": battery_data_322[3]
|
||||
# }
|
||||
|
||||
# return jsonify(data_dict)
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# app.run(host='127.0.0.1', port=5000)
|
||||
|
||||
from flask import Flask, jsonify
|
||||
import mysql.connector
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -99,5 +47,30 @@ def get_data():
|
||||
|
||||
return jsonify(battery_data) # Directly return the list of dictionaries as JSON
|
||||
|
||||
def get_weather_data():
|
||||
api_key = "05ddd06644"
|
||||
location = "Leiden"
|
||||
url = f"https://weerlive.nl/api/weerlive_api_v2.php?key={api_key}&locatie={location}"
|
||||
response = requests.get(url).json()
|
||||
return response
|
||||
|
||||
@app.route('/weather', methods=['GET'])
|
||||
def get_weather():
|
||||
weather_response = get_weather_data()
|
||||
|
||||
if 'error' in weather_response:
|
||||
return jsonify({"error": "Kon weerdata niet ophalen"})
|
||||
|
||||
live_weather = weather_response.get('liveweer', [])
|
||||
today_forecast = weather_response.get('verwachting_vandaag', [])
|
||||
|
||||
weather_data = {
|
||||
"live_weather": live_weather[0] if live_weather else {},
|
||||
"today_forecast": today_forecast[0] if today_forecast else {}
|
||||
}
|
||||
|
||||
return jsonify(weather_data)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="127.0.0.1", port=5000)
|
||||
|
||||
|
||||
@@ -156,3 +156,28 @@ function updateBatteryData(batteryData)
|
||||
}
|
||||
console.log(batteryData);
|
||||
}
|
||||
|
||||
const apiKey = "9516081f15727d063c9e2f08454a2fe9";
|
||||
const city = "Leiden";
|
||||
|
||||
// Maak de URL voor de API-aanroep
|
||||
const apiUrl = `https://weerlive.nl/api/weerlive_api_v2.php?key=${apiKey}&locatie=${city}`;
|
||||
|
||||
// Doe een GET-verzoek naar de API
|
||||
fetch(apiUrl)
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw new Error('Network response was not ok.');
|
||||
})
|
||||
.then(data => {
|
||||
// Verwerk de ontvangen weerdata
|
||||
console.log(data);
|
||||
const weatherIcon = data.live_weather.image;
|
||||
const weatherImageUrl = `https://www.weerlive.nl/delen.php?size=klein&weer=${weatherIcon}`;
|
||||
document.getElementById('weatherImage').src = weatherImageUrl;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('There was a problem with the fetch operation:', error);
|
||||
});
|
||||
Reference in New Issue
Block a user