This commit is contained in:
jeanGaston 2024-05-04 15:17:55 +02:00
parent 06d905ae57
commit 660ae4a448
5 changed files with 45 additions and 19 deletions

View File

@ -79,7 +79,10 @@ def admin():
# Fetch email settings # Fetch email settings
email_settings = fetch_email_settings() email_settings = fetch_email_settings()
return render_template('admin.html', data=data, sensors=data, email_settings=email_settings)
#Fetch threshold settings
threshold_settings = fetch_threshold_settings()
return render_template('admin.html', data=data, sensors=data, email_settings=email_settings, threshold_settings=threshold_settings)
@app.route('/updateMail', methods=['POST']) @app.route('/updateMail', methods=['POST'])
def update_mail(): def update_mail():

View File

@ -5,7 +5,6 @@ import plotly.graph_objs as go
import pandas as pd import pandas as pd
import time
from env import * from env import *
@ -31,14 +30,16 @@ def create_database(db_name):
Bat INT, Bat INT,
FOREIGN KEY (Sensor) REFERENCES Sensors(Name))''') FOREIGN KEY (Sensor) REFERENCES Sensors(Name))''')
c.execute('''CREATE TABLE IF NOT EXISTS settings ( c.execute('''CREATE TABLE IF NOT EXISTS alert_settings (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
smtp_id TEXT, smtp_id TEXT,
smtp_pwd TEXT, smtp_pwd TEXT,
smtp_server TEXT, smtp_server TEXT,
smtp_port INTEGER, smtp_port INTEGER,
recipient_email TEXT);''') 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)) MAX_HR INT
MAX_TEMP);''')
c.execute("INSERT OR IGNORE INTO alert_settings (smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email, MAX_HR, MAX_TEMP) VALUES (?, ?, ?, ?, ?, ?, ?)", (SMTP_ID, SMTP_PWD, SMTP, SMTP_PORT, RECIPIENT, MAX_HR, MAX_TEMP))
@ -49,11 +50,11 @@ def create_database(db_name):
def check_database(db_name): def check_database(db_name):
if not path.exists(db_name): if not path.exists(db_name):
print(f"[{datetime.now()}] Database '{db_name}' not found. Creating...") print(f"[{datetime.now()}] DB - Database '{db_name}' not found. Creating...")
create_database(db_name) create_database(db_name)
print(f"[{datetime.now()}] Database and tables created successfully.") print(f"[{datetime.now()}] DB - Database and tables created successfully.")
else: else:
print(f"[{datetime.now()}] Database '{db_name}' found.") print(f"[{datetime.now()}] DB - Database '{db_name}' found.")
# Function to add data to SensorData table # Function to add data to SensorData table
@ -107,7 +108,15 @@ def update_sensor_settings(new_name, old_name):
def fetch_email_settings(): def fetch_email_settings():
conn = sqlite3.connect(DBFILE) conn = sqlite3.connect(DBFILE)
c = conn.cursor() c = conn.cursor()
c.execute("SELECT smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email FROM settings") c.execute("SELECT smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email FROM alert_settings")
settings = c.fetchone()
conn.close()
return settings
# Function to fetch MAX HR and MAX temp from the database
def fetch_threshold_settings():
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute("SELECT MAX_HR, MAX_TEMP FROM alert_settings")
settings = c.fetchone() settings = c.fetchone()
conn.close() conn.close()
return settings return settings
@ -116,9 +125,19 @@ def fetch_email_settings():
def update_email_settings(smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email): def update_email_settings(smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email):
conn = sqlite3.connect(DBFILE) conn = sqlite3.connect(DBFILE)
c = conn.cursor() 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)) c.execute("UPDATE alert_settings SET smtp_id = ?, smtp_pwd = ?, smtp_server = ?, smtp_port = ?, recipient_email = ? ", (smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email))
conn.commit() conn.commit()
print(f"[{datetime.now()}] Mail settings updated :") print(f"[{datetime.now()}] Web - Mail settings updated :")
print_email_settings()
conn.close()
# Function to update threshold settings in the database
def update_threshold_settings(max_hr, max_temp):
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute("UPDATE alert_settings SET smtp_id = ?, smtp_pwd = ? ", (smtp_id, smtp_pwd, smtp_server, smtp_port, recipient_email))
conn.commit()
print(f"[{datetime.now()}] Web - Mail settings updated :")
print_email_settings() print_email_settings()
conn.close() conn.close()
@ -128,7 +147,7 @@ def print_email_settings():
c = conn.cursor() c = conn.cursor()
# Fetch email settings # Fetch email settings
c.execute("SELECT * FROM settings") c.execute("SELECT * FROM alert_settings")
settings = c.fetchone() settings = c.fetchone()
# Print settings # Print settings
@ -172,8 +191,6 @@ def history_graph_temp():
graph_json = fig.to_json() graph_json = fig.to_json()
#print(graph_json) #print(graph_json)
return graph_json return graph_json
def history_graph_HR(): def history_graph_HR():
# Fetch sensor data # Fetch sensor data
sensorList = fetch_all_sensor() sensorList = fetch_all_sensor()

View File

@ -32,7 +32,7 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp):
- The sensor on wich the values are maxed out - The sensor on wich the values are maxed out
- the time stamp - the time stamp
""" """
print(fetch_email_settings()) #print(fetch_email_settings())
sender_email, password, smtp_server, port, recipient_email = fetch_email_settings() sender_email, password, smtp_server, port, recipient_email = fetch_email_settings()
# Create a MIME message # Create a MIME message
@ -55,11 +55,11 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp):
# Send the email # Send the email
server.sendmail(sender_email, recipient_email, msg.as_string()) server.sendmail(sender_email, recipient_email, msg.as_string())
print(f" [{datetime.now()}] Email sent successfully!") print(f"[{datetime.now()}] Mail Alerte System - Email sent successfully!")
# Close the connection # Close the connection
server.quit() server.quit()
except Exception as e: except Exception as e:
print(f"[{datetime.now()}] Failed to send email. Error: {e}") print(f"[{datetime.now()}] Mail Alerte System - Failed to send email. Error: {e}")
finally: finally:
None None
@ -81,10 +81,10 @@ def check_and_send_email():
if temp > MAX_HR: if temp > MAX_HR:
email(RECIPIENT, MESSAGE_TEMP, temp, sensor, time) email(RECIPIENT, MESSAGE_TEMP, temp, sensor, time)
elif hr > MAX_TEMP: elif hr > MAX_TEMP:
email(RECIPIENT, MESSAGE_HR, temp, sensor, time) email(RECIPIENT, MESSAGE_HR, hr, sensor, time)
else: else:
print("No data found in the database.") print(f"[{datetime.now()}] Mail - No data found in the database.")
# Close database connection # Close database connection
conn.close() conn.close()

View File

@ -129,6 +129,12 @@
<label for="recipient_email">Recipient Email:</label> <label for="recipient_email">Recipient Email:</label>
<input type="email" id="recipient_email" name="recipient_email" value="{{ email_settings[4] }}"> <input type="email" id="recipient_email" name="recipient_email" value="{{ email_settings[4] }}">
<br><br> <br><br>
<label for="MAX_TEMP">Temperature max threshold:</label>
<input type="number" id="MAX_TEMP" name="MAX_TEMP" value="{{ threshold_settings[0] }}">
<br><br>
<label for="MAX_HR">HR max threshold:</label>
<input type="number" id="MAX_HR" name="MAX_HR" value="{{ threshold_settings[1] }}">
<br><br>
<button type="submit">Save Settings</button> <button type="submit">Save Settings</button>
</form> </form>
</body> </body>