-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamserver.py
327 lines (305 loc) · 11.4 KB
/
camserver.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
#!/usr/bin/env python3
"""
urls:
/ = index:
show state (recording/not recording)
grab image from camera (and display)
show space left on disk
show videos on disk (allow delete of videos)
/camera (all post requests)
grab: grab image (allow parameter setting)
record: start recording (allow parameter setting)
status: get recording status, disk space, available videos
remove: remove video file
config: set grab/record settings
op: (grab/record/status/space/remove/config)
configuration:
(add hostname to video filename) [meh]
(duration) [meh]
(grab arguments) [meh]
(night mode [getting LED control is tricky on the pi3])
duration: allow ms/s/m/h & inf
"""
import datetime
import fcntl
import json
import os
import socket
import struct
import subprocess
import sys
import time
import urllib.parse
import tornado
import tornado.httpserver
import tornado.ioloop
import tornado.web
this_directory = os.path.dirname(os.path.realpath(__file__))
static_directory = os.path.join(this_directory, 'static')
video_directory = os.path.join(this_directory, 'videos')
grab_args_fn = os.path.expanduser('~/.grab_args')
def set_led(state, led=134):
if state not in (0, 1):
raise ValueError("Invalid state: %s (not 0/1)" % state)
b = struct.pack("=8I", 32, 0, 0x38041, 8, 0, led, state, 0)
f = os.open("/dev/vcio", os.O_TRUNC)
try:
fcntl.ioctl(f, 0xC0046400, b)
except Exception as e:
try:
os.close(f)
except Exception:
pass
raise e
class CamSite(tornado.web.RequestHandler):
def get(self):
template = os.path.join(this_directory, 'templates', 'camera.html')
self.render(template)
class CamQuery(tornado.web.RequestHandler):
def is_recording(self):
if self.application.record_process is None:
return False
r = self.application.record_process.poll()
if r is None: # still recording
return True
# finished for some reason, save error
self.error = r
# TODO save stdout or more information?
return False
def return_image(self):
# check if recording
if self.is_recording():
self.set_status(400)
self.write("Bad Request: cannot grab image while recording")
return
cmd = " ".join(["raspistill -t 300 -n -o -", self.application.grab_args])
print("calling: %s" % (cmd, ))
try:
r = subprocess.check_output(cmd.split())
except subprocess.CalledProcessError as e:
self.set_status(500)
self.write("raspistill failed: %s" % (e, ))
return
self.set_header("Content-type", "image/jpeg")
self.write(r)
def get_disk_space(self):
cmd = "df -h %s" % video_directory
output = subprocess.check_output(cmd, shell=True).decode('latin8')
lines = output.strip().split(os.linesep)
l = lines[-1]
ts = l.split()
space, used, avail, perc = ts[1], ts[2], ts[3], ts[4]
return avail
def get_video_filenames(self):
fns = [os.path.splitext(fn) for fn in os.listdir(video_directory)]
return [fn[0] for fn in fns if fn[1] == '.h264']
def get(self):
self.return_image()
def check_filename(self, fn):
for c in '. \\/':
if c in fn:
return True
return False
def post(self):
args = list(self.request.arguments.keys())
kwargs = {k: self.get_argument(k) for k in args}
if 'op' not in kwargs:
self.set_status(400) # bad request
self.write("Bad Request: missing operation[op]")
return
op = kwargs['op']
if op == 'grab':
# grab camera frame return as jpeg
self.return_image()
return
elif op == 'led':
if 'state' not in kwargs:
self.set_status(400)
self.write("Bad Request: missing state [0/1]")
try:
set_led(int(kwargs['state']))
except Exception as e:
self.set_status(500)
self.write(
"Server error: failed to set led to state=%s"
% kwargs['state'])
elif op == 'record':
# record video (if not recording)
if self.is_recording():
self.set_status(400)
self.write("Bad Request: already recording")
return
# get filename from kwargs (or make a new one)
if 'fn' in kwargs:
fn = kwargs['fn']
if self.check_filename(fn):
self.set_status(400)
self.write("Bad Request: invalid filename");
return
else:
fn = '_'.join([
socket.gethostname(),
datetime.datetime.now().strftime('%y%m%d_%H%M%S')])
base_fn = os.path.join(video_directory, fn)
full_fn = base_fn + '.h264'
i = 0
# if already exists, don't overwrite
while os.path.exists(full_fn):
full_fn = "%s_%i.h264" % (base_fn, i)
i += 1
if i > 100:
self.set_status(500)
self.write("Server Error: could not make unique filename")
return
# get duration from kwargs
if 'duration' not in kwargs or len(kwargs['duration']) == 0:
self.set_status(400)
self.write("Bad Request: missing duration")
return
if not kwargs['duration'].isdigit():
d = kwargs['duration']
if d[0] == '-' or d.lower() == 'inf':
v = 0 # continuous
m = 0
elif d[-1] == 's': # seconds
v = kwargs['duration'][:-1]
m = 1000
elif d[-1] == 'm': # minutes
v = kwargs['duration'][:-1]
m = 1000 * 60
elif d[-1] == 'h': # hours
v = kwargs['duration'][:-1]
m = 1000 * 60 * 60
else:
self.set_status(400)
self.write("Bad Request: invalid duration[%s]" % d)
try:
kwargs['duration'] = str(int(v) * m)
except ValueError as e:
self.set_status(400)
self.write(
"Bad Request: invalid duration[%s:%s]" % (d, e))
# start recording
cmd = " ".join([
"raspivid -t %s -n -o %s" % (kwargs['duration'], full_fn),
self.application.grab_args])
print("Starting recording: %s" % (cmd, ))
self.application.record_process = subprocess.Popen(cmd.split())
# return filename, status (recording or not)
s = self.is_recording()
r = {
'fn': fn,
'status': s,
}
if not s:
r['error'] = self.application.error
self.write(json.dumps(r))
elif op == 'stop':
if self.application.record_process is None:
# TODO set status? self.set_status(200)
# self.write("not recording, ignoring")
return
print("killing record process")
# send kill to record_process
self.application.record_process.kill()
# join
# TODO timeout
print("waiting for record process to terminate")
self.application.record_process.wait()
print("setting record process to None")
self.application.record_process = None
elif op == 'shutdown':
cmd = "sudo shutdown -h now"
print("calling: %s" % (cmd, ))
try:
r = subprocess.check_output(cmd.split())
except subprocess.CalledProcessError as e:
self.set_status(500)
self.write("shutdown failed: %s" % (e, ))
return
elif op == 'status':
# get status
status = {
'recording': self.is_recording(),
'space': self.get_disk_space(),
'videos': sorted(self.get_video_filenames()),
'grab_args': self.application.grab_args,
}
if self.application.error is not None:
status['error'] = self.application.error
self.application.error = None
# json encode
self.write(json.dumps(status))
elif op == 'remove':
# remove video filename
if 'fn' not in kwargs:
self.set_status(400)
self.write("Bad Request: missing filename[fn]")
return
# sanitize input
if self.check_filename(kwargs['fn']):
self.set_status(400)
self.write("Bad Request: invalid filename");
return
# remove video_directory + fn
fn = kwargs['fn'] + '.h264'
full_fn = os.path.join(video_directory, fn)
if not os.path.exists(full_fn):
self.set_status(400)
self.write("Bad Request: filename does not exist[%s]" % (fn, ))
return
try:
print("removing: %s" % (full_fn, ))
os.remove(full_fn)
except Exception as e:
self.set_status(500)
self.write("Failed to remove file[%s]: %s" % (fn, e))
return
elif op == 'config':
# configure video/grab settings
if 'args' not in kwargs: # return config instead
self.write(json.dumps(self.application.grab_args))
#self.set_status(400)
#self.write("Bad Request: missing arguments[args]")
return
s = urllib.parse.unquote(kwargs['args'])
print("setting grab_args: %s" % (s, ))
self.application.grab_args = s
# save grab args
print("saving grab_args to %s" % grab_args_fn)
with open(grab_args_fn, 'w') as f:
f.write(self.application.grab_args)
else: # invalid op
self.set_status(400)
self.write("Bad Request: unkonwn operation[op:%s]" % (op, ))
return
class CamApplication(tornado.web.Application):
def __init__(self, **kwargs):
self.record_process = None
self.error = None
# load defaults
if os.path.exists(grab_args_fn):
with open(grab_args_fn, 'r') as f:
self.grab_args = f.readline().strip()
else:
self.grab_args = "-w 100 -h 100"
handlers = [
(r"/", CamSite),
(r"/camera", CamQuery),
(
r"/videos/(.*)",
tornado.web.StaticFileHandler,
{"path": video_directory}),
(
r"/static/(.*)",
tornado.web.StaticFileHandler,
{"path": static_directory}),
]
super().__init__(handlers, **kwargs)
if __name__ == "__main__":
for d in (static_directory, video_directory):
if not os.path.exists(d):
os.makedirs(d)
server = tornado.httpserver.HTTPServer(CamApplication())
server.listen(8888)
tornado.ioloop.IOLoop.current().start()