From df7e05fea4a14d6749eb476ae4e66b4f2a771a19 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 5 Feb 2022 18:40:27 +0000 Subject: Working on a few last features. --- dictionary.py | 14 ++++++++++++++ main.py | 38 ++++++++++++++++++++++++++++++++++---- sql.py | 7 ++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/dictionary.py b/dictionary.py index 32c4e73..9fe1bb4 100644 --- a/dictionary.py +++ b/dictionary.py @@ -33,6 +33,20 @@ class dictionary: self.data.append([key, value]) self.size += 1 + # increment a value + def inc(self, key, value=1): + # defensive programming + in_data = False + i = 0 + # high efficiency loop stops after found + while i < self.size and in_data == False: + if self.data[i][0] == key: + self.data[i][1] += value + in_data = True + i += 1 + if not in_data: + self.add(key, value) + # Sort dictionary by value def sort_by_value(self): self.data = sort_v(self.data, self.size, 1) diff --git a/main.py b/main.py index 7559888..2fdb0f9 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import sql import weather import time import sqlite3 +import dates from flask import Flask, render_template, redirect, url_for from threading import Thread @@ -12,16 +13,24 @@ from threading import Thread app = Flask(__name__) +# TODO: +# schedule +# export as pdf (dictionary/merge sort) + +# Use of constants # Paramaterised location of database file -# Use of constant 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 +# on the wrong device, the instantiate heating class try: with open("/sys/firmware/devicetree/base/model", "r") as file: device = file.read() @@ -87,6 +96,23 @@ def export(opt): 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" @@ -100,7 +126,11 @@ def export(opt): def updater(): while True: boiler.update() - time.sleep(5) + db.exec( + "insert into weather values (?,?,?,?)", + (date.day(), date.time(), weather.temperature()), + ) + time.sleep(TIME_INCREMENT) def webapp(): diff --git a/sql.py b/sql.py index 38d40e1..7276ff9 100644 --- a/sql.py +++ b/sql.py @@ -47,10 +47,11 @@ class db: """ CREATE TABLE weather ( date text not null, + time integer not null, temperature real, - wind real, - PRIMARY KEY (date), - FOREIGN KEY (date) REFERENCES temperature(date) + PRIMARY KEY (date, times), + FOREIGN KEY (date) REFERENCES temperature(date), + FOREIGN KEY (time) REFERENCES temperature(time) ); """ ) -- cgit v1.2.3