This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (73 loc) · 2.43 KB
/
main.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
from flask import Flask, jsonify, request, render_template
import sqlite3
from sqlite3 import Error
from datetime import datetime
from threading import Thread
app = Flask(__name__)
def connnectSQL():
try:
conn = sqlite3.connect('database/data.db')
return conn
except Error as e:
print(e)
def generate_data(limit=30):
while True:
conn = connnectSQL()
cur = conn.cursor()
cur.execute("SELECT * FROM access_log ORDER BY id DESC LIMIT ?", (limit,))
rows = cur.fetchall()
conn.close()
data = []
for row in rows:
data.append(row)
return data
def is_card_id_exists(card_id):
conn = connnectSQL()
cur = conn.cursor()
cur.execute("SELECT * FROM card_data WHERE card_id=?", (card_id,))
rows = cur.fetchall()
conn.close()
if len(rows) == 0:
return "false", "UNAUTHORIZED"
else:
card_holder = rows[0][2]
return "true", card_holder
@app.route('/auth', methods=['GET'])
def auth():
card_id = request.args.get('card_id')
if card_id is None or card_id == "":
return jsonify(granted="false", card_holder="UNAUTHORIZED")
granted, card_holder = is_card_id_exists(request.args.get('card_id'))
return jsonify(granted=granted, card_holder=card_holder)
@app.route('/log', methods=['GET'])
def log_access():
time_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
card_holder = request.args.get('card_holder')
if card_holder is None or card_holder == "":
return jsonify(status="failed")
conn = connnectSQL()
cur = conn.cursor()
cur.execute("INSERT INTO access_log (card_holder, datetime) VALUES (?, ?)", (card_holder, time_now))
conn.commit()
conn.close()
return jsonify(status="success")
@app.route('/get-data')
def get_data():
token = request.args.get('token') or request.form.get('token')
try:
count = int(request.args.get('count')) or 30
except Exception:
count = 30
if token != '1234':
return jsonify({'error': 'Invalid password'}), 403
# return jsonify(data[-count:])
return jsonify(generate_data(count))
@app.route('/')
def index():
return render_template('index.html', data=generate_data(30))
if __name__ == '__main__':
data_thread = Thread(target=generate_data)
data_thread.daemon = True
data_thread.start()
print("Dashboard Access Token: 1234")
app.run(debug=True, host='0.0.0.0', port=3000)