Add temperature course image

This commit is contained in:
Thomas Klaehn 2022-03-30 14:56:24 +02:00
parent 055f632c96
commit 85d8ba58b4
3 changed files with 78 additions and 4 deletions

View File

@ -1,6 +1,9 @@
"""Flask application for greenhouse"""
from datetime import datetime, timedelta
import json
import logging
import os
import sys
import xmlrpc.client
from flask import Flask
@ -9,6 +12,11 @@ from flask import make_response
from flask import request
from flask import jsonify
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
LOG_LEVEL = logging.INFO
LOG_FILE = "/var/log/greenhouseui.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
@ -17,16 +25,78 @@ HOST = "localhost"
PORT = 64001
URL = f"http://{HOST}:{PORT}"
# logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE)
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE)
# logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
log = logging.getLogger()
def get_image_install_path():
"""Get the path where to install dynamically created images to"""
for path in sys.path:
try:
content = os.listdir(path)
if "greenhouseui" in content:
return os.path.join(path, "greenhouseui/static")
except FileNotFoundError:
pass
return None
def draw_temperature_image():
temperature_dates = []
temperatures = []
with open("/var/log/gardencontrol.log", "r", encoding="UTF-8") as logfile:
for line in logfile:
line = line.strip()
if "Temperature" in line:
chunks = line.split(" ")
logtime = chunks[0] + " " + chunks[1]
logtime = datetime.strptime(logtime, "%Y-%m-%d %H:%M:%S,%f")
now = datetime.now()
start_time = now - timedelta(hours=24)
if logtime >= start_time:
temperature_dates.append(logtime)
temperature = float(chunks[4])
temperatures.append(temperature)
background_color = "#282929"
color = "#b6b6b6"
fig, ax = plt.subplots(1)
fig.set_facecolor(background_color)
ax.set_facecolor(background_color)
ax.tick_params(axis="x", colors = color)
ax.tick_params(axis="y", colors = color)
ax.spines["top"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
# ax.set_ylabel("°C", rotation=0)
ax.yaxis.label.set_color(color)
fig.autofmt_xdate()
plt.plot(temperature_dates, temperatures, color=color)
xfmt = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)
path = get_image_install_path()
if path:
img_file = os.path.join(path, "greenhouse_temperature.png")
if os.path.exists(img_file):
os.remove(img_file)
plt.savefig(img_file)
plt.close("all")
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
"""Handle GET to index.html"""
draw_temperature_image()
return render_template('index.html')
@app.route('/sample/<idx>', methods=['GET'])

View File

@ -14,8 +14,12 @@
<tr>
<td><h3>Temperatur </h3></td>
<td><h3 id="temperature_value"></h3></td>
<td><h3> °C</h3></td>
</tr>
</table>
<div>
<img src="{{url_for('static', filename='greenhouse_temperature.png')}}"/>
</div>
<table class="center">
<tr>
<td class="table_left">Heizung</td>

View File

@ -6,11 +6,11 @@ from setuptools import setup
from setuptools.command.install import install
NAME = 'Greenhouseui'
VERSION = '1'
VERSION = '2'
AUTHOR = 'Thomas Klaehn'
EMAIL = 'tkl@blackfinn.de'
PACKAGES = ['greenhouseui']
REQUIRES = ['Flask', 'gunicorn']
REQUIRES = ['Flask', 'gunicorn', 'matplotlib']
PACKAGE_DATA = {'greenhouseui': ['templates/*', 'static/css/*', 'static/scripts/*']}
SERVICEDIR = "/lib/systemd/system"