Browse Source

Upload files to 'server/app/services'

master
5okin 4 years ago
parent
commit
e0bf3836fd
  1. 28
      server/app/services/data_process.py
  2. 28
      server/app/services/database_handle.py
  3. 40
      server/app/services/weather_mail.py

28
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)

28
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")

40
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"""\
<!DOCTYPE html>
<html>
<body>
<h1 style="color:SlateGray;">Weather is off the charts !!!!</h1>
<p>{message}</p>
</body>
</html>
""", subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Loading…
Cancel
Save