diff --git a/src/main.py b/src/main.py index a745c16..51211d1 100644 --- a/src/main.py +++ b/src/main.py @@ -2,9 +2,14 @@ from requests import get from bs4 import BeautifulSoup from chardet import detect import csv -from json import dumps +from json import dumps, load +from flask import Flask, request, jsonify, render_template +from datetime import datetime +import threading +import logging ROOT = "http://192.168.2.20" +app = Flask(__name__) # Turns a csv reader into a list of dictionaries(json) def reader_to_json(reader:csv.DictReader) -> list[dict]: @@ -43,9 +48,10 @@ def get_file(entries:int) -> dict: raise ValueError("No link found") - # DEBUG: Get file locally - # return local_file("test.csv") - + # DEBUG: Local json + # with open("out.json") as i: + # return load(i) + # Saves a json file def save_file(json:list[dict], filename:str) -> None: try: @@ -54,11 +60,44 @@ def save_file(json:list[dict], filename:str) -> None: except Exception as exc: raise IndexError("Error saving file") from exc -def main(): - size = int(input("Entries: ")) - file = get_file(size) +@app.route("/", methods=["GET"]) +def home(): + return render_template("index.html", entries=get_entries(10).json, date=datetime.now()) + +@app.route("/get", methods=["GET"]) +def get_entries(entries:int=None): + if not entries: + entries = request.args.get("entries") + try: + entries = int(entries) + except: + return jsonify({"error":"Invalid entries"}) + try: + file = get_file(entries) + return jsonify(file) + except Exception as exc: + return jsonify({"error":str(exc)}) + +@app.route("/dump", methods=["GET"]) +def dump(): + with open("out.json") as i: + return i.read() + +@app.route("/dumpLast", methods=["GET"]) +def dump_last(): + with open("out.json") as i: + # Return last 50 entries + return dumps(load(i)[-50:]) - save_file(file, "out.json") + +def pull_data(): + logging.info("Pulling data") + threading.Timer(86400, pull_data).start() + save_file(get_file(50), "out.json") + + if __name__ == "__main__": - main() + # Add threading + pull_data() + app.run(debug=True) \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..547b486 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,397 @@ + + + + + + + + Water Monitor + + + + + + +
+
+ {% for item in entries %} +
+
+
+ {{ item.Record }} +
+
+ {{ item.Date }} +
+
+ +
+ + UTC Time: {{ item['UTC Time'] }} +
+
+ + PT1: {{ item.PT1 }} +
+
+ + PT2: {{ item.PT2 }} +
+
+ + C1: {{ item.C1 }} +
+
+ + C2: {{ item.C2 }} +
+
+ + UVC1: {{ item.UVC1 }} +
+
+ + Alarm1: {{ item.Alarm1 }} +
+
+ + Status: {{ item.STATUS }} +
+
+ {% endfor %} +
+
+ +
+
+ + + +