diff --git a/greenhouseui/app.py b/greenhouseui/app.py index feb64bd..3a763d2 100644 --- a/greenhouseui/app.py +++ b/greenhouseui/app.py @@ -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/', methods=['GET']) diff --git a/greenhouseui/templates/index.html b/greenhouseui/templates/index.html index 8dc2e98..23d77ee 100644 --- a/greenhouseui/templates/index.html +++ b/greenhouseui/templates/index.html @@ -14,8 +14,12 @@

Temperatur

+

°C

+
+ +
diff --git a/setup.py b/setup.py index c3aa44a..38ed798 100644 --- a/setup.py +++ b/setup.py @@ -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"
Heizung