#!/usr/bin/env python3 import cgi import sqlite3 import mpld3 import matplotlib.pyplot as plt from datetime import datetime, timedelta import csv from io import StringIO # Parse the form data form = cgi.FieldStorage() # Get the current date and time current_datetime = datetime.now() # Calculate one day before and after the current date at 22:22 start_datetime_default = current_datetime - timedelta(days=1, hours=current_datetime.hour, minutes=current_datetime.minute, seconds=current_datetime.second) start_datetime_default = start_datetime_default.replace(hour=22, minute=22, second=0) end_datetime_default = current_datetime + timedelta(days=1, hours=1, minutes=38) # Assuming you want 24 hours + 1 hour and 38 minutes end_datetime_default = end_datetime_default.replace(hour=22, minute=22, second=0) # Get the selected start and end dates with times from the form, or use the defaults start_datetime = form.getvalue("start_datetime", start_datetime_default.strftime("%Y-%m-%dT%H:%M")) end_datetime = form.getvalue("end_datetime", end_datetime_default.strftime("%Y-%m-%dT%H:%M")) # Establish a connection to the database (please adjust the database path) conn = sqlite3.connect('/home/divers/datenbank/temperaturen.db') cursor = conn.cursor() # Create a function to generate the temperature plot and CSV data def generate_temperature_plot_and_csv(start_datetime, end_datetime): cursor.execute("SELECT zeitstempel, temperatur FROM istwert WHERE messstelle = 1 AND zeitstempel BETWEEN ? AND ?", (start_datetime, end_datetime)) rows = cursor.fetchall() timestamps = [datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") for row in rows] temperaturen = [row[1] for row in rows] # Generate CSV data csv_data = "Zeitstempel,Temperatur (°C)\n" csv_data += "\n".join([f"{timestamps[i]},{temperaturen[i]}" for i in range(len(timestamps))]) plt.figure(figsize=(10, 5)) plt.plot(timestamps, temperaturen, marker='o', linestyle='-', color='b') plt.xlabel('Zeitstempel') plt.ylabel('Temperatur (°C)') plt.title('Temperaturverlauf Tank 1') plt.grid(True) plot_html = mpld3.fig_to_html(plt.gcf()) plt.close() return plot_html, csv_data # Generate the temperature plot and CSV data temperature_plot, csv_data = generate_temperature_plot_and_csv(start_datetime, end_datetime) # Generate the HTML page with the temperature plot and CSV data print("Content-type: text/html\n") print("") print("
") print("") print(csv_data) print("") print("") print("") # Close the database connection conn.close()