-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
414 lines (332 loc) · 14.8 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import datetime
import hashlib
import json
from functools import wraps
from flask import Flask, render_template, redirect, url_for, request, flash, abort
from flask_navigation import Navigation
from sqlalchemy import and_, or_
import model
from config import Config
import forms
from flask_migrate import Migrate
from lib.sensors import get_sensors, get_unused_1waddresses
from lib.relays import get_relays, get_unused_ports, get_unused_rules_for_relay
from lib.rules import get_rules
def create_app():
app = Flask('heizung')
app.secret_key = Config.secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = Config.database_url
app.app_context()
return app
app = create_app()
app.app_context().push()
with app.app_context():
model.db.init_app(app)
migrate = Migrate(app, model.db)
nav = Navigation(app)
def gen_nav():
nav.Bar('main', [
nav.Item('Dashboard', 'dashboard'),
nav.Item('Sensors', 'index', items=[nav.Item(s.name, 'sensor', {'sensorID': s.id}) for s in get_sensors()] +
[nav.Item('+ new', 'new_sensor'), nav.Item('+ new remote', 'new_remote_sensor')]
),
nav.Item('Relays', 'index', items=[nav.Item(r.name, 'relay', {'relayID': r.id}) for r in get_relays()] +
[nav.Item('+ new', 'new_relay')]
),
nav.Item('Rules', 'index', items=[nav.Item(r.name, 'rule', {'ruleID': r.id}) for r in get_rules()] +
[nav.Item('+ new', 'new_rule')]
),
nav.Item('User administration', 'user_administration'),
nav.Item('Logout', 'logout'),
])
# decorator for views which need navigation (this is needed to update the navigation without restarting flask)
def with_navigation(func):
@wraps(func)
def decorated_function(*args, **kwargs):
gen_nav()
return func(*args, **kwargs)
return decorated_function
@app.route('/')
def index():
return redirect(url_for('dashboard'))
@app.route('/dashboard/')
@with_navigation
def dashboard():
relays = get_relays()
sensors = get_sensors()
return render_template('dashboard.html', relays=relays, sensors=sensors)
@app.route('/sensor/<int:sensorID>/', methods=['GET', 'POST'])
@with_navigation
def sensor(sensorID):
sensor = model.Sensor.query.get(sensorID)
form = forms.DateRangeSelectForm(request.form)
if form.end.data is None:
form.end.data = datetime.datetime.now()
if form.start.data is None:
if request.args.get('interval') == '24h':
form.start.data = form.end.data - datetime.timedelta(hours=24, )
elif request.args.get('interval') == '3d':
form.start.data = form.end.data - datetime.timedelta(days=3, )
else:
form.start.data = form.end.data - datetime.timedelta(hours=12,)
values = model.SensorValue.query.filter( and_(model.SensorValue.sensor_id==sensorID,
model.SensorValue.time>=form.start.data,
model.SensorValue.time<=form.end.data)).all()
data = []
print(values)
for v in values:
data.append({"y": v.time.strftime("%Y-%m-%d %H:%M:%S"), "item1": str(v.value)} )
jsontemps = json.dumps(data)
return render_template('sensor.html', sensor=sensor, tempdata=jsontemps, form=form)
@app.route('/sensor/<int:sensorID>/delete')
@with_navigation
def del_sensor(sensorID):
sensor = model.Sensor.query.get(sensorID)
model.SensorValue.query.filter_by(sensor_id=sensorID).delete()
model.Condition_sensorCompare.query.filter( or_(model.Condition_sensorCompare.sensor1==sensorID,
model.Condition_sensorCompare.sensor2==sensorID)).delete()
model.Condition_sensorDiffCompare.query.filter(or_(model.Condition_sensorDiffCompare.sensor1 == sensorID,
model.Condition_sensorDiffCompare.sensor2 == sensorID)).delete()
model.Condition_valueCompare.query.filter_by(sensor=sensorID).delete()
if sensor.is_remote:
id = sensor.address1w[4:]
model.RemoteSensor.query.filter_by(id=id).delete()
model.Sensor.query.filter_by(id=sensorID).delete()
model.db.session.commit()
flash('Deletion successfull.')
return redirect(url_for('index'))
@app.route('/sensor/new', methods=['GET', 'POST'])
@with_navigation
def new_sensor():
form = forms.NewSensorForm(request.form)
form.address1w.choices = [(addr, addr) for addr in get_unused_1waddresses()]
if request.method == 'POST' and form.validate():
# form sent with correct data
sensor = model.Sensor(form.address1w.data, form.name.data)
model.db.session.add(sensor)
model.db.session.commit()
flash('Sensor created successfully.')
return render_template('new_sensor.html', form=form)
@app.route('/sensor/new_remote', methods=['GET', 'POST'])
@with_navigation
def new_remote_sensor():
form = forms.NewRemoteSensorForm(request.form)
if request.method == 'POST' and form.validate():
rem_sensor = model.RemoteSensor()
model.db.session.add(rem_sensor)
model.db.session.flush()
sensor = model.Sensor('rem-{}'.format(rem_sensor.id), form.name.data)
model.db.session.add(sensor)
model.db.session.commit()
flash('Sensor created successfully.')
return render_template('new_remote_sensor.html', form=form)
@app.route('/relay/<int:relayID>/', methods=['GET', 'POST'])
@with_navigation
def relay(relayID):
relay = model.Relay.query.get(relayID)
form = forms.NewRuleForRelayForm(request.form)
form.rule.choices = [(r.id, r.name) for r in get_unused_rules_for_relay(relay.id)]
if request.method == 'POST' and form.validate():
# form sent with correct data
relayrule = model.RelayRules(relay.id, form.rule.data)
model.db.session.add(relayrule)
model.db.session.commit()
flash('Rule linked successfully.')
rules = [model.Rule.query.get(rr.rule) for rr in model.RelayRules.query.filter_by(relay=relay.id).all()]
return render_template('relay.html', relay=relay, form=form, rules=rules)
@app.route('/relay/<int:relayID>/delete')
@with_navigation
def del_relay(relayID):
model.RelayRules.query.filter_by(relay=relayID).delete()
model.Relay.query.filter_by(id=relayID).delete()
model.db.session.commit()
flash('Deletion successfull.')
return redirect(url_for('index'))
@app.route('/relay/new', methods=['GET', 'POST'])
@with_navigation
def new_relay():
form = forms.NewRelayForm(request.form)
form.port.choices = [(nr, 'Port {}'.format(nr)) for nr in get_unused_ports()]
if request.method == 'POST' and form.validate():
# form sent with correct data
relay = model.Relay(form.port.data, form.name.data)
model.db.session.add(relay)
model.db.session.commit()
flash('Relay created successfully.')
return render_template('new_relay.html', form=form)
@app.route('/relay/<int:idRelay>/delRoute/<int:idRule>/')
def del_relay_rule(idRelay, idRule):
rr = model.RelayRules.query.filter(
model.RelayRules.relay==idRelay,
model.RelayRules.rule==idRule
).first()
model.db.session.delete(rr)
model.db.session.commit()
return redirect(url_for('relay', relayID=idRelay))
@app.route('/rule/<int:ruleID>/')
@with_navigation
def rule(ruleID):
rule = model.Rule.query.get(ruleID)
return render_template('rule.html', rule=rule)
@app.route('/rule/new', methods=['GET', 'POST'])
@with_navigation
def new_rule():
form = forms.NewRuleForm(request.form)
form.op.choices = [(op.value, str(op)) for op in model.OPERATORS]
if request.method == 'POST' and form.validate():
# form sent with correct data
form.op.data = model.OPERATORS(form.op.data)
rule = model.Rule(form.name.data, form.op.data)
model.db.session.add(rule)
model.db.session.commit()
flash('Rule created successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_rule.html', form=form)
@app.route('/rule/<int:ruleID>/newSensorCondition', methods=['GET', 'POST'])
@with_navigation
def new_sensor_compare_condition(ruleID):
rule = model.Rule.query.get(ruleID)
form = forms.NewSensorConditionForm(request.form)
form.sensor1.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
form.relation.choices = [(r.value, str(r)) for r in model.RELATIONS]
form.sensor2.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
if request.method == 'POST' and form.validate():
# form sent with correct data
form.relation.data = model.RELATIONS(form.relation.data)
c = model.Condition_sensorCompare(rule.id, form.sensor1.data, form.sensor2.data, form.relation.data)
model.db.session.add(c)
model.db.session.commit()
flash('Condition created successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_sensorCondition.html', rule=rule, form=form)
@app.route('/rule/<int:ruleID>/newSensorDiffCondition', methods=['GET', 'POST'])
@with_navigation
def new_sensor_diff_condition(ruleID):
rule = model.Rule.query.get(ruleID)
form = forms.NewSensorDiffConditionForm(request.form)
form.sensor1.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
form.relation.choices = [(r.value, str(r)) for r in model.RELATIONS]
form.sensor2.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
if request.method == 'POST' and form.validate():
# form sent with correct data
form.relation.data = model.RELATIONS(form.relation.data)
c = model.Condition_sensorDiffCompare(rule.id, form.sensor1.data, form.sensor2.data, form.value.data, form.relation.data)
model.db.session.add(c)
model.db.session.commit()
flash('Condition created successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_sensorDiffCondition.html', rule=rule, form=form)
@app.route('/rule/<int:ruleID>/newValueCondition', methods=['GET', 'POST'])
@with_navigation
def new_value_compare_condition(ruleID):
rule = model.Rule.query.get(ruleID)
form = forms.NewValueConditionForm(request.form)
form.sensor.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
form.relation.choices = [(r.value, str(r)) for r in model.RELATIONS]
if request.method == 'POST' and form.validate():
# form sent with correct data
form.relation.data = model.RELATIONS(form.relation.data)
c = model.Condition_valueCompare(rule.id, form.sensor.data, form.value.data, form.relation.data)
model.db.session.add(c)
model.db.session.commit()
flash('Condition created successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_valueCondition.html', rule=rule, form=form)
@app.route('/rule/<int:ruleID>/newTimeCondition', methods=['GET', 'POST'])
@with_navigation
def new_time_compare_condition(ruleID):
rule = model.Rule.query.get(ruleID)
form = forms.NewTimeConditionForm(request.form)
if request.method == 'POST' and form.validate():
# form sent with correct data
c = model.Condition_timeCompare(rule.id, form.start_time.data, form.end_time.data)
model.db.session.add(c)
model.db.session.commit()
flash('Condition created successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_timeCondition.html', rule=rule, form=form)
@app.route('/rule/<int:ruleID>/delete', methods=['GET', 'POST'])
def delete_rule(ruleID):
model.RelayRules.query.filter_by(rule=ruleID).delete()
rule = model.Rule.query.get(ruleID)
model.db.session.delete(rule)
model.db.session.commit()
return redirect(url_for('index'))
@app.route('/rule/<int:ruleID>/edit/vcCondition/<int:conditionID>/', methods=['GET', 'POST'])
@with_navigation
def edit_valueCompareCondition(ruleID, conditionID):
rule = model.Rule.query.get(ruleID)
vc = model.Condition_valueCompare.query.get(conditionID)
form = forms.NewValueConditionForm(request.form)
form.sensor.choices = [(s.id, s.name) for s in model.Sensor.query.all()]
form.relation.choices = [(r.value, str(r)) for r in model.RELATIONS]
if form.sensor.data is None: form.sensor.data = vc.sensor
if form.relation.data is None: form.relation.data = vc.relation.value
if form.value.data == 0: form.value.data = vc.value
if request.method == 'POST' and form.validate():
# form sent with correct data
form.relation.data = model.RELATIONS(form.relation.data)
vc.sensor = form.sensor.data
vc.relation = form.relation.data
vc.value = form.value.data
model.db.session.commit()
flash('Condition updated successfully.')
return redirect(url_for('rule', ruleID=rule.id))
return render_template('new_valueCondition.html', rule=rule, form=form, edit=True)
@app.route('/rule/<int:ruleID>/edit/sdCondition/<int:conditionID>/')
@with_navigation
def edit_sensorDiffCompareCondition(ruleID, conditionID):
return "sensorDiffcondition"
@app.route('/rule/<int:ruleID>/edit/scCondition/<int:conditionID>/')
@with_navigation
def edit_sensorCompareCondition(ruleID, conditionID):
return "sensorCompareCondition"
@app.route('/admin/users/')
@with_navigation
def user_administration():
user = model.User.query.all()
return render_template('user.html', user=user)
@app.route('/admin/users/new/', methods=['GET', 'POST'])
@with_navigation
def new_user():
form = forms.CreateUserForm(request.form)
if request.method == 'POST' and form.validate():
user = model.User(form.name.data, form.password.data)
model.db.session.add(user)
model.db.session.commit()
flash('User successfully created.')
return render_template('new_user.html', form=form)
@app.route('/api/remotesensor/<int:sensorID>', methods=['GET'])
def api_remote_sensor_new_value(sensorID):
sensor = model.Sensor.query.get(sensorID)
signature = request.args.get("signature","")
value = request.args.get("value","")
if sensor.is_remote:
addr = sensor.address1w[4:]
remote_sensor = model.RemoteSensor.query.get(addr)
# check if value is float
try:
float(value)
except ValueError:
return abort(400)
# check signature
s = remote_sensor.key + value
s = hashlib.sha256(s.encode()).hexdigest()
if s != signature:
# signature was wrong
return abort(403)
# save value
sv = model.SensorValue(sensor, float(value))
model.db.session.add(sv)
model.db.session.commit()
return "OK"
else:
return abort(400)
@app.route('/logout/')
def logout():
return "logout"
def get_app():
global app
return app
if __name__ == '__main__':
app.run()