trend/trend.py

82 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-09-06 12:09:11 +02:00
#!/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("<html>")
print("<head>")
print("<title>Temperaturverlauf</title>")
print("</head>")
print("<body>")
print("<h1>Temperaturverlauf Tank 1</h1>")
print("<form method='POST'>")
print("Startdatum und Uhrzeit: <input type='datetime-local' name='start_datetime' value='{}'><br>".format(start_datetime))
print("Enddatum und Uhrzeit: <input type='datetime-local' name='end_datetime' value='{}'><br>".format(end_datetime))
print("<input type='submit' value='Anzeigen'>")
print("</form>")
print(temperature_plot)
print("<h2>Dargestellte Werte als CSV</h2>")
print("<pre>")
print(csv_data)
print("</pre>")
print("</body>")
print("</html>")
# Close the database connection
conn.close()