-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera_cgi.py
97 lines (86 loc) · 2.37 KB
/
camera_cgi.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
from flask import Flask, request, abort, make_response, render_template, redirect, url_for
from auth import *
from config import *
from timelapse import *
app = Flask(__name__)
app.register_blueprint(timelapse_cgi)
@app.route("/")
@requires_auth
def home():
return render_template("index.html")
@app.route("/videostream.cgi")
@requires_auth
def stream():
return Response(executor.video_stream(), mimetype="multipart/x-mixed-replace;boundary=--BoundaryString")
@app.route("/snapshot.cgi")
@requires_auth
def snapshot():
return Response(executor.snapshot(), mimetype="image/jpeg")
@app.route("/decoder_control.cgi")
@requires_auth
def control():
if "command" not in request.args:
abort(make_response("Invalid grammar, expected command querystring parameter", 400))
cmd = int(request.args.get("command"))
if cmd == 0: # up
executor.up()
elif cmd == 2: # down
executor.down()
elif cmd == 4: # left
executor.left()
elif cmd == 6: # right
executor.right()
elif cmd == 25: # center
executor.center()
elif cmd == 26: # vertical patrol
executor.vertical_patrol()
elif cmd == 29: # horizontal patrol
executor.horizontal_patrol()
elif cmd in [1, 3, 5, 7, 27, 30]:
executor.stop()
else:
abort(make_response("Unknown command", 400))
return "Ok!"
@app.route("/config/set/<key>/<value>")
@requires_auth
def set_config(key, value):
config_set(str(key), str(value))
return "Ok!"
@app.route("/config")
@requires_auth
def config():
d = shelve.open(CONFIG_FILE)
exp = []
for k, v in d.iteritems():
if type(v) is int:
exp.append((k, v, 'number'))
elif type(v) is bool:
exp.append((k, v, 'boolean'))
else:
exp.append((k, v, 'string'))
d.close()
tmpl = render_template("config.html",
config = sorted(exp, key=lambda v: v[0]))
return tmpl
@app.route("/config/set", methods=['POST'])
@requires_auth
def set_config_all():
d = shelve.open(CONFIG_FILE)
for key in request.form.keys():
val = request.form[key]
if d.has_key(key):
cval = d[key]
if type(cval) is int:
val = int(val)
elif type(cval) is bool:
val = True if val != '0' else False
d[key] = val
d.close()
return redirect(url_for('config'))
if __name__ == "__main__":
app.run(
host=config_get("listen_host"),
port=config_get("listen_port"),
debug=True,
threaded=True,
use_reloader=False)