Einlesen und Anzeigen getrennt
Einlesen per HTPP (nicht mehr MQTT)
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Wetterstation - HTTP-POST Datenempfang und Web-Visualisierung
|
||||
Wetterstation Web-Interface - Visualisierung und API
|
||||
Stellt das Web-Interface und Lese-APIs für historische Daten bereit
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from flask import Flask, render_template, jsonify, request
|
||||
from flask import Flask, render_template, jsonify
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Lade Umgebungsvariablen aus .env Datei
|
||||
@@ -21,58 +22,10 @@ app = Flask(__name__)
|
||||
|
||||
|
||||
class WetterDB:
|
||||
"""Klasse für Datenbankoperationen"""
|
||||
"""Klasse für Datenbankoperationen (nur Lesezugriff)"""
|
||||
|
||||
def __init__(self, db_file):
|
||||
self.db_file = db_file
|
||||
self.init_db()
|
||||
|
||||
def init_db(self):
|
||||
"""Datenbank initialisieren"""
|
||||
conn = sqlite3.connect(self.db_file)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS wetterdaten (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
dateTime TEXT NOT NULL,
|
||||
barometer REAL,
|
||||
outTemp REAL,
|
||||
outHumidity INTEGER,
|
||||
windSpeed REAL,
|
||||
windDir REAL,
|
||||
windGust REAL,
|
||||
rainRate REAL,
|
||||
rain REAL
|
||||
)
|
||||
''')
|
||||
cursor.execute('''
|
||||
CREATE INDEX IF NOT EXISTS idx_dateTime ON wetterdaten(dateTime)
|
||||
''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def save_data(self, data):
|
||||
"""Wetterdaten speichern"""
|
||||
conn = sqlite3.connect(self.db_file)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO wetterdaten
|
||||
(dateTime, barometer, outTemp, outHumidity, windSpeed, windDir, windGust, rainRate, rain)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
data.get('dateTime'),
|
||||
data.get('barometer'),
|
||||
data.get('outTemp'),
|
||||
data.get('outHumidity'),
|
||||
data.get('windSpeed'),
|
||||
data.get('windDir'),
|
||||
data.get('windGust'),
|
||||
data.get('rainRate'),
|
||||
data.get('rain')
|
||||
))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(f"Daten gespeichert: {data.get('dateTime')}")
|
||||
|
||||
def get_data(self, hours=24):
|
||||
"""Daten der letzten X Stunden abrufen"""
|
||||
@@ -127,29 +80,10 @@ def index():
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/api/data/upload', methods=['POST'])
|
||||
def upload_data():
|
||||
"""HTTP-POST Endpoint für Wetterdaten"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
if not data:
|
||||
return jsonify({'error': 'Keine Daten empfangen'}), 400
|
||||
|
||||
# Daten speichern (unverändert)
|
||||
db.save_data(data)
|
||||
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'message': 'Daten empfangen und gespeichert'
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Verarbeiten der POST-Anfrage: {e}")
|
||||
return jsonify({'error': str(e)}), 400
|
||||
|
||||
|
||||
|
||||
@app.route('/health')
|
||||
def health():
|
||||
"""Health-Check Endpoint"""
|
||||
return jsonify({'status': 'ok', 'service': 'web'}), 200
|
||||
|
||||
|
||||
@app.route('/api/data/<period>')
|
||||
@@ -167,9 +101,12 @@ def get_historical_data(period):
|
||||
|
||||
def main():
|
||||
"""Hauptprogramm"""
|
||||
print("Wetterstation wird gestartet...")
|
||||
print("\nWeb-Interface verfügbar unter: http://localhost:5003")
|
||||
print("HTTP-POST Endpoint: http://localhost:5003/api/data/upload")
|
||||
print("Wetterstation Web-Interface wird gestartet...")
|
||||
print(f"\nWeb-Interface verfügbar unter: http://0.0.0.0:{HTTP_PORT}")
|
||||
print(f"API Endpoints:")
|
||||
print(f" - http://0.0.0.0:{HTTP_PORT}/api/data/day")
|
||||
print(f" - http://0.0.0.0:{HTTP_PORT}/api/data/week")
|
||||
print(f"Health-Check: http://0.0.0.0:{HTTP_PORT}/health")
|
||||
print("Drücke CTRL+C zum Beenden\n")
|
||||
app.run(host='0.0.0.0', port=HTTP_PORT, debug=False)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user