Einlesen und Anzeigen getrennt
Einlesen per HTPP (nicht mehr MQTT)
This commit is contained in:
122
wetterstation_ingestion.py
Normal file
122
wetterstation_ingestion.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Wetterstation Ingestion Service - HTTP-POST Datenempfang
|
||||
Empfängt Wetterdaten via POST und speichert sie in der Datenbank
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
from flask import Flask, jsonify, request
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Lade Umgebungsvariablen aus .env Datei
|
||||
load_dotenv()
|
||||
|
||||
# Konfiguration aus Umgebungsvariablen
|
||||
DB_FILE = os.getenv("DB_FILE", "wetterdaten.db")
|
||||
HTTP_PORT = int(os.getenv("INGESTION_PORT", 5004))
|
||||
|
||||
app = Flask(__name__)
|
||||
app.url_map.strict_slashes = False
|
||||
|
||||
|
||||
class WetterDB:
|
||||
"""Klasse für Datenbankoperationen"""
|
||||
|
||||
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')}")
|
||||
|
||||
|
||||
# Globale Datenbankinstanz
|
||||
db = WetterDB(DB_FILE)
|
||||
|
||||
|
||||
# Flask Routes
|
||||
@app.route('/health')
|
||||
def health():
|
||||
"""Health-Check Endpoint"""
|
||||
return jsonify({'status': 'ok', 'service': 'ingestion'}), 200
|
||||
|
||||
|
||||
@app.route('/api/data/upload', methods=['POST'])
|
||||
@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
|
||||
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
|
||||
|
||||
|
||||
def main():
|
||||
"""Hauptprogramm"""
|
||||
print("Wetterstation Ingestion Service wird gestartet...")
|
||||
print(f"\nHTTP-POST Endpoint: http://0.0.0.0:{HTTP_PORT}/api/data/upload")
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user