forked from ThisIsQasim/WebGPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
95 lines (84 loc) · 2.79 KB
/
backend.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
import datetime, json
from flask import Flask, render_template, request, redirect, Markup, make_response, url_for
from lib.cors import crossdomain
from lib.setup import rooms, settings
from lib.GPIOSetup import GPIO
from lib.appliance import Appliance
from lib import authentication
app = Flask(__name__)
def updateStates(rooms):
for i, room in enumerate(rooms):
for j, appliance in enumerate(room['Appliances']):
current_appliance = Appliance(appliance)
rooms[i]['Appliances'][j]['State'] = current_appliance.getState()
return rooms
@app.context_processor
def inject_enumerate():
return dict(enumerate=enumerate)
@app.route("/")
@authentication.login_required
def home():
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %I:%M %p")
templateData = {
'title' : 'WebGPIO',
'time': timeString,
'rooms' : updateStates(rooms),
'refresh_rate' : settings['RefreshRate']*1000
}
return render_template('home.html', **templateData)
@app.route("/grid/")
@authentication.login_required
@crossdomain(origin='*')
def grid():
templateData = {
'title' : 'WebGPIO',
'rooms' : updateStates(rooms)
}
return render_template('grid.html', **templateData)
@app.route("/button/<int:room_index>/<int:appliance_index>/")
@authentication.login_required
@crossdomain(origin='*')
def button(room_index, appliance_index):
appliance = Appliance(rooms[room_index]['Appliances'][appliance_index])
appliance.executeAction()
templateData = {
'title' : 'WebGPIO',
'state' : appliance.getState(),
'room_index' : room_index,
'appliance_index' : appliance_index,
'name' : appliance.name
}
return render_template('button.html', **templateData)
@app.route("/login/")
def login():
return render_template('login.html')
@app.route("/authenticate/", methods=['GET', 'POST'])
def auth():
if request.method == 'POST':
password = request.form['password']
token = authentication.generateToken(password)
if token:
expiry_date = datetime.datetime.now() + datetime.timedelta(days=30)
response = make_response(redirect(url_for('.home')))
response.set_cookie('token', token, expires=expiry_date, secure=True, httponly=True, samesite='Lax')
return response
return redirect(url_for('.login'))
@app.route("/logout/")
def logout():
authentication.removeToken()
response = make_response(redirect(url_for('.login')))
response.set_cookie('token', '', expires=0, secure=True, httponly=True, samesite='Lax')
return response
if __name__ == "__main__":
if settings['SSL']['Enabled']:
app.run(host = settings['Host'],
port = settings['Port'],
threaded = settings['Threaded'],
debug = settings['Debug'],
ssl_context = (settings['cerPath'], settings['keyPath']))
else:
app.run(host = settings['Host'],
port = settings['Port'],
threaded = settings['Threaded'],
debug = settings['Debug'])