This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.py
132 lines (118 loc) · 4.24 KB
/
server.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
from flask import Flask, render_template, Response,send_file, request, session, redirect, url_for
import camera
import flask_httpauth
import config
import os
import io
import threading
import time
import hashlib
import logging
import datetime
import ssl
import cloud
app = Flask(__name__)
conf = config.Configuration()
logging.basicConfig(filename='app.log',level=logging.DEBUG)
auth = flask_httpauth.HTTPBasicAuth()
app.secret_key = os.urandom(24)
user = None
online = None
cmra = camera.VideoCamera(conf)
drop = cloud.DropObj(conf)
@auth.get_password
def get_pw(username):
global user
user = username
return conf.get('User')[username]
@auth.hash_password
def hash_pw(password):
return hashlib.sha224(password).hexdigest()
@app.route('/', methods=['GET', 'POST'])
@auth.login_required
def index():
cloud = False
auth = False
error = ''
if request.method == 'POST':
key = request.form['code']
drop.auth(key)
dropbox = '#'
else:
dropbox = drop.get_website()
if conf.get('Cloud')['token'] == 'none':
error = "You need to register your dropbox account first, go to settings tab."
if request.args.get('options') == 'record':
if request.args.has_key('cloud'):
cloud = True
recording = threading.Thread(target=cmra.record,args=[cloud,drop] )
recording.start()
session['options'] = 'record'
return '<IMG id="bg" SRC="/video_feed_record" width="320" height="240" >'
return render_template('index.html', online = online, dropbox = dropbox, error = error)
def gen(camera, save=False, vstart=False):
while True:
frame = camera.get_frame(False,save,vstart)
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n{0}\r\n\r\n'.format(frame))
@app.route('/audio', methods=['POST'])
@auth.login_required
def audio():
file = request.files['edition[audio]']
timestamp = str(time.time())
file.save("audio" + timestamp + ".wav")
cmra.playAudio(timestamp)
return ('', 204)
@app.route('/video_feed')
@auth.login_required
def video_feed():
if 'options' in session:
if session['options'] == 'record':
return Response(gen(cmra,False,True),
mimetype='multipart/x-mixed-replace; boundary=frame')
else:
return Response(gen(cmra),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/video_feed_record')
@auth.login_required
def video_feed2():
return Response(gen(cmra,False,True),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/get_frame')
@auth.login_required
def get_frame():
frame = cmra.get_frame(False)
if request.args.get('options') == 'save':
timestr = time.strftime("%Y%m%d-%H%M%S")
f = open(conf.get('File')['photos'] + 'image' + timestr +'.jpg', 'wb')
f.write(frame)
logging.info('Snapshot taken at ' + str(datetime.datetime.now()))
return ('', 204)
return send_file(io.BytesIO(frame))
@app.route('/stopV')
@auth.login_required
def stopV():
session.pop('options',None)
cmra.endVideo()
return '<IMG id="bg" SRC="/video_feed" width="320" height="240" >'
@app.route('/toggle_online',methods=['POST'])
@auth.login_required
def toggle_online():
global online
if 'submit' in request.form:
cmra.online = True
return redirect(url_for('index'))
sens = int(request.form['sensitive'])
method = request.form['method']
sound = True if 'chk-sound' in request.form else False
mail = True if 'chk-mail' in request.form else False
notify = True if 'chk-not' in request.form else False
online = threading.Thread(target=cmra.start, args=[sens, method, mail, sound, notify])
online.start()
return redirect(url_for('index'))
if __name__ == "__main__":
if conf.boolean('Connection','https'):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(conf.get('Connection')['certificate'], conf.get('Connection')['key'])
app.run(threaded=True, host=conf.get('Connection')['ip'], port=int(conf.get('Connection')['port']) ,ssl_context=context)
else:
app.run(threaded=True,host=conf.get('Connection')['ip'], port=int(conf.get('Connection')['port']))