diff --git a/Program/__pycache__/mail.cpython-311.pyc b/Program/__pycache__/mail.cpython-311.pyc new file mode 100644 index 0000000..d55802b Binary files /dev/null and b/Program/__pycache__/mail.cpython-311.pyc differ diff --git a/Program/datascraper.py b/Program/datascraper.py index 550d653..65a6614 100644 --- a/Program/datascraper.py +++ b/Program/datascraper.py @@ -1,3 +1,4 @@ +from mail import * import schedule import threading import time @@ -23,13 +24,13 @@ def BltDataScrap(): Bat = int(value[20:22], 16) #print(f"Temp : {temp} °c \n HR : {HR} % , \n Batterie : {Bat} %") add_sensor_data(DBFILE, sensor_dict[device.addr], time.strftime("%Y-%m-%d %H:%M:%S"), temp, HR, Bat) - if temp > MAX_TEMP : + """if temp > MAX_TEMP : email(RECIPIENT, MESSAGE_TEMP, temp, sensor_dict[device.addr], time.strftime("%Y-%m-%d %H:%M:%S") ) print("mail sent for temp max") elif temp > MAX_HR : email(RECIPIENT, MESSAGE_HR, HR, sensor_dict[device.addr], time.strftime("%Y-%m-%d %H:%M:%S") ) - print("mail sent for HR max") + print("mail sent for HR max") """ return 0 diff --git a/Program/mail.py b/Program/mail.py new file mode 100644 index 0000000..690db63 --- /dev/null +++ b/Program/mail.py @@ -0,0 +1,95 @@ +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import sqlite3 +import threading + +import schedule +from env import * + +MESSAGE_TEMP = """ + +Hi {recipient}, + +The temperature of {Sensor} as reached {Val} °c at {TimeStamp}. + +""" +MESSAGE_HR = """ + +Hi {recipient}, + +The HR of {Sensor} as reached {Val} percent at {TimeStamp}. + +""" + +def email(recipient_email, message, ReachedVal, Sensor, TimeStamp): + + """ + Sends emails with the following arguments: + - Recipient's email address + - Message body + - Reached value either for temp or HR + - The sensor on wich the values are maxed out + - the time stamp + """ + port = SMTP_PORT # For SSL + smtp_server = SMTP + sender_email = SMTP_ID # Enter your address + password = SMTP_PWD + # Create a MIME message + msg = MIMEMultipart() + msg['From'] = sender_email + msg['To'] = recipient_email + msg['Subject'] = f"One alert on {Sensor}" # Update with your email subject + + # Add message body + body = message.format(recipient=recipient_email, Sensor=Sensor, Val=ReachedVal, TimeStamp=TimeStamp) + msg.attach(MIMEText(body, 'plain')) + + try: + # Start a TLS encrypted connection + server = smtplib.SMTP(smtp_server, port) + server.starttls() + + # Login to the SMTP server + server.login(sender_email, password) + + # Send the email + server.sendmail(sender_email, recipient_email, msg.as_string()) + print("Email sent successfully!") + except Exception as e: + print(f"Failed to send email. Error: {e}") + finally: + # Close the connection + server.quit() + +def check_and_send_email(): + # Connect to the database + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + + # Retrieve the last record from SensorData table + c.execute("SELECT * FROM SensorData ORDER BY Id DESC LIMIT 1") + last_record = c.fetchone() + + if last_record: + # Extract HR and Temp values from the last record + _, sensor, time, temp, hr, _ = last_record + + # Check if HR or Temp exceed set values + if temp > MAX_HR: + email(RECIPIENT, MESSAGE_TEMP, temp, sensor, time) + elif hr > MAX_TEMP: + email(RECIPIENT, MESSAGE_HR, temp, sensor, time) + + else: + print("No data found in the database.") + + # Close database connection + conn.close() + +def RunInThread_MailAlerts(): + print("######################################################################\nOpen Thread mail check Alert") + threading.Thread(target=check_and_send_email, daemon=True).start() + +def ScheduleMailAlerts(): + schedule.every(2).minutes.do(RunInThread_MailAlerts) #run every 2 minutes \ No newline at end of file diff --git a/Program/main.py b/Program/main.py index b2dc1a0..8ac0017 100644 --- a/Program/main.py +++ b/Program/main.py @@ -1,14 +1,14 @@ -import smtplib -import ssl from database import * from datascraper import * from Webserver import RunInThread_WebServer +from mail import ScheduleMailAlerts from env import * import schedule check_database(DBFILE) RunInThread_WebServer() ScheduleDataScrap() +ScheduleMailAlerts() while True: diff --git a/Program/templates/admin.html b/Program/templates/admin.html index d41d527..3e13a48 100644 --- a/Program/templates/admin.html +++ b/Program/templates/admin.html @@ -4,7 +4,7 @@