From e0bf3836fd5080aec30c62b23c5f9af4f9ce2bca Mon Sep 17 00:00:00 2001 From: 5okin Date: Sun, 24 Jan 2021 17:33:14 +0000 Subject: [PATCH] Upload files to 'server/app/services' --- server/app/services/data_process.py | 28 ++++++++++++++++++ server/app/services/database_handle.py | 28 ++++++++++++++++++ server/app/services/weather_mail.py | 40 ++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 server/app/services/data_process.py create mode 100644 server/app/services/database_handle.py create mode 100644 server/app/services/weather_mail.py diff --git a/server/app/services/data_process.py b/server/app/services/data_process.py new file mode 100644 index 0000000..f1d9b02 --- /dev/null +++ b/server/app/services/data_process.py @@ -0,0 +1,28 @@ +import time +from datetime import datetime +from .weather_mail import send_mail + + +def percentage(old, new, operator, percent): + '''Check if numbers are within range''' + + if operator == '+': + return bool(round((old + old * percent / 100.0), 1) == new) + elif operator == '-': + return bool(round((old - old * percent / 100.0)) == new) + + +def process(data): + '''Check incoming data for +40% temp and -50% ground humidity''' + + if len(data) > 0: + latest_measurement = data[len(data)-1] + + for measurement in data: + if measurement != data[len(data)-1]: + tdelta = datetime.strptime(latest_measurement['time'], '%H:%M:%S') - datetime.strptime(measurement['time'], '%H:%M:%S') + if tdelta.seconds < 300: + if percentage(measurement['temperature'], latest_measurement['temperature'], '+', 40) and percentage(measurement['air-humidity'], latest_measurement['air-humidity'], '-', 50): + send_mail(measurement['temperature'], latest_measurement['temperature'], measurement['air-humidity'], latest_measurement['air-humidity']) + else: + data.remove(measurement) diff --git a/server/app/services/database_handle.py b/server/app/services/database_handle.py new file mode 100644 index 0000000..ccc7d8c --- /dev/null +++ b/server/app/services/database_handle.py @@ -0,0 +1,28 @@ +import json +import os +from flask_pymongo import PyMongo +from influxdb import InfluxDBClient +from app import app + + +#Connect to db using environment variables +CLIENT = InfluxDBClient('influxdb', 8086, os.environ['INFLUXDB_USERNAME'], os.environ['INFLUXDB_PASSWORD'], os.environ['INFLUXDB_DATABASE']) + + +def send_data(data): + '''Send a single json object to the database''' + + data_to_send = [] + add_db_info = {} + + add_db_info['measurement'] = "Rpi_data" + add_db_info['fields'] = data + + data_to_send.append(add_db_info) + + print(f'Send to DB: {data_to_send}', flush=True) + + try: + CLIENT.write_points(data_to_send) + except: + print("Failed to send to database") diff --git a/server/app/services/weather_mail.py b/server/app/services/weather_mail.py new file mode 100644 index 0000000..70a7ba0 --- /dev/null +++ b/server/app/services/weather_mail.py @@ -0,0 +1,40 @@ +import os +import smtplib +from email.message import EmailMessage + + +EMAIL_ADDRESS = os.environ['MAILUSER'] +EMAIL_PASSWORD= os.environ['MAILPASS'] + +def send_mail(measurment_temp, latest_measurment_temp, measurment_hum, latest_measurment_hum): + '''Handles email compose and connection to SMTP''' + + print(f"Email send: {measurment_temp} and {latest_measurment_temp}", flush=True) + + message = f"""The temperature went from {measurment_temp} to {latest_measurment_temp} + in five minutes, thats a 40% increase! And the air humidity went from + {measurment_hum} to {latest_measurment_hum} in five minutes, thats a 50% + drop! + """ + + msg = EmailMessage() + msg['Subject'] = 'Weather Staion Report' + msg['From'] = EMAIL_ADDRESS + msg['To'] = 'nkoutsolelos@gmail.com' + + msg.set_content('This email was send automaticly from the weather server') + + msg.add_alternative(f"""\ + + + +

Weather is off the charts !!!!

+

{message}

+ + + """, subtype='html') + + + with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: + smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) + smtp.send_message(msg)