-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
243 lines (199 loc) · 5.86 KB
/
Copy pathserver.py
File metadata and controls
243 lines (199 loc) · 5.86 KB
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
import datetime
import gevent
import gevent.monkey
from gevent.pywsgi import WSGIServer
import MySQLdb
gevent.monkey.patch_all()
from flask import Flask, request, Response, render_template
from gpiozero import LED
led = LED(26)
def ledOn():
led.on()
return "Room Light is on."
def ledOff():
led.off()
return "Room Light is off"
def ledStatus():
if led.is_lit:
return 'On'
else:
return 'Off'
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/readLED/")
def readPin():
response = ledStatus()
templateData = {
'title' : 'Status of LED: ',
'response' : response
}
return render_template('pin.html', **templateData)
@app.route("/writeLED/<status>")
def writePin(status):
if status == 'On':
response = ledOn()
else:
response = ledOff()
templateData = {
'title' : 'Status of LED',
'response' : response
}
return render_template('pin.html', **templateData)
@app.route("/viewAtmosphere/")
@app.route("/viewAtmosphere/realtime/")
def viewAtmosphereRT():
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
query = "SELECT datetimevalue, temperature, humidity FROM atmosphere ORDER BY datetimevalue DESC LIMIT 10"
curs.execute(query)
data = []
for (datetimevalue, temperature, humidity) in curs:
d = []
d.append("{:%H:%M:%S}".format(datetimevalue))
d.append(temperature)
d.append(humidity)
data.append(d)
print(data)
data_reversed = data[::-1]
return render_template('atmosphere.html', data=data_reversed)
@app.route("/viewAtmosphere/historic/")
def viewAtmosphereHistoricRouter():
return render_template('router.html')
@app.route("/viewAtmosphere/historic/<date>")
def viewAtmosphereHistoric(date):
date = str(date)
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL dacon.tabase")
query = "SELECT datetimevalue, temperature, humidity FROM atmosphere WHERE DATE(datetimevalue) = '%s' " % (date)
print(query)
curs.execute(query)
data = []
for (datetimevalue, temperature, humidity) in curs:
d = []
d.append("{:%H:%M:%S}".format(datetimevalue))
d.append(temperature)
d.append(humidity)
data.append(d)
print(data)
data_reversed = data[::-1]
return render_template('atmosphere.html', data=data_reversed)
@app.route("/viewUserLogs/")
def viewUserLogs():
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL dacon.tabase")
query = "SELECT u.Username, l.DateTime FROM Users u INNER JOIN UserLog l ON u.UserID = l.UserID;"
print(query)
curs.execute(query)
data = []
for (username, datetime) in curs:
d = []
d.append(username)
d.append(datetime)
data.append(d)
print(data)
data_reversed = data[::-1]
return render_template('userlog.html', data=data_reversed)
@app.route("/viewFailedEntryLogs/")
def viewFailedEntryLogs():
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL dacon.tabase")
query = "SELECT DateTime, PictureID from FailedEntryLog;"
curs.execute(query)
data = []
for (datetime, pictureid) in curs:
d = []
d.append(datetime)
d.append(pictureid)
data.append(d)
print(data)
data_reversed = data[::-1]
return render_template('failedlogs.html', data=data_reversed)
@app.route("/changePassword/")
def changePassword():
return render_template('changepassword.html')
@app.route("/changePassword/<password>")
def changePasswordDB(password):
password = int(password)
print(password)
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
try:
sql = "UPDATE Security SET Passcode = %d WHERE ID = 1;" % (password)
print(sql)
curs.execute(sql)
db.commit()
print('\nDatabase Modified')
except MySQLdb.Error as e:
print(e)
return render_template('passwordchanged.html')
@app.route("/registerFace/")
def registerFaceForm():
return render_template('facescan.html')
@app.route('/registerFace/<userid>/<username>')
def registerFace(userid, username):
from faceid import face
face(userid)
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
try:
sql = "INSERT into Users(UserID, Username) VALUES ('%d', '%s')" % (int(userid), str(username))
curs.execute(sql)
db.commit()
print('\nDatabase Modified')
except MySQLdb.Error as e:
print(e)
return render_template('faceregistered.html')
@app.route("/changeFaceUnlockConfidence/")
def changeConfidence():
return render_template('changeconfidence.html')
@app.route("/changeFaceUnlockConfidence/<value>")
def changeConfidenceDB(value):
value = int(value)
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
try:
sql = "UPDATE Security SET FaceScanConfidence = %d WHERE ID = 1;" % (value)
print(sql)
curs.execute(sql)
db.commit()
print('\nDatabase Modified')
except MySQLdb.Error as e:
print(e)
return render_template('confidencechanged.html')
if __name__ == '__main__':
try:
http_server = WSGIServer(('0.0.0.0', 8001), app)
app.debug = True
http_server.serve_forever()
except:
print("Exception")