-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.py
executable file
·176 lines (146 loc) · 5.26 KB
/
web.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
# web.py
# import the Flask class from the flask module
from importlib import import_module
import os, shutil
from flask import Flask, render_template, redirect, \
url_for, request, session, flash, Response
from functools import wraps
from camera_pi import Camera
import finger_recognition
import door_control
import message_control
import visitor_verification_upload
import time
music_dir = '/home/pi/smart_door_system/static/media/audio'
image_dir = '/home/pi/smart_door_system/static/media/image'
music_files_original = [f for f in os.listdir(music_dir) if f.endswith('wav')]
music_files_number_original = len(music_files_original)
data = {'message': 'false'}
def message_b(num):
music_files = [f for f in os.listdir(music_dir) if f.endswith('wav')]
music_files_number = len(music_files)
global data2
if music_files_number != num:
data2 = {'message': 'true'}
else:
data2 = {'message': 'false'}
def message_a():
global data1
if message_control.file_read() == 1:
data1 = {'message': 'true'}
else:
data1 = {'message': 'false'}
# create the application object
app = Flask(__name__)
# config
app.secret_key = 'smart door'
# login required decorator
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('login'))
return wrap
# use decorators to link the function to a url
@app.route('/')
#@login_required
@app.route('/home')
def home():
return render_template('home.html') # render a template
# return "Hello, World!" # return a string
# route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if (request.form['username'] != 'rw564') \
or request.form['password'] != 'rw564':
error = 'Invalid Credentials. Please try again.'
else:
session['logged_in'] = True
global music_files_number_original
message_b(music_files_number_original)
global data2
message_a()
global data1
return render_template('index.html',
title = 'Message',
data1 = data1,
data2 = data2) # render a template
return render_template('login.html', error=error)
@app.route('/logout')
#@login_required
def logout():
session.pop('logged_in', None)
flash('You are logged out now.')
return render_template('logout.html') # render a template
@app.route('/index_1')
#@login_required
def index_1():
global music_files_number_original
message_b(music_files_number_original)
global data2
data1 = {'message': 'false'}
message_control.file_write(0)
a,b,c=visitor_verification_upload.file_read()
return render_template('index_1.html',
data1 = data1,
data2 = data2,
a=a,
b=b,
c=c) # render a template
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/index_2')
#@login_required
def index_2():
music_files = [f for f in os.listdir(music_dir) if f.endswith('wav')]
music_files_number = len(music_files)
image_files = [f for f in os.listdir(image_dir) if f.endswith('jpg')]
image_files_number = len(image_files)
global music_files_number_original
music_files_number_original = music_files_number
data2 = {'message': 'false'}
message_a()
global data1
return render_template("index_2.html",
title = 'Message',
music_files_number = music_files_number,
music_files = music_files,
image_files_number = image_files_number,
image_files = image_files,
data1 = data1,
data2 = data2)
@app.route('/open', methods=['GET','POST'])
def open():
print('Open the door!')
door_control.door_ctrl()
return "Door opening..."
@app.route('/delete_visitor', methods=['GET','POST'])
def delete_visitor():
print('Delete visitor!')
visitor_verification_upload.file_write(0,0,0)
return "Deleting..."
@app.route('/delete_voice', methods=['GET','POST'])
def delete_voice():
print('Delete message!')
shutil.rmtree('/home/pi/smart_door_system/static/media/audio')
os.mkdir('/home/pi/smart_door_system/static/media/audio')
shutil.rmtree('/home/pi/smart_door_system/static/media/image')
os.mkdir('/home/pi/smart_door_system/static/media/image')
return "Delete...message"
# start the server with the 'run()' method
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True, threaded=True)