-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
119 lines (94 loc) · 3.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask, flash, render_template, request, redirect, session, url_for
import uuid
import sqlite3
import json
from sqlite3 import Error
from marshmallow import Schema, fields
from sense_hat import SenseHat
import datetime
app = Flask(__name__)
app.config.from_object("config.DevelopmentConfig")
db = app.config["DB_NAME"]
sense = SenseHat()
def create_connection(db):
con = None
try:
con = sqlite3.connect(db)
except Error as e:
print(e)
return con
@app.route('/data')
def get_sensor_data():
temp = sense.get_temperature()
humidity = sense.get_humidity()
pressure = sense.get_pressure()
data = {}
data['timestamp'] = str(datetime.datetime.now())
data['temp'] = temp
data['humidity'] = humidity
data['pressure'] = pressure
json_data = json.dumps(data)
return json_data
@app.route('/')
def index():
# fetch current settings from db
con = create_connection(db)
cur = con.cursor()
cur.execute('SELECT * FROM settings')
results = cur.fetchone()
current_settings = {}
if results is not None:
current_settings = {
'userid': results[0],
'low_temp': results[1],
'high_temp': results[2],
'low_humidity': results[3],
'high_humidity': results[4],
'low_pressure': results[5],
'high_pressure': results[6],
'polling_frequency': results[7]
}
# render configuration page with current settings
return render_template('index.html', settings = current_settings)
# set validation rules for settings
class UpdateSettingsInputSchema(Schema):
userid = fields.Str(required=True)
high_temp = fields.Int(required=True)
low_temp = fields.Int(required=True)
high_humidity = fields.Int(required=True)
low_humidity = fields.Int(required=True)
high_pressure = fields.Int(required=True)
low_pressure = fields.Int(required=True)
polling_frequency = fields.Int(required=True)
@app.route('/update_settings', methods=['POST'])
def update_settings():
if request.method == 'POST':
# check for validation errors
update_settings_schema = UpdateSettingsInputSchema()
errors = update_settings_schema.validate(request.form)
if errors:
flash(errors)
return redirect(url_for('index'))
# get setting values from form data
userid = request.form.get('userid')
high_temp = request.form.get('high_temp')
low_temp = request.form.get('low_temp')
high_humidity = request.form.get('high_humidity')
low_humidity = request.form.get('low_humidity')
high_pressure = request.form.get('high_pressure')
low_pressure = request.form.get('low_pressure')
polling_frequency = request.form.get('polling_frequency')
# clear existing settings in db
con = create_connection(db)
cur = con.cursor()
cur.execute('DELETE from settings')
con.commit()
# insert the updated settings into db
sql = 'INSERT INTO settings (userid,low_temp,high_temp,low_humidity,high_humidity,low_pressure,high_pressure,polling_frequency) VALUES (?,?,?,?,?,?,?,?)'
cur.execute(sql, (userid, low_temp, high_temp, low_humidity,
high_humidity, low_pressure, high_pressure, polling_frequency))
con.commit()
flash('Updated settings.')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0')