forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumWebcam.py
614 lines (497 loc) · 23.7 KB
/
terrariumWebcam.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
try:
import thread as _thread
except ImportError as ex:
import _thread
try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
import time
import cv2
import math
import datetime
import os
import glob
import re
import sys
import subprocess
import sys
import shlex
from picamera import PiCamera, PiCameraError
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
from hashlib import md5
from shutil import copyfile
from terrariumUtils import terrariumUtils
from gevent import monkey, sleep
monkey.patch_all()
class terrariumWebcamSource(object):
TYPE = None
# Class defaults
TILE_LOCATION = 'webcam/'
ARCHIVE_LOCATION = TILE_LOCATION + 'archive/'
STORE_LOCATION = '/dev/shm/' + TILE_LOCATION
TILE_SIZE = 256
JPEG_QUALITY = 95
FONT_SIZE = 10
RETRIES = 3
WARM_UP = 2
OFFLINE = 'offline'
ONLINE = 'online'
UPDATE_TIMEOUT = 60
VALID_ROTATIONS = ['0','90','180','270','h','v']
def __init__(self, webcam_id, location, name = '', rotation = '0', width = 640, height = 480, archive = False, archive_light = 'ignore', archive_door = 'ignore', environment = None):
# Variables per webcam
self.raw_image = None
# 'Hidden' variables
self.__max_zoom = 0
self.__last_update = 0
self.__last_archive = 0
self.__running = False
self.__previous_image = None
self.__state = None
self.__environment = environment
# Per webcam config
self.set_location(location)
self.set_name(name)
self.set_resolution(width,height)
self.set_rotation(rotation)
self.set_archive(archive)
self.set_archive_light(archive_light)
self.set_archive_door(archive_door)
if webcam_id is None:
self.__id = md5(self.get_location().encode()).hexdigest()
else:
self.__id = id
if not os.path.isdir(terrariumWebcamSource.STORE_LOCATION + self.get_id()):
os.makedirs(terrariumWebcamSource.STORE_LOCATION + self.get_id())
if not os.path.islink(terrariumWebcamSource.TILE_LOCATION + self.get_id()):
os.symlink(terrariumWebcamSource.STORE_LOCATION + self.get_id(),terrariumWebcamSource.TILE_LOCATION + self.get_id())
logger.info('Initialized %s webcam \'%s\' on location %s' %
(self.get_type(),
self.get_name(),
self.get_location()))
self.update()
def __set_timestamp(self,image):
# Get the image dimensions
source_width, source_height = image.size
# Select font
font = ImageFont.truetype('fonts/DejaVuSans.ttf',terrariumWebcamSource.FONT_SIZE)
# Draw on image
draw = ImageDraw.Draw(image)
# Create black box on the bottom of the image
draw.rectangle([0,source_height-(terrariumWebcamSource.FONT_SIZE+2),source_width,source_height],fill='black')
# Draw the current time stamp on the image
draw.text((1, source_height-(terrariumWebcamSource.FONT_SIZE+1)), self.name + ' @ ' + (datetime.datetime.now()).strftime('%d/%m/%Y %H:%M:%S') ,(255,255,255),font=font)
def __tile_image(self):
starttime = time.time()
# Original width
source_width, source_height = self.raw_image.size
# Calc new square canvas size
longest_side = float(source_width if source_width > source_height else source_height)
max_size = float(math.pow(2,math.ceil(math.log(longest_side,2))))
# Set canvas dimensions
canvas_width = canvas_height = max_size
resize_factor = max_size / longest_side
# Set raw image new dimensions
source_width *= resize_factor
source_height *= resize_factor
# Calculate the max zoom factor
zoom_factor = int(math.log(max_size/terrariumWebcamSource.TILE_SIZE,2))
self.__max_zoom = zoom_factor
logger.debug('Tiling image with source resolution %s, from %sx%s with resize factor %s in %s steps' %
(self.resolution, source_width,source_height,resize_factor, zoom_factor))
# as long as there is a new layer, continue
while zoom_factor >= 0:
# Create black canvas on zoom factor dimensions
logger.debug('Creating new black canvas with dimensions %sx%s' % (canvas_width,canvas_height))
canvas = Image.new("RGB", ((int(round(canvas_width)),int(round(canvas_height)))), "black")
# Scale the raw image to the zoomfactor dimensions
logger.debug('Scale raw image to new canvas size (%sx%s)' % (canvas_width,canvas_height))
source = self.raw_image.resize((int(round(source_width)),int(round(source_height))))
# Set the timestamp on resized image
self.__set_timestamp(source)
# Calculate the center in the canvas for pasting raw image
paste_center_position = (int(round((canvas_width - source_width) / 2)),int(round((canvas_height - source_height) / 2)))
logger.debug('Pasting resized image to center of canvas at position %s' % (paste_center_position,))
canvas.paste(source,paste_center_position)
# Loop over the canvas to create the tiles
logger.debug('Creating the lose tiles with dimensions %sx%s' % (canvas_width, canvas_height,))
for row in range(0,int(math.ceil(canvas_height/terrariumWebcamSource.TILE_SIZE))):
for column in range(0,int(math.ceil(canvas_width/terrariumWebcamSource.TILE_SIZE))):
crop_size = ( int(row*terrariumWebcamSource.TILE_SIZE), int(column*terrariumWebcamSource.TILE_SIZE) ,int((row+1)*terrariumWebcamSource.TILE_SIZE), int((column+1)*terrariumWebcamSource.TILE_SIZE))
logger.debug('Cropping image from position %s' % (crop_size,))
tile = canvas.crop(crop_size)
logger.debug('Saving cropped image to %s' % (terrariumWebcamSource.TILE_LOCATION + self.__id + '_tile_' + str(zoom_factor) + '_' + str(row) + '_' + str(column) + '.jpg',))
tile.save(terrariumWebcamSource.STORE_LOCATION + self.get_id() + '/' + self.__id + '_tile_' + str(zoom_factor) + '_' + str(row) + '_' + str(column) + '.jpg','jpeg',quality=terrariumWebcamSource.JPEG_QUALITY)
logger.debug('Done saving %s' % (terrariumWebcamSource.TILE_LOCATION + self.__id + '_tile_' + str(zoom_factor) + '_' + str(row) + '_' + str(column) + '.jpg',))
# Scale down by 50%
canvas_width /= 2.0
canvas_height /= 2.0
source_width /= 2.0
source_height /= 2.0
zoom_factor -= 1
logger.debug('Done tiling webcam image \'%s\' in %.5f seconds' % (self.get_name(),time.time()-starttime))
def set_offline_image(self):
def draw_text_center(im, draw, text, font, **kwargs):
text_height = text_top = None
linecounter = 0
for line in text:
text_size = draw.textsize(line, font)
if text_height is None:
text_height = len(text) * ( text_size[1])
text_top = (im.size[1] - text_height) / 2
draw.text(
((im.size[0] - text_size[0]) / 2, (text_top + (linecounter * text_height)) / 2),
line, font=font, **kwargs)
linecounter += 1
self.raw_image = Image.open('static/images/webcam_offline.png')
mask = Image.open('static/images/mask_offline.png')
draw = ImageDraw.Draw(mask)
font = ImageFont.truetype('fonts/DejaVuSans.ttf',40)
text = [_('Offline since') + ':',datetime.datetime.now().strftime('%A %d %B %Y'),datetime.datetime.now().strftime('%H:%M:%S')]
draw_text_center(mask,draw,text,font)
mask_width, mask_height = mask.size
source_width, source_height = self.raw_image.size
self.raw_image.paste(mask, (int((source_width/2)-(mask_width/2)),int((source_height/2)-(mask_height/2))), mask)
def get_archive_images(self,prefix = None):
regex = r'(' + terrariumWebcamSource.ARCHIVE_LOCATION + ')\d+/\d+/\d+/([^_]+_archive_)\d+(\..*)'
subst = '\\1/' + str(prefix[0]) + '/' + str(prefix[1]) + '/' + str(prefix[2]) + '/\\2*\\3'
file_filter = re.sub(regex, subst, self.get_raw_image(True)).replace('//','/')
files = glob.glob(file_filter)
files.sort(key=os.path.getmtime,reverse = True)
return files
def archive_image(self):
if self.get_archive() != 'disabled' and \
((self.get_archive_light() == 'ignore' or \
self.get_archive_light() == 'on' and self.__environment is not None and self.__environment.light_on() or \
self.get_archive_light() == 'off' and self.__environment is not None and not self.__environment.light_on()) and \
(self.get_archive_door() == 'ignore' or \
self.get_archive_door() == 'open' and self.__environment is not None and self.__environment.is_door_open() or \
self.get_archive_door() == 'closed' and self.__environment is not None and not self.__environment.is_door_open())):
archive_image = self.get_raw_image(True)
if not os.path.isdir(os.path.dirname(archive_image)):
try:
os.makedirs(os.path.dirname(archive_image))
except Exception as ex:
pass
if self.get_archive() == 'motion':
# https://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/
try:
current_image = cv2.imread(self.get_raw_image())
current_image = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
current_image = cv2.GaussianBlur(current_image, (21, 21), 0)
# Reset previous image. This is needed/happening when resolution of image changes due to rotation or settings update
if self.__previous_image is None or self.__previous_image.shape[0] != current_image.shape[0] or self.__previous_image.shape[1] != current_image.shape[1]:
self.__previous_image = current_image
thresh = cv2.threshold(cv2.absdiff(self.__previous_image, current_image), 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if sys.version_info.major == 2:
# On pyton2 we use OpenCV2. Is different then Python 3 with OpenCV3
(cnts,_) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
elif sys.version_info.major == 3:
# On pyton2 we use OpenCV2. Is different then Python 3 with OpenCV3
(_,cnts,__) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
self.__previous_image = current_image
motion_detected = False
raw_image = cv2.imread(self.get_raw_image())
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) < 500:
continue
motion_detected = True
# compute the bounding box for the contour, draw it on the frame,
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(raw_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
if motion_detected:
cv2.imwrite(archive_image,raw_image)
logger.info('Saved webcam %s image for archive due to motion detection' % (self.get_name(),))
self.__last_update = int(time.time())
self.__environment.notification.message('webcam_motion',self.get_data(),[archive_image])
except Exception as ex:
logger.exception('Error in motion detection for webcam \'%s\' with error message: %s' % (self.get_name(),ex))
elif int(time.time()) - self.__last_archive >= int(self.get_archive()):
copyfile(self.get_raw_image(),archive_image)
logger.info('Saved webcam %s image for archive due to timer interval %s seconds' % (self.get_name(),self.get_archive()))
self.__last_archive = int(time.time())
self.__last_update = int(time.time())
self.__environment.notification.message('webcam_motion',self.get_data(),[archive_image])
def rotate_image(self):
# Rotate image if needed
if '90' == self.get_rotation():
self.raw_image = self.raw_image.transpose(Image.ROTATE_90)
elif '180' == self.get_rotation():
self.raw_image = self.raw_image.transpose(Image.ROTATE_180)
elif '270' == self.get_rotation():
self.raw_image = self.raw_image.transpose(Image.ROTATE_270)
elif 'h' == self.get_rotation():
self.raw_image = self.raw_image.transpose(Image.FLIP_TOP_BOTTOM)
elif 'v' == self.get_rotation():
self.raw_image = self.raw_image.transpose(Image.FLIP_LEFT_RIGHT)
logger.debug('Rotated raw image %s to %s' % (self.get_name(),self.get_rotation()))
def update(self):
starttime = time.time()
if not self.__running and ((int(starttime) - self.get_last_update()) >= terrariumWebcamSource.UPDATE_TIMEOUT):
self.__running = True
logger.debug('Start getting raw image data for webcam \'%s\' from location: \'%s\'' % (self.get_name(),self.get_location(),))
state = terrariumWebcamSource.OFFLINE
for trying in range(0,terrariumWebcamSource.RETRIES):
try:
if self.get_raw_data():
# "Rewind" the stream to the beginning so we can read its content
logger.debug('Resetting raw image %s' % (self.get_name(),))
self.raw_image.seek(0)
# Store image in memory for further processing
self.raw_image = Image.open(self.raw_image)
logger.debug('Loaded raw image %s to memory' % (self.get_name(),))
# Rotate image if needed
self.rotate_image()
# Store copy on disk
self.raw_image.save(self.get_raw_image(),'jpeg',quality=terrariumWebcamSource.JPEG_QUALITY)
logger.debug('Saved raw image %s to disk: %s' % (self.get_name(),self.get_raw_image()))
# Store archive image if needed based on time or motion and door detection
self.archive_image()
# Tile the images for Leaflet.js
if not self.is_live():
self.__tile_image()
# Webcam is online
state = terrariumWebcamSource.ONLINE
break
except Exception as ex:
pass
if self.get_state() != state and state == terrariumWebcamSource.OFFLINE:
logger.warning('Raw image \'%s\' at location %s is not available!' % (self.get_name(),self.get_location(),))
# Set the offline message
self.set_offline_image()
# Store copy on disk
self.raw_image.save(self.get_raw_image(),'jpeg',quality=terrariumWebcamSource.JPEG_QUALITY)
# Tile the images for Leaflet.js
self.__tile_image()
self.__state = state
self.__last_update = int(starttime)
self.__running = False
logger.info('Done updating webcam \'%s\' at location %s in %.5f seconds' % (self.get_name(), self.get_location(),time.time()-starttime))
def get_data(self,archive = False):
data = {'id': self.get_id(),
'location': self.get_location(),
'name': self.get_name(),
'rotation' : self.get_rotation(),
'resolution': self.get_resolution(),
'max_zoom' : self.get_max_zoom(),
'state' : self.get_state(),
'is_live': self.is_live(),
'last_update' : self.get_last_update(),
'image': self.get_raw_image(),
'preview': self.get_preview_image(),
'archive': self.get_archive(),
'archivelight': self.get_archive_light(),
'archivedoor': self.get_archive_door(),
'archive_images' : []
}
if archive:
data['archive_images'] = self.get_archive_images(archive)
return data
def get_id(self):
return self.__id
def get_type(self):
return self.TYPE
def get_name(self):
return self.name
def set_name(self,name):
self.name = name
def get_location(self):
return self.location
def set_location(self,location):
self.location = location
def get_rotation(self):
return self.rotation
def set_rotation(self,rotation):
if rotation in terrariumWebcamSource.VALID_ROTATIONS:
self.rotation = rotation
def set_resolution(self,width,height):
try:
self.resolution = {'width' : int(width),
'height' : int(height)}
except Exception as ex:
self.resolution = {'width' : 640,
'height' : 480}
def get_resolution(self):
return self.resolution
def get_max_zoom(self):
return self.__max_zoom
def get_archive(self):
return self.archive
def set_archive(self,enabled):
self.archive = enabled
def get_archive_light(self):
return self.archive_light_state
def set_archive_light(self,state):
self.archive_light_state = state
def set_archive_door(self,state):
self.archive_door_state = state
def get_archive_door(self):
return self.archive_door_state
def get_state(self):
return self.__state
def get_last_update(self):
return self.__last_update
def get_raw_image(self,archive = False):
image = terrariumWebcamSource.TILE_LOCATION + self.get_id() + '/' + self.get_id() + '_raw.jpg'
if archive:
image = terrariumWebcamSource.ARCHIVE_LOCATION + (datetime.datetime.now()).strftime("%Y/%m/%d") + '/' + self.get_id() + '_archive_' + str(int(time.time())) + '.jpg'
return image
def get_preview_image(self):
if not self.is_live():
return terrariumWebcamSource.TILE_LOCATION + self.get_id() + '_tile_0_0_0.jpg'
def is_live(self):
return False
class terrariumWebcamLiveSource(terrariumWebcamSource):
def __init__(self, webcam_id, location, name = '', rotation = '0', width = 640, height = 480, archive = False, archive_light = 'ignore', archive_door = 'ignore', environment = None):
super(terrariumWebcamLiveSource,self).__init__(webcam_id, location, name, rotation, width, height, archive, archive_light, archive_door, environment)
self.process_id = None
self.start()
def start(self):
if self.process_id is not None:
subprocess.Popen(['/usr/bin/pkill', '-P',str(self.process_id.pid)])
sleep(1)
_thread.start_new_thread(self.run, ())
def run(self):
cmd = shlex.split(self.cmd())
self.process_id = subprocess.Popen(cmd,stdout=DEVNULL,stderr=DEVNULL)
def get_raw_data(self):
logger.debug('Using HLS Live Cam device: %s' % (self.get_location(),))
try:
location = self.get_location()
if 'rpicam_live' == location:
location = 'http://localhost:8090/{}/{}/stream.m3u8'.format(terrariumWebcamSource.TILE_LOCATION, self.get_id())
cmd = 'ffmpeg -hide_banner -loglevel panic -i {} -vframes 1 -f image2 -'.format(location)
cmd = shlex.split(cmd)
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
out, err = proc.communicate()
self.raw_image = BytesIO(out)
return True
except Exception as ex:
print(ex)
logger.exception('Error getting raw HLS Live Cam image from webcam \'%s\' with error message: {}'.format(self.get_name(),ex))
return False
def rotate_image(self):
# Live webcam is remotely rotated
pass
def is_live(self):
return True
class terrariumWebcamRPI(terrariumWebcamSource):
TYPE = 'rpicam'
VALID_SOURCE = '^rpicam$'
INFO_SOURCE = 'rpicam'
def get_raw_data(self):
logger.debug('Using RPICAM')
stream = BytesIO()
try:
with PiCamera(resolution=(self.resolution['width'], self.resolution['height'])) as camera:
logger.debug('Open rpicam')
camera.start_preview()
logger.debug('Wait %s seconds for preview' % (terrariumWebcamSource.WARM_UP,))
time.sleep(terrariumWebcamSource.WARM_UP)
logger.debug('Save rpicam to jpeg')
camera.capture(stream, format='jpeg')
logger.debug('Done creating RPICAM image')
self.raw_image = stream
return True
except PiCameraError:
logger.exception('Error getting raw RPI image from webcam \'%s\' with error message:' % (self.get_name(),))
return False
class terrariumWebcamUSB(terrariumWebcamSource):
TYPE = 'usb'
VALID_SOURCE = '^/dev/video\d+'
INFO_SOURCE = '/dev/video0'
def get_raw_data(self):
logger.debug('Using USB device: %s' % (self.location,))
readok = False
stream = BytesIO()
camera = None
try:
logger.debug('Open USB')
camera = cv2.VideoCapture(int(self.location[10:]))
logger.debug('Set USB height')
camera.set(3, float(self.resolution['width']))
logger.debug('Set USB width')
camera.set(4, float(self.resolution['height']))
logger.debug('Wait %s seconds for preview' % (terrariumWebcamSource.WARM_UP,))
time.sleep(terrariumWebcamSource.WARM_UP)
logger.debug('Save USB to raw data')
readok, image = camera.read()
if readok:
logger.debug('Save USB to jpeg')
jpeg = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
jpeg.save(stream,'JPEG')
logger.debug('Done creating USB image')
self.raw_image = stream
logger.debug('Release USB camera')
camera.release()
logger.debug('Done release USB camera')
return True
except Exception:
logger.exception('Error getting raw USB image from webcam \'%s\' with error message:' % (self.get_name(),))
return False
class terrariumWebcamRemote(terrariumWebcamSource):
TYPE = 'remote'
VALID_SOURCE = '^https?://.*\.[^m3u8]*$'
INFO_SOURCE = 'https://server.com/stream.jpg'
def get_raw_data(self):
logger.debug('Using URL: %s' % (self.location,))
try:
remote_image = terrariumUtils.get_remote_data(self.location)
if remote_image is None:
raise terrariumWebcamRAWUpdateException()
self.raw_image = BytesIO(remote_image)
return True
except terrariumWebcamRAWUpdateException as ex:
logger.warning('Error getting raw online image from webcam \'%s\' with error message: %s' % (self.get_name(),ex))
return False
class terrariumWebcamRPILive(terrariumWebcamLiveSource):
TYPE = 'rpicam_live'
VALID_SOURCE = '^rpicam_live$'
INFO_SOURCE = 'rpicam_live'
def cmd(self):
resolution = self.get_resolution()
return './live_rpicam.sh "{}" {} {} {} {}'.format(self.get_name(),
resolution['width'] if self.get_rotation() not in ['90','270'] else resolution['height'],
resolution['height'] if self.get_rotation() not in ['90','270'] else resolution['width'],
self.get_rotation(),
terrariumWebcamRPILive.STORE_LOCATION + self.get_id())
class terrariumWebcamHLSLive(terrariumWebcamLiveSource):
TYPE = 'hls_live'
VALID_SOURCE = '^https?://.*\.m3u8$'
INFO_SOURCE = 'https://server.com/stream/playlist.m3u8'
def cmd(self):
return './hls-proxy/hlsproxy.py "{}" {} {}'.format(self.get_location(),
'-o',
terrariumWebcamRPILive.STORE_LOCATION + self.get_id())
class terrariumWebcamSourceException(Exception):
'''The entered online webcam source is not known or invalid'''
class terrariumWebcamRAWUpdateException(Exception):
'''The entered online webcam source is not available'''
# Factory class
class terrariumWebcam(object):
SOURCES = [terrariumWebcamRPI,
terrariumWebcamUSB,
terrariumWebcamRemote,
terrariumWebcamRPILive,
terrariumWebcamHLSLive]
def __new__(self,webcam_id, location, name = '', rotation = '0', width = 640, height = 480, archive = False, archive_light = 'ignore', archive_door = 'ignore', environment = None):
for webcam_source in terrariumWebcam.SOURCES:
if re.search(webcam_source.VALID_SOURCE, location, re.IGNORECASE):
return webcam_source(webcam_id,location,name,rotation,width,height,archive,archive_light,archive_door,environment)
raise terrariumWebcamSourceException()
@staticmethod
def valid_sources():
data = {}
for webcam_source in terrariumWebcam.SOURCES:
data[webcam_source.TYPE] = webcam_source.INFO_SOURCE
return data