-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
168 lines (140 loc) · 5.71 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
import atexit
import json
import os
import requests
from math import pi
from datetime import datetime
from bokeh.embed import components
from bokeh.models import HoverTool, ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure
from bokeh.resources import CDN
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
# Dev
#from config import weather_key, pi_key
# Production
weather_key = os.environ['weather_key']
pi_key = os.environ['pi_key']
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///temperature.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Temperature(db.Model):
id = db.Column(db.Integer, primary_key=True)
isOutside = db.Column(db.Boolean, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.now)
temp_C = db.Column(db.Float, nullable=False)
temp_F = db.Column(db.Float, nullable=False)
def __repr__(self):
place = 'Outside' if self.isOutside else 'Inside'
return f'<{place}Temp {self.id}: ({self.timestamp.time()}, {self.temp_F})>'
def make_plot():
inside = Temperature.query.filter_by(isOutside=False).order_by(Temperature.timestamp).all()
outside = Temperature.query.filter_by(isOutside=True).order_by(Temperature.timestamp).all()
max_points = len(inside) if len(inside) < len(outside) else len(outside)
inside_time = []
inside_temp = []
outside_time = []
outside_temp = []
if max_points > 0:
inside_time = [t.timestamp for t in inside][-max_points:]
inside_temp = [t.temp_F for t in inside][-max_points:]
outside_time = [t.timestamp for t in outside][-max_points:]
outside_temp = [t.temp_F for t in outside][-max_points:]
data = {
'inside_time': inside_time,
'inside_temp': inside_temp,
'outside_time': outside_time,
'outside_temp': outside_temp,
}
source = ColumnDataSource(data)
inside_hover = HoverTool(
tooltips=[
('Time', '@inside_time{%m/%d %I:%M:%S %p}'),
('Inside', '@inside_temp{0.00}'),
],
formatters={
'@inside_time': 'datetime',
},
mode='vline'
)
outside_hover = HoverTool(
tooltips=[
('Time', '@outside_time{%m/%d %I:%M:%S %p}'),
('Outside', '@outside_temp{0.00}'),
],
formatters={
'@outside_time': 'datetime',
},
mode='vline'
)
p = figure(plot_width=700, plot_height=700, tools=[inside_hover, outside_hover], title="Temperature over time", background_fill_color="#EFEFEF", x_axis_type='datetime')
p.xaxis.formatter = DatetimeTickFormatter(
microseconds = [r"%m/%d %I:%M:%S %p"],
milliseconds = [r"%m/%d %I:%M:%S %p"],
seconds = [r"%m/%d %I:%M:%S %p"],
minsec = [r"%m/%d %I:%M:%S %p"],
minutes=[r"%m/%d %I:%M:%S %p"],
hourmin=[r"%m/%d %I:%M:%S %p"],
hours=[r"%m/%d %I:%M:%S %p"],
days=[r"%m/%d %I:%M:%S %p"],
months=[r"%m/%d %I:%M:%S %p"],
years=[r"%m/%d %I:%M:%S %p"],
)
p.xaxis.major_label_orientation = pi/4
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Temp (°F)'
p.line('inside_time', 'inside_temp', line_color='blue', legend_label='Inside', source=source)
inside_circle = p.circle('inside_time', 'inside_temp', line_color='black', fill_color='blue', size=5, source=source)
p.line('outside_time', 'outside_temp', line_color='red', legend_label='Outside', source=source)
outside_circle = p.circle('outside_time', 'outside_temp', line_color='black', fill_color='red', size=5, source=source)
p.tools[0].renderers = [inside_circle]
p.tools[1].renderers = [outside_circle]
return components(p)
@app.route('/', methods=['GET'])
def index():
script, div = make_plot()
return render_template('index.html', script=script, div=div, resources=CDN.render())
@app.route('/add-data/inside', methods=['POST'])
def add_inside_data():
# Incoming temperature data from temp probe
key = request.form['key']
if key != pi_key:
print(f"{datetime.now()} - Unauthorized upload attempt using key {key} from {request.remote_addr}.")
return redirect('/')
temp_C = request.form['temp_C']
temp_F = request.form['temp_F']
new_temp = Temperature(isOutside=False, temp_C=temp_C, temp_F=temp_F)
try:
db.session.add(new_temp)
db.session.commit()
print(f"{datetime.now()} - Added internal temp ({temp_C}, {temp_F})")
return f"{datetime.now()} - Successfully added internal temp ({temp_C},{temp_F})!"
except:
return 'There was a problem adding internal temp'
@app.route('/add-data/outside', methods=['POST'])
def add_outside_data():
# Incoming temperature data from weather API
key = request.form['key']
if key != pi_key:
print(f"{datetime.now()} - Unauthorized upload attempt using key {key} from {request.remote_addr}.")
return redirect('/')
temp_C = request.form['temp_C']
temp_F = request.form['temp_F']
new_temp = Temperature(isOutside=True, temp_C=temp_C, temp_F=temp_F)
try:
db.session.add(new_temp)
db.session.commit()
print(f"{datetime.now()} - Added external temp ({temp_C}, {temp_F})")
return f"{datetime.now()} - Successfully added external temp ({temp_C},{temp_F})!"
except:
return 'There was a problem adding external temp'
@app.errorhandler(400)
def handle_bad_request(error):
return 'Error 400: Bad Request'
@app.errorhandler(404)
def handle_bad_request(error):
return 'Error 404: Page does not exist'
if __name__ == '__main__':
app.run(debug=False, use_reloader=False)