From a56ccaf14cb2813eb2c2ad7b5dfadf28039a1dfb Mon Sep 17 00:00:00 2001 From: jeanGaston Date: Thu, 2 May 2024 02:54:48 +0200 Subject: [PATCH] Updates --- Program/Webserver.py | 58 +++++++++------ Program/__pycache__/database.cpython-311.pyc | Bin 4552 -> 8068 bytes Program/__pycache__/mail.cpython-311.pyc | Bin 4278 -> 4427 bytes Program/database.py | 74 ++++++++++++++++--- Program/mail.py | 14 ++-- Program/main.py | 1 + Program/templates/admin.html | 30 ++++++-- Program/templates/history.html | 3 +- Program/templates/index.html | 1 + 9 files changed, 135 insertions(+), 46 deletions(-) diff --git a/Program/Webserver.py b/Program/Webserver.py index 3a54eec..9dbf94b 100644 --- a/Program/Webserver.py +++ b/Program/Webserver.py @@ -4,7 +4,7 @@ import threading import requests from env import * -from flask import Flask, render_template, request +from flask import Flask, redirect, render_template, request import sqlite3 from database import * app = Flask(__name__) @@ -38,15 +38,17 @@ def fetch_outdoor_temperature(): # Appel de la fonction pour obtenir la température -temperature = fetch_outdoor_temperature() -print(temperature) +#temperature = fetch_outdoor_temperature() +#print(temperature) + + # Route to display the database contents @app.route('/') def dashboard(): data = fetch_all_data()[:5] - return render_template('index.html', data=data, temperature=temperature) + return render_template('index.html', data=data, temperature=None) #Route to display the sensor history @app.route('/history') @@ -64,26 +66,40 @@ def history(): return render_template('history.html', S1=disp_data[0], S2=disp_data[1], S3=disp_data[2]) -@app.route('/adm', methods=['GET', 'POST']) +@app.route('/adm', methods=['GET']) def admin(): - if request.method == 'POST': - # Get form data - old_name = request.form['old_name'] - new_name = request.form['new_name'] - - # Update sensor name in the database - conn = sqlite3.connect(DBFILE) - c = conn.cursor() - print(old_name, new_name) - c.execute("UPDATE Sensors SET Name = ? WHERE mac = ?", (new_name, old_name)) - print() - conn.commit() - conn.close() - + + # Fetch sensor data data = fetch_all_sensor() - print(data) - return render_template('admin.html', data=data, sensors=data) + + # Fetch email settings + email_settings = fetch_email_settings() + return render_template('admin.html', data=data, sensors=data, email_settings=email_settings) + +@app.route('/updateMail', methods=['POST']) +def update_mail(): + # Process the form data here + smtp_id = request.form['smtp_id'] + smtp_pwd = request.form['smtp_pwd'] + smtp_server = request.form['smtp_server'] + smtp_port = request.form['smtp_port'] + recipient_email = request.form['recipient_email'] + + # Update email settings in the database or perform any other actions + update_email_settings(smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email) + # Redirect to a success page or render a template + return redirect("/adm") + +@app.route('/updateSensor', methods=['POST']) +def updateSensor(): + # Get sensor name form data + old_name = request.form['old_name'] + new_name = request.form['new_name'] + + update_sensor_settings(new_name, old_name) + + return redirect("/adm") def run_flask(): app.run() diff --git a/Program/__pycache__/database.cpython-311.pyc b/Program/__pycache__/database.cpython-311.pyc index ea1a347cf50e0678e71ec7a6bfc08309d4829945..e9c9ad8bcfbbff52d624500e98888baf283147a3 100644 GIT binary patch literal 8068 zcmeHL%WoUU8Q)#*l1uZY$c~!0NW6*Bij*kHY1$-qoXE07MYKdpA*slA;>t94Wy#?~ z*IRw4Ph|_~l{l3`;U!p?$ z7NEoBH?MDJW@moiW4^!ne0wNJnScFe!PiYu|Hg`y7_;z$424^iNQrco%2B5&8p}+U zK21aJ$TB&{X@}Xyo@Swq&2lM9_s`!ck-K+JgfJK+Dr6KK6fsdar9 z>~e|v3d-Nw^;Nn=Z&{y-quA*yD1B?!R+$pBg`>A5k!~E3rc_*snYK8qRG&AFJw}~* z=_6Rfs$-RHI}1~Cl-LF4E8zRqUN22;zTEZ=NAtScas+*MZw7+Fc( zXO$>wfoonp?7gSfUn@244^07I-MsDjL<&ITj2FexL}Vlt5mF<^ry@dhT!>GngviNg zGL;mPQeG*@${wL91!L)qkcym42{VakY$S0?n2ekXx3I<2If<}^if=~a$w(q4Oecis zd-3T6%oI;eTN4O4U|2x@knq9C)NCXv1c$@Ia46ji@cYRXj-}Of%k06Zh?7L#izMoo zDU8gfrlWBfGZu-bS{H46Gg)?PX9~nhQPa8QcKs4`!KAHkPQZs7i)rmUo>q5$X&g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1hM*%D0H#VqEEOCa z4da`300;Sr)@T}sa+Z~K?21#RzB^3<1Zp+f$+@EFBR5 z%Wo=mHI>jJn_q6Fpr?S7Vjshz@c61ju?SCe7s|FL*p(&!=2E{mc*OqE`CX)#%rD1R-Q+0B`K9lw7s^%Z sPRYJV$KW0Q=bZPf9-kcQ`@|jcYslTCy}|9lINOQO7@R`F4Fq5KHwvuf1ONa4 diff --git a/Program/__pycache__/mail.cpython-311.pyc b/Program/__pycache__/mail.cpython-311.pyc index 80282071346badfd7f340fe81573a004809ea9e4..ba56e5387a241755e6cf073dfb9193f480ff07b0 100644 GIT binary patch delta 985 zcmY*X%}*0S6rbsSwB2@hTdY_JY6}=xO#FhzKnzd=L`5P}5L1n$b{8SGh0G3UOdF~( zo;=u0f*y$n1-~vN{sE*O)I@253E6~b^r9#0ftYwO&a_g(?96Z9{NBgB_hw!P-`6Wo zWmy7rY#7EC7%ZGGfM_aby4%m@S`irUmjH9w3jE-K5^O7xE-*+rHq9=iLE5?CW`5)cr;-|5Xd`y|LXgrL0IuDR z-Uv`##*HDF=enTsKTd5$@-8>?x1p?B!ZeypVPvPCL|}qgex4{PYyURPoq9HbeXqk>RC|q zaYEgu0|?S5Xdcu7yVnd}sFTgXTy5i@wFVO%b^KM72*T zFIyM!Qu~Ycf;Y704Uv2Hw?|f#lTuF&$opZhALMJKo-QumP}S4HZU)`Ok~wB3{o*pTGUpNk(!sp)#Lg>rtf9YPr>4h)U4&uV>os-M%&JA z54AD~F<^Z!rB^x)v4H3tgKh>ay+4i<$tis%VRWJqx@CErAIVQO(zg!OZ0TDE-qo$S k4z$&sv&f4cKI<+jqQYmTqNfE`7eNIslAE3mzMDq<2fC&8^#A|> delta 829 zcmYjNOH30%7@pb3>~6c=ZUK2V1PF*FCK6~U5+5`aBLd-3&{$2HhFt}v4`)Zbv~f+m zO47{1E8(JEObm%vqj11@(Zqvpg7M ztAShJ1689L-l_Ip=|LIIQZfNceGD=|OJk^80fr$f$k4E)G>GZd&3N_ayU`5+fTw`) zDgUe@6BawP3L?@W2cV)5^q;2^jn-hZRME-`jSD?cruVdZnitw3;!Ff2OgDuyA@jIt z1M-Xv%BU<;Q=Ai(vPvy61{Jz4o`r36SM02h9@lpR9Ifc2jUI>>Vc4HPBefo}$kw{_ z2=(t9G!oaD5;hokQmKfE z7j*KZlyz*pW@Arx?1E+EtewxTta@rG=Qt0F*rMOijZ=NBrjIZR>@kFzR=9RYOU7@abr@&w~IY2L`d zB;7S$_Hq9Oz)AMQzHnWdDO>kf?P+!^$yFN@2hXu=j&6hp5wDAd>DO>#@icF_#9@ko zC)$NI9Ov#32Y!M6E|@F$6Ywl&M@qldbNM0{x_}p1f-mX3p~OrN)ewND8|Kd!5czoc e)+A&~u3j^cA-RE?*$W$M;0WevC^91P>3;z4?#x2~ diff --git a/Program/database.py b/Program/database.py index be4a675..46ead2b 100644 --- a/Program/database.py +++ b/Program/database.py @@ -3,7 +3,7 @@ import sqlite3 from os import path import time -from env import SENSORS, DBFILE +from env import * # Function to create the database and tables if they don't exist def create_database(db_name): @@ -14,7 +14,9 @@ def create_database(db_name): c.execute('''CREATE TABLE IF NOT EXISTS Sensors (Mac TEXT PRIMARY KEY, Name TEXT)''') - + for mac, name in SENSORS.items(): + # Use INSERT OR IGNORE to prevent duplicates based on primary key + c.execute("INSERT OR IGNORE INTO Sensors (Mac, Name) VALUES (?, ?)", (mac, name)) # Create SensorData table c.execute('''CREATE TABLE IF NOT EXISTS SensorData (Id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -25,14 +27,15 @@ def create_database(db_name): Bat INT, FOREIGN KEY (Sensor) REFERENCES Sensors(Name))''') - c.execute('''CREATE TABLE IF NOT EXISTS Settings - (Id INTEGER PRIMARY KEY AUTOINCREMENT, - Name TEXT, - Value TEXT''') + c.execute('''CREATE TABLE IF NOT EXISTS settings ( + id INTEGER PRIMARY KEY, + smtp_id TEXT, + smtp_pwd TEXT, + smtp_server TEXT, + smtp_port INTEGER, + recipient_email TEXT);''') + c.execute("INSERT OR IGNORE INTO settings (smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email) VALUES (?, ?, ?, ?, ?)", (SMTP_ID, SMTP_PWD, SMTP, SMTP_PORT, RECIPIENT)) - for mac, name in SENSORS.items(): - # Use INSERT OR IGNORE to prevent duplicates based on primary key - c.execute("INSERT OR IGNORE INTO Sensors (Mac, Name) VALUES (?, ?)", (mac, name)) conn.commit() @@ -86,4 +89,55 @@ def fetch_all_sensor(): data = c.fetchall() conn.close() data.reverse() - return data \ No newline at end of file + return data + +def update_sensor_settings(new_name, old_name): + # Update sensor name in the database + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("UPDATE Sensors SET Name = ? WHERE mac = ?", (new_name, old_name)) + conn.commit() + conn.close() + +# Function to fetch email settings from the database +def fetch_email_settings(): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("SELECT smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email FROM settings") + settings = c.fetchone() + conn.close() + return settings + +# Function to update email settings in the database +def update_email_settings(smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("UPDATE settings SET smtp_id = ?, smtp_pwd = ?, smtp_server = ?, smtp_port = ?, recipient_email = ? ", (smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email)) + conn.commit() + print(f"[{datetime.now()}] Mail settings updated :") + print_email_settings() + conn.close() + +def print_email_settings(): + # Connect to the database + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + + # Fetch email settings + c.execute("SELECT * FROM settings") + settings = c.fetchone() + + # Print settings + if settings: + print("Email Settings:") + print(f"Sender Email: {settings[1]}") + print(f"SMTP pwd:{settings[2]}") + print(f"SMTP Server: {settings[3]}") + print(f"SMTP Port: {settings[4]}") + print(f"Recipient Email: {settings[5]}") + else: + print("No email settings found in the database.") + + # Close the connection + conn.close() + diff --git a/Program/mail.py b/Program/mail.py index 5bf1020..6ada2bc 100644 --- a/Program/mail.py +++ b/Program/mail.py @@ -3,10 +3,9 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import sqlite3 import threading - - import schedule from env import * +from database import fetch_email_settings MESSAGE_TEMP = """ @@ -33,11 +32,14 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp): - The sensor on wich the values are maxed out - the time stamp """ - port = SMTP_PORT # For SSL + """ port = SMTP_PORT # For SSL smtp_server = SMTP sender_email = SMTP_ID # Enter your address - password = SMTP_PWD - # Create a MIME message + password = SMTP_PWD """ + print(fetch_email_settings()) + sender_email, password, smtp_server, port, recipient_email = fetch_email_settings() + + # Create a MIME message msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = recipient_email @@ -49,7 +51,7 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp): try: # Start a TLS encrypted connection - server = smtplib.SMTP(smtp_server, port) + server = smtplib.SMTP(smtp_server, int(port)) server.starttls() # Login to the SMTP server diff --git a/Program/main.py b/Program/main.py index 8ac0017..43a9209 100644 --- a/Program/main.py +++ b/Program/main.py @@ -9,6 +9,7 @@ check_database(DBFILE) RunInThread_WebServer() ScheduleDataScrap() ScheduleMailAlerts() +#print_email_settings() while True: diff --git a/Program/templates/admin.html b/Program/templates/admin.html index 3e13a48..9a13cb4 100644 --- a/Program/templates/admin.html +++ b/Program/templates/admin.html @@ -75,8 +75,9 @@

Sensors

@@ -98,7 +99,7 @@

Rename Sensor

-
+ +

+ + +

+ + +

+ + +

+ + +

+ +
diff --git a/Program/templates/history.html b/Program/templates/history.html index 50e144a..6b01f51 100644 --- a/Program/templates/history.html +++ b/Program/templates/history.html @@ -55,8 +55,9 @@ tr:hover {

Database Contents

Sensor 1

diff --git a/Program/templates/index.html b/Program/templates/index.html index 039be6d..19d4e3d 100644 --- a/Program/templates/index.html +++ b/Program/templates/index.html @@ -52,6 +52,7 @@