# Import other functions and classes import heating import errors import sql import weather import time from flask import Flask, render_template, redirect, url_for from threading import Thread app = Flask(__name__) # Paramaterised location of database file # Use of constant DBFILE = "data.db" db = sql.db(DBFILE) weather = weather.weather_met_no() # Check if running on correct device : defensive coding against running # on the wrong device try: with open("/sys/firmware/devicetree/base/model", "r") as file: device = file.read() except: errors.device_error() if device.startswith("Raspberry Pi 4"): boiler = heating.rpi_heating(db) else: errors.device_error() # Import API Script after Flask app and boiler classexists import api def main_page(): # paramaterised location of template in 'templates' folder return render_template( "main.html", # Rounding value for user interface so number is not too long actual_temp=round(boiler.temperature, 2), target_temp=boiler.target, forecast_temp=weather.temperature(), ) @app.route("/") @app.route("/index") @app.route("/index.html") def index(): return main_page() @app.route("/up") def form(): boiler.up() return redirect(url_for("index")) @app.route("/down") def activity(): boiler.down() return redirect(url_for("index")) @app.route("/export") def export_choice(): return render_template("export_choice.html") @app.route("/export/") def export(opt): # Defensive handling of input if opt in ["temperature", "weather", "history", "schedule"]: data = db.exec("select * from {}".format(opt)) o = "" for i in data: o += str(a) + "\n" return o else: return render_template( "error.html", error="Invalid export type" ) # Methods that will run continously: the update script for the heating # and the flask webapp def updater(): while True: boiler.update() time.sleep(5) def webapp(): app.run(host="0.0.0.0") if __name__ == "__main__": # Start the two main methods as threads # Use of multithreaded processing t1 = Thread(target=webapp) t2 = Thread(target=updater) t1.start() t2.start()