summaryrefslogtreecommitdiff
path: root/main.py
blob: 2fdb0f953f69780012eb6a82875d0d1ec6e3f842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Import other functions and classes
import heating
import errors
import sql
import weather
import time
import sqlite3
import dates

from flask import Flask, render_template, redirect, url_for
from threading import Thread

app = Flask(__name__)


# TODO:
# schedule
# export as pdf (dictionary/merge sort)

# Use of constants
# Paramaterised location of database file
DBFILE = "data.db"
TIME_INCREMENT = 5
# number of increments per day
STEPS = (60 * 60 * 24) / TIME_INCREMENT

# instantiate classes
db = sql.db(DBFILE)
date = date.dates()
weather = weather.weather_met_no()

# Check if running on correct device : defensive coding against running
#   on the wrong device, the instantiate heating class
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/<opt>")
def export(opt):
    # Defensive handling of input
    if opt in ["temperature", "weather", "history", "schedule"]:
        con = sqlite3.connect(DBFILE)
        cur = con.cursor()
        data = cur.execute("select * from {}".format(opt))
        o = ""
        for i in data:
            o += str(i) + "\n"
        con.close()
        # writing data to a file
        with open("static/export.txt", "w") as file:
            file.write(o)
        return redirect("/static/export.txt")
    elif opt == "pdf":
        con = sqlite3.connect(DBFILE)
        cur = con.cursor()
        data = cur.execute("select date, on from history")
        on = 0
        total = 0
        days = dictionary.dictionary()
        for i in data:
            if data[1] == 1:
                days.inc(data[0], 1 / STEPS * 100)
        days.sort_by_value()
        con.close()
        # writing data to a file
        # with open("static/export.txt", "w") as file:
        #    file.write(o)
        # return redirect("/static/export.txt")
        return True
    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()
        db.exec(
            "insert into weather values (?,?,?,?)",
            (date.day(), date.time(), weather.temperature()),
        )
        time.sleep(TIME_INCREMENT)


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()