-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflasketta.py
413 lines (371 loc) · 14.5 KB
/
flasketta.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
from flask import Flask, render_template, request
from mailbox import NotEmptyError
import pymysql
import pandas as pd
import json
import plotly
import numpy as np
import plotly.express as px
from sklearn.neighbors import KNeighborsClassifier
from datetime import datetime
import time
app = Flask(__name__)
class Database: # Database è una classe DAO -> Data Access Object
host = ""
username = ""
password = ""
cursor = None
db = None
def __init__(self, host, username, password, db):
"""
connection to database
:param host:
:param username:
:param password:
:param db:
"""
try:
self.db = pymysql.connect(host=f'{host}', user=f'{username}', password=f'{password}', db=f'{db}')
self.cursor = self.db.cursor()
except:
print("errore di connessione")
def queryRetrieveAll(self, query, like):
"""
this function returns all data regardin an email
:param query:
:param like:
:return:
"""
try:
self.cursor.execute(query, like)
alldata = self.cursor.fetchall()
return alldata
except:
print("errore di prelievo dati")
return None
def checkEmail(self, email):
"""
this function checks if email is already in use
:param email:
:return:
"""
try:
query = f'SELECT * FROM `users` WHERE `email` LIKE %s'
self.cursor.execute(query, email)
alldata = self.cursor.fetchall()
print(alldata)
if alldata == ():
return 'free'
else:
return 'match'
except:
self.db.rollback()
print('nope')
def insert(self, user):
"""
this function registers a new user
:param user:
:return:
"""
query = f'INSERT INTO `users` (`name`, `surname`, `email`, `password`) VALUES (%s,%s,%s,%s)'
try:
self.cursor.execute(query, (
f'{user.nome}', f'{user.surnome}', f'{user.email}', f'{user.password}'))
self.db.commit()
except:
self.db.rollback()
print('database upload error')
def __updatePriv(self, email, role):
"""
Private function to update role
:param email:
:param role:
:return:
"""
query = f' UPDATE users SET `role`= %s WHERE `email`= %s'
try:
self.cursor.execute(query, (role, email))
self.db.commit()
print(f'User: {email} role was updated to: {role}')
except:
self.db.rollback()
print('database upload error')
def insertAction(self, email, action):
"""
This function inserts an action into the database
:param email:
:param action:
:return:
"""
query = f'INSERT INTO `actions` (`email`, `action`) VALUES (%s,%s)'
try:
self.cursor.execute(query, (f'{email}', f'{action}'))
self.db.commit()
except:
self.db.rollback()
print('database action upload error')
def pieGraph(self, role, email, width=200, showlegend=True):
"""
this function graphs pie charts for users, taking their data if they are standard users or all data if they are root
:param role:
:param email:
:param width:
:param showlegend:
:return:
"""
if role == 'root':
query = f'SELECT `action` FROM iot_database.actions WHERE `email` LIKE %s '
actions = np.array(self.queryRetrieveAll(query, '%'))
actions, counts = np.unique(actions, return_counts=True)
fig = px.pie(values=counts, names=actions, width=width)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', showlegend=showlegend)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
else:
query = f'SELECT `action` FROM iot_database.actions WHERE `email` LIKE %s '
actions = np.array(self.queryRetrieveAll(query, email))
actions, counts = np.unique(actions, return_counts=True)
fig = px.pie(values=counts, names=actions, width=width)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', showlegend=showlegend)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
def knn(self, actions):
"""
this function takes all actions and timestamps of said action, and returns a suggestion
:param actions:
:return: suggestion
"""
try:
y = []
for i in range(len(actions)):
y.append(actions[i][1])
labels = np.unique(np.array(y))
y = np.unique(np.array(y), return_inverse=True)[1]
b = []
for i in range(len(actions)):
a = str(actions[i][2])
a = a.split(', ')
d, h = a[0].split(' ')
yea, m, day = d.split('-')
h, min, sec = h.split(':')
# h, m, s = d.split(':')
ts = datetime(int(yea), int(m), int(day), int(h), int(min), int(sec))
timestamp = int(round(ts.timestamp()))
b.append(timestamp)
b = np.array(b)
x = b.reshape(-1, 1)
neigh = KNeighborsClassifier(n_neighbors=35)
neigh.fit(x, y)
# print(time.time())
consnum = neigh.predict([[time.time()]])
consiglio = labels[consnum]
return consiglio
except:
return 'query'
def actionled(self, action,redPin = 17,greenPin = 27,bluePin = 22 ):
"""
this function activates LEDs if the program is uploaded into a raspberry py
:param action: what the action is, customizable
:param redPin: #refer to readme
:param greenPin: #refer to readme
:param bluePin: #refer to readme
:return: //
"""
try:
import RPi.GPIO as GPIO
# set pins as outputs
GPIO.setup(redPin, GPIO.OUT)
GPIO.setup(greenPin, GPIO.OUT)
GPIO.setup(bluePin, GPIO.OUT)
if action.upper() == 'RED':
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.HIGH)
GPIO.output(bluePin, GPIO.HIGH)
if action.upper() == 'BLUE':
GPIO.output(redPin, GPIO.HIGH)
GPIO.output(greenPin, GPIO.HIGH)
GPIO.output(bluePin, GPIO.LOW)
if action.upper() == 'GREEN':
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.HIGH)
GPIO.output(bluePin, GPIO.HIGH)
if action.upper() == 'YELLOW':
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.LOW)
GPIO.output(bluePin, GPIO.HIGH)
if action.upper() == 'LIGHTBLUE':
GPIO.output(redPin, GPIO.HIGH)
GPIO.output(greenPin, GPIO.LOW)
GPIO.output(bluePin, GPIO.LOW)
if action.upper() == 'PURPLE':
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.LOW)
GPIO.output(bluePin, GPIO.HIGH)
if action.upper() == 'WHITE':
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.LOW)
GPIO.output(bluePin, GPIO.LOW)
if action.upper() == 'OFF':
GPIO.output(redPin, GPIO.HIGH)
GPIO.output(greenPin, GPIO.HIGH)
GPIO.output(bluePin, GPIO.HIGH)
except Exception as e:
return e
def login(self, email, password):
"""
this function checks whether a user is registeres
:param email:
:param password:
:return: 1 if the user is not in the database, 777 if the user is admin, 0 if user is normal and reistered
"""
try:
query = f'SELECT * FROM `users` WHERE `email` LIKE %s'
self.cursor.execute(query, email)
alldata = self.cursor.fetchall()
# print(alldata)
if alldata == ():
print('no user with this email')
return 1
else:
print('email match')
if alldata[0][3] == password:
print('password corretta')
if alldata[0][4] == 'root':
print('User is Root')
return 777
return 0
else:
print('password doesnt match')
return 1
except:
self.db.rollback()
print('couldnt verify account')
def close(self):
try:
self.db.close()
except:
print("errore")
class User:
nome = 'None'
surnome = 'None'
email = 'None'
password = 'None'
def __init__(self, surnome, nome, email, password):
"""
this class defines the user
:param surnome:
:param nome:
:param email:
:param password:
"""
self.nome = nome
self.surnome = surnome
self.email = email
self.password = password
@app.route("/")
def index():
return render_template("index.html")
@app.route("/one")
def page1():
return render_template("page1.html")
@app.route("/quer", methods=["post"]) # login / homepage
def querry():
email = request.form.get("email")
password = request.form.get("pswd")
# print(password)
# print(password)
action = 'off'
if request.form.get("query") == 'login':
action = 'off'
else:
action = request.form.get("query") # whats written in the html box
db = Database("localhost", "root", "", "iot_database")
res = db.login(email, password)
db.insertAction(email, action)
consiglio = 'query'
#db.actionled(action) ########################## THIS CODE IS FOR LEDS#######################################
if res == 777:
graphJSON = db.pieGraph('root', '%', showlegend=False)
query = f'SELECT `code`,`email`, `action`, `datetime` FROM iot_database.actions WHERE `email` LIKE %s ORDER BY `code` DESC'
actions = db.queryRetrieveAll(query, '%')
return render_template("homepage.html", kemail=email, kactions=actions, graphJSON=graphJSON, kaction=action,
konsiglio=consiglio)
else:
graphJSON = db.pieGraph('user', email)
query = f"SELECT `code`, `action`, `datetime` FROM iot_database.actions WHERE `email` LIKE %s AND `action` NOT LIKE 'login' ORDER BY `code` DESC"
actions = db.queryRetrieveAll(query, email)
consiglio = db.knn(actions)
return render_template("homepage.html", kemail=email, kactions=actions, graphJSON=graphJSON, kaction=action,
konsiglio=consiglio)
# print(actions)
# neigh = KNeighborsClassifier(n_neighbors=3)
@app.route("/onepoint5", methods=["post"]) # login / homepage
def page1dot5():
email = request.form.get("email")
password = request.form.get("pswd")
# print(password)
db = Database("localhost", "root", "", "iot_database")
res = db.login(email, password)
action = 'off'
consiglio = 'query'
if res == 0:
db.insertAction(email, 'login')
graphJSON = db.pieGraph('user', email, showlegend=False, width=300)
query = f"SELECT `code`, `action`, `datetime` FROM iot_database.actions WHERE `email` LIKE %s AND `action` NOT LIKE 'login' ORDER BY `code` DESC"
actions = db.queryRetrieveAll(query, email)
consiglio = db.knn(actions)
# action=f'<table><tr><th>{actions[0][0]}</th><th>{actions[0][1]}</th><th>{actions[0][2]}</th></tr></table>'
return render_template("homepage.html", kemail=email, kpass=password, kactions=actions, graphJSON=graphJSON,
kaction=action, konsiglio=consiglio)
elif res == 777:
db.insertAction(email, 'login')
queryadmin = f'SELECT `code`, `email`, `action`,`datetime` FROM iot_database.actions WHERE`email` LIKE %s ORDER BY `code` DESC'
actions = db.queryRetrieveAll(queryadmin, '%')
graphJSON2 = db.pieGraph('root', '%', showlegend=False, width=300)
# action=f'<table><tr><th>{actions[0][0]}</th><th>{actions[0][1]}</th><th>{actions[0][2]}</th></tr></table>'
return render_template("homepage.html", kemail=email, kpass=password, kactions=actions, graphJSON=graphJSON2,
kaction=action, konsiglio=consiglio)
else:
db.insertAction(email, 'attemptedLogin')
return render_template("page1.5err.html")
@app.route("/two") # registration
def page2():
return render_template("page2.html")
@app.route('/exita', methods=["post"]) # logout
def page4():
email = request.form.get("email")
db = Database("localhost", "root", "", "iot_database")
db.insertAction(email, 'logout')
return render_template('logout.html')
@app.route("/three", methods=["post"]) # check for registration
def getlogin():
surnome = request.form.get("surrname")
nome = request.form.get("name")
email = request.form.get("email")
password = request.form.get("pswd")
userx = User(surnome, nome, email, password)
db = Database("localhost", "root", "", "iot_database")
if db.checkEmail(email) != 'free':
db.insertAction(email, 'arwue') # attempted registration with used email
return render_template("pagewrongmail.html")
else:
db.insert(userx)
db.insertAction(email, 'Registered')
return render_template("page3.html", kuser=userx)
@app.route('/dash', methods=['post'])
def notdash():
db = Database("localhost", "root", "", "iot_database")
email = request.form.get("email")
graphJSON = db.pieGraph('root', email, 600)
return render_template('plots.html', graphJSON=graphJSON)
if __name__ == "__main__":
app.run(debug=True)
# db = Database("localhost", "marco", "password", "iot_database")
# email= '[email protected]'
# query = f' UPDATE users SET role="root" WHERE email= "[email protected]"'
# db.updatePriv(email,'root')
# actions = db.queryRetrieveAll(query, email)
# print(f'<tr><th>{actions[0][0]}</th><th>{actions[0][1]}</th><th>{actions[0][2]}</th></tr>')
# users = db.getAllUser()
# for u in users:
# print(u)