-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
116 lines (101 loc) · 3.75 KB
/
run.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
from flask import request, render_template
from flask.ext.api import FlaskAPI, status
from flask import jsonify
from lights.utils import *
app = FlaskAPI(__name__)
# endpoint is responsible for turning on/off lights and by default returns stats
@app.route('/switch_lights', methods=['GET', 'POST'])
def switch_lights():
con = sqlite3.connect("database.db")
cur = con.cursor()
if request.method == 'POST':
command = request.args.get("command")
if command != "turn_on" and command != "turn_off":
return jsonify({
'error': 'performing bad query'
}), status.HTTP_404_NOT_FOUND
try:
if command == "turn_on":
cur.execute("UPDATE turning SET turn_on=? WHERE id = 0", (1,))
else:
cur.execute("UPDATE turning SET turn_on=? WHERE id = 0", (2,))
con.commit()
except:
con.rollback()
return jsonify({
'error': 'error occurred during inserting into database'
}), status.HTTP_404_NOT_FOUND
data = cur.execute('SELECT luminosity, people_count, time, last_status FROM stats WHERE id = 0')
lum, people_count, time, status = data.fetchone()
con.close()
return jsonify({
'luminosity': lum,
'people_count': people_count,
'time': time
'status': status
})
# endpoint creates new user
@app.route('/create_user', methods=['POST'])
def new_user():
if request.method == 'POST':
nm = request.args.get("name")
mac = request.args.get("mac")
# validate given arguments
if nm is None or mac is None:
return jsonify({
'performed': False,
'error': 'one of arguments is not defined'
}), status.HTTP_404_NOT_FOUND
if len(re.findall(p, mac)) == 0:
return jsonify({
'performed': False,
'error': 'given mac is not valid'
}), status.HTTP_404_NOT_FOUND
con = None
try:
con = sqlite3.connect("database.db")
cur = con.cursor()
cur.execute("INSERT INTO users (name,mac) VALUES(?, ?)", (nm, mac))
con.commit()
except:
con.rollback()
return jsonify({
'performed': False,
'error': 'error occurred during inserting into database'
}), status.HTTP_404_NOT_FOUND
finally:
con.close()
return jsonify({
'performed': True
})
# TODO check database here and if there is a change render change
@app.route("/message")
def show_message():
con = None
try:
con = sqlite3.connect("database.db")
cur = con.cursor()
cur.execute("SELECT show FROM show_message")
if cur.fetchone()[0] == 'yes':
cur.execute("SELECT seconds, name, message "
"FROM show_message LEFT JOIN users ON show_message.mac = users.mac")
remaining_time, user, message = cur.fetchone()
remaining_time = int(remaining_time) - 1
if remaining_time == 0:
cur.execute("UPDATE show_message SET seconds=?, show=? WHERE id = 0", (remaining_time, 'no'))
else:
cur.execute("UPDATE show_message SET seconds=?", (remaining_time,))
con.commit()
return render_template('message.html', user=user)
else:
return render_template('message.html')
except:
con.rollback()
return jsonify({
'performed': False,
'error': 'error occurred during inserting into database'
}), status.HTTP_404_NOT_FOUND
finally:
con.close()
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=False)