Updates
This commit is contained in:
parent
c87cd6fc93
commit
d1e4ca8b97
BIN
Program/__pycache__/mail.cpython-311.pyc
Normal file
BIN
Program/__pycache__/mail.cpython-311.pyc
Normal file
Binary file not shown.
@ -1,3 +1,4 @@
|
|||||||
|
from mail import *
|
||||||
import schedule
|
import schedule
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
@ -23,13 +24,13 @@ def BltDataScrap():
|
|||||||
Bat = int(value[20:22], 16)
|
Bat = int(value[20:22], 16)
|
||||||
#print(f"Temp : {temp} °c \n HR : {HR} % , \n Batterie : {Bat} %")
|
#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)
|
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") )
|
email(RECIPIENT, MESSAGE_TEMP, temp, sensor_dict[device.addr], time.strftime("%Y-%m-%d %H:%M:%S") )
|
||||||
print("mail sent for temp max")
|
print("mail sent for temp max")
|
||||||
elif temp > MAX_HR :
|
elif temp > MAX_HR :
|
||||||
email(RECIPIENT, MESSAGE_HR, HR, sensor_dict[device.addr], time.strftime("%Y-%m-%d %H:%M:%S") )
|
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
|
return 0
|
||||||
|
|||||||
95
Program/mail.py
Normal file
95
Program/mail.py
Normal file
@ -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
|
||||||
@ -1,14 +1,14 @@
|
|||||||
import smtplib
|
|
||||||
import ssl
|
|
||||||
from database import *
|
from database import *
|
||||||
from datascraper import *
|
from datascraper import *
|
||||||
from Webserver import RunInThread_WebServer
|
from Webserver import RunInThread_WebServer
|
||||||
|
from mail import ScheduleMailAlerts
|
||||||
from env import *
|
from env import *
|
||||||
import schedule
|
import schedule
|
||||||
|
|
||||||
check_database(DBFILE)
|
check_database(DBFILE)
|
||||||
RunInThread_WebServer()
|
RunInThread_WebServer()
|
||||||
ScheduleDataScrap()
|
ScheduleDataScrap()
|
||||||
|
ScheduleMailAlerts()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Sensor Data Dashboard</title>
|
<title>Sensor Data Dashboard</title>
|
||||||
<link rel="stylesheet" href="/css">
|
<!-- <link rel="stylesheet" href="/css"> -->
|
||||||
<style>body {
|
<style>body {
|
||||||
font-family: Arial, sans-serif;
|
font-family: Arial, sans-serif;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Database Contents</title>
|
<title>Database Contents</title>
|
||||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||||
<link rel="stylesheet" href="/css">
|
<!-- <link rel="stylesheet" href="/css"> -->
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
font-family: Arial, sans-serif;
|
font-family: Arial, sans-serif;
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Sensor Data Dashboard</title>
|
<title>Sensor Data Dashboard</title>
|
||||||
<link rel="stylesheet" href="/css">
|
<!-- <link rel="stylesheet" href="/css"> -->
|
||||||
<style>body {
|
<style>body {
|
||||||
font-family: Arial, sans-serif;
|
font-family: Arial, sans-serif;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user