From eaee2e77c9218a3c77ad3895ce4ac61611da18c6 Mon Sep 17 00:00:00 2001 From: jeanGaston Date: Tue, 30 Apr 2024 13:50:33 +0200 Subject: [PATCH] Updates --- .gitignore | 11 ++ Install.sh | 21 +++ Program/Webserver.py | 49 +++++++ Program/__pycache__/database.cpython-311.pyc | Bin 1690 -> 2273 bytes Program/database.py | 7 +- Program/datascraper.py | 38 ++++-- Program/main.py | 11 ++ Program/templates/history.html | 129 +++++++++++++++++++ Program/templates/index.html | 88 +++++++++++++ 9 files changed, 337 insertions(+), 17 deletions(-) create mode 100644 Program/Webserver.py create mode 100644 Program/templates/history.html create mode 100644 Program/templates/index.html diff --git a/.gitignore b/.gitignore index fa8bef0..84cecd8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,14 @@ Program/__pycache__/env.cpython-311.pyc Program/data.db Program/env.py +Program/__pycache__/Webserver.cpython-311.pyc +Program/__pycache__/datascraper.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +data.db +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc diff --git a/Install.sh b/Install.sh index e69de29..c083082 100644 --- a/Install.sh +++ b/Install.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Install Python3 and pip +sudo apt update +sudo apt install -y python3 python3-pip python3-flask python3-schedule + +# Install required Python modules +pip3 install schedule +pip3 install bluepy + +# Create env.py file +echo -n "Enter database file name (default: data.db): " +read db_file +db_file=${db_file:-data.db} + + +echo -e "DB_FILE = '$db_file'" > Program/env.py + +# Execute main.py +cd Program +python3 main.py diff --git a/Program/Webserver.py b/Program/Webserver.py new file mode 100644 index 0000000..ee3e4bb --- /dev/null +++ b/Program/Webserver.py @@ -0,0 +1,49 @@ +import threading +from env import * +from flask import Flask, render_template +import sqlite3 + +app = Flask(__name__) +# Function to fetch data from the database +def fetch_all_data(): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("SELECT * FROM SensorData ") + data = c.fetchall() + conn.close() + data.reverse() + return data +# Function to fetch data from the database with the sensor name as a parameter +def fetch_data_by_sensor(sensor): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("SELECT * FROM SensorData WHERE Sensor LIKE ? ", ('%' + sensor + '%',)) + data = c.fetchall() + conn.close() + data.reverse() + return data +# Route to display the database contents +@app.route('/') +def dashboard(): + data = fetch_all_data()[:5] + + return render_template('index.html', data=data) + +#Route to display the sensor history +@app.route('/history') +def history(): + S1 = fetch_data_by_sensor("DEMO1") + S2 = fetch_data_by_sensor("DEMO2") + S3 = fetch_data_by_sensor("DEMO3") + + return render_template('history.html', S1=S1, S2=S2, S3=S3) + + +def run_flask(): + app.run() + +def RunInThread_WebServer(): + threading.Thread(target=run_flask, daemon=True).start() +if __name__ == '__main__': + app.run(debug=True) + diff --git a/Program/__pycache__/database.cpython-311.pyc b/Program/__pycache__/database.cpython-311.pyc index 11a8aa71ff94efd608e79b416a7d88a7b292a0a3..1f0704624d9ef0f38d55db2bff23e3f0469ff6fb 100644 GIT binary patch delta 741 zcmZWm&ui2`6rP!6(pbB@U3M!%MQ1Im8%&EJUPP3tsYcsu$;Kb+Wyx+*t9H9Gi3lYk zh~U*Fx8k8ErFc>N2YU3VHw_;2KiFa~?a4QZTR|V0FE8`H`R3(&>$#uB{M%e^6hS!O zPA|1+KYtkO{gWpS(iiA1^>71e$_KlOsXKuYidRV`UdQ$6xw>tbS1oqKXtG+vUk!R( zW41A8*oJl4Xs~Q6s9A2;*WyJ|j#c90dvN}2X~FF<$GGD#+gzBnn?ge{#soJjXqz#S zUg7aLmh3VqRJIYJ$9Dt_QcyqcN-J0&hpsiu$KVw}bBOT*V3O}F`<;g^&kfzS8~A(z zHYejgU7IXI)=>b4p+X|T`t`m4M5jumY>@t!QryA?<>^Q?vLV6O6!LI{BN9oG9ML8E zh9L9~Ru&k+Avrr#lp&{Ce&`vo)?CwS7`DSq%c=jd2uTeI^w!PweGo&9|@PN5A=O+84IARR<AiIE0ajg<41r}72F5$YpF8xL#8jy=jH(6!l|#*XWlor8Qbl{x>7w?!o>mF2VKE41GEoiRaF0nRQNYrMzq8L delta 303 zcmaDTIE$BWIWI340}yQ9XpqLtGLcV$QEj5S8Y^QkgC^_72vtU=s?U>WGAT}8%;?V+ zoSIjhUo_c + + + + + Database Contents + + + + + +

Database Contents

+

Sensor 1

+ + + + + + + + + + {% for row in S1 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+

Sensor 2

+ + + + + + + + + + {% for row in S2 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+

Sensor 3

+ + + + + + + + + + {% for row in S3 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+ + + + diff --git a/Program/templates/index.html b/Program/templates/index.html new file mode 100644 index 0000000..2690e90 --- /dev/null +++ b/Program/templates/index.html @@ -0,0 +1,88 @@ + + + + + + Sensor Data Dashboard + + + + +
+

Sensor Data Dashboard

+ + + + + + + + + + {% for row in data %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+ +
+ + +