|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# Nat Morris (c) 2017 |
|
3
|
|
|
"""CCTV GIF buffer - WebServer.""" |
|
4
|
|
|
|
|
5
|
|
|
import cherrypy |
|
6
|
|
|
import copy |
|
7
|
|
|
import imageio |
|
8
|
|
|
import io |
|
9
|
|
|
from cctvgifbuffer import version |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class WebServer(object): |
|
13
|
|
|
|
|
14
|
|
|
service = None |
|
15
|
|
|
|
|
16
|
|
|
def __init__(self, service): |
|
17
|
|
|
self.service = service |
|
18
|
|
|
|
|
19
|
|
|
@cherrypy.expose |
|
20
|
|
|
def index(self): |
|
21
|
|
|
return "cctv-gif-buffer v%s" % (version()) |
|
22
|
|
|
|
|
23
|
|
|
@cherrypy.expose |
|
24
|
|
|
def gif(self, camera, duration, interval): |
|
25
|
|
|
# sanitize the parameters |
|
26
|
|
|
if camera not in self.service.cameras.keys(): |
|
27
|
|
|
raise cherrypy.HTTPError(404, "Camera not found") |
|
28
|
|
|
duration = int(duration) |
|
29
|
|
|
if duration != 60: |
|
30
|
|
|
raise cherrypy.HTTPError(500, "Only 60 second duration is currently supported") |
|
31
|
|
|
interval = float(interval) |
|
32
|
|
|
valid_intervals = [0.25, 0.5, 1] |
|
33
|
|
|
if interval not in valid_intervals: |
|
34
|
|
|
raise cherrypy.HTTPError(500, "Support frame intervals are %s" % (valid_intervals)) |
|
35
|
|
|
camobject = self.service.cameras[camera] |
|
36
|
|
|
# obtain a temporary lock to copy the buffer |
|
37
|
|
|
with camobject["lock"]: |
|
38
|
|
|
x = copy.copy(camobject["buffer"]) |
|
39
|
|
|
# generate the GIF |
|
40
|
|
|
outbytes = imageio.mimsave(imageio.RETURN_BYTES, x, 'GIF', duration=interval) |
|
41
|
|
|
# serve it to the user |
|
42
|
|
|
cherrypy.response.headers['Content-Type'] = 'image/gif' |
|
43
|
|
|
return io.BytesIO(outbytes) |
|
44
|
|
|
|
|
45
|
|
|
def start(self): |
|
46
|
|
|
cherrypy.config.update({'server.socket_port': 8080, |
|
47
|
|
|
'server.socket_host': '0.0.0.0', |
|
48
|
|
|
'engine.autoreload.on': False}) |
|
49
|
|
|
cherrypy.quickstart(self) |
|
50
|
|
|
|