forked from opengridcc/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.py
executable file
·264 lines (202 loc) · 6.75 KB
/
website.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
import sys
import os
import pandas as pd
import config
from flask import Flask, render_template, send_file, flash, redirect, url_for, safe_join, request, abort
from forms import SearchForm, DownloadForm, EmptyForm
if sys.version_info.major >= 3:
from io import StringIO
else:
from StringIO import StringIO
c = config.Config()
try:
from opengrid.library import houseprint
except ImportError:
sys.path.append(c.get('backend', 'opengrid'))
from opengrid.library import houseprint
app = Flask(__name__)
SECRET_KEY = "secret_key" # TODO add a real key in the config file
app.config.from_object(__name__)
try:
hp = houseprint.Houseprint()
except:
print("Connection failed, loading houseprint from cache")
hp = houseprint.load_houseprint_from_file("cache_hp.hp")
else:
hp.save("cache_hp.hp")
@app.route("/")
@app.route("/index")
def index():
return render_template('index.html')
@app.route("/data")
def data():
devices = hp.get_devices()
devices.sort(key=lambda x: x.key)
return render_template('data.html', fluksos=devices)
@app.route("/development")
def development():
return render_template('development.html')
@app.route("/subscribe")
def subscribe():
return render_template('subscribe.html')
@app.route("/flukso/<fluksoid>")
def flukso(fluksoid):
f = hp.find_device(fluksoid)
if f is None:
abort(404)
sensors = f.get_sensors()
sensors.sort(key=lambda x: x.type)
return render_template(
'flukso.html',
flukso=f,
sensors=sensors
)
@app.route("/sensor/<sensorid>")
def sensor(sensorid):
s = hp.find_sensor(sensorid)
if s is None:
abort(404)
analyses = ['timeseries']
if s.type == 'electricity' and not s.system == 'solar':
analyses.append('standby_horizontal')
analyses.append('standby_vertical')
return render_template(
'sensor.html',
sensor=s,
analyses=analyses
)
@app.route("/standby_horizontal/<sensorid>")
def standby_horizontal(sensorid):
s = hp.find_sensor(sensorid)
filename = 'standby_horizontal_' + sensorid + '.png'
if not figure_exists(filename):
flash('No standby_horizontal graph found for this sensor')
return redirect(url_for('sensor', sensorid=sensorid))
return render_template(
'analysis_image.html',
analysisname='Standby Horizontal',
filename=filename,
sensor=s
)
@app.route("/standby_vertical/<sensorid>")
def standby_vertical(sensorid):
s = hp.find_sensor(sensorid)
filename = 'standby_vertical_{}.png'.format(s.key)
if not figure_exists(filename):
flash('No standby_vertical graph found for this sensor')
return redirect(url_for('sensor', sensorid=sensorid))
return render_template(
'analysis_image.html',
analysisname='Standby Vertical',
filename=filename,
sensor=s)
@app.route("/timeseries/<sensorid>")
def timeseries(sensorid):
s = hp.find_sensor(sensorid)
path = c.get('backend', 'figures')
filename = 'TimeSeries_{}.html'.format(s.key)
file_path = safe_join(path, filename)
if not os.path.exists(file_path):
flash('No timeseries graph found for this sensor')
return redirect(url_for('sensor', sensorid=sensorid))
with open(file_path, "r") as html_graph:
content = html_graph.read()
return render_template(
'analysis_html.html',
analysisname='Time Series',
sensor=s,
content=content
)
@app.route("/figures/<filename>")
def figure(filename):
path = c.get('backend', 'figures')
file_path = safe_join(path, filename)
return send_file(file_path)
def figure_exists(filename):
path = c.get('backend', 'figures')
file_path = safe_join(path, filename)
return os.path.exists(file_path)
@app.route("/search", methods=['GET', 'POST'])
def search():
form = SearchForm()
if request.method == 'POST' and form.validate():
f = hp.find_device(form.search_string.data)
if f is not None: # flukso was found
return redirect(url_for('flukso', fluksoid=f.key))
else:
flash("Sorry, we couldn't find that Fluksometer")
return render_template(
"search.html",
form=form)
@app.route("/download", methods=['GET', 'POST'])
@app.route("/download/<guid>")
def download(guid=None):
form = DownloadForm()
if request.method == 'POST' and form.validate():
s = hp.find_device(form.guid.data)
if s is None:
s = hp.find_sensor(form.guid.data)
if s is None:
flash("ID not found")
else:
try:
# We need to connect and disconnect with tmpo
# to make sure the website doesn't lock access to the sqlite
hp.init_tmpo()
tmpos = hp.get_tmpos()
output = StringIO()
df = s.get_data(
head=pd.Timestamp(form.start.data),
tail=pd.Timestamp(form.end.data),
resample=form.resample.data
)
tmpos.dbcon.close()
except:
# This will happen if another process is currently using the tmpo
flash("Error connecting to the data storage, please try again later")
else:
df.to_csv(output, encoding='utf-8')
output.seek(0)
return send_file(
output,
mimetype="text/csv",
as_attachment=True,
attachment_filename='{}.csv'.format(s.key)
)
if guid is not None:
form.guid.data = guid
return render_template(
'download.html',
form=form
)
@app.route("/issue30", methods=['GET', 'POST'])
def issue30():
form = EmptyForm() # Empty form, only validates the secret token to protect against cross-site scripting
if request.method == 'POST' and form.validate():
try:
hp.init_tmpo()
tmpos = hp.get_tmpos()
hp.sync_tmpos()
tmpos.dbcon.close()
except:
flash("Error syncing TMPO, please try again later")
else:
flash("TMPO Sync Successful")
return render_template(
'issue30.html',
form=form
)
@app.errorhandler(404)
def internal_error(error):
flash('ERROR 404 - Page not found')
return redirect(url_for('index'))
if __name__ == "__main__":
try:
env = c.get('env', 'type')
except:
env = 'prod'
if env == 'dev':
app.run(debug=True)
else:
app.run(debug=False, host='0.0.0.0', port=5000)