Showing
3 changed files
with
21 additions
and
25 deletions
... | @@ -13,6 +13,7 @@ class Device(models.Model) : | ... | @@ -13,6 +13,7 @@ class Device(models.Model) : |
13 | class Video(models.Model) : | 13 | class Video(models.Model) : |
14 | vid_name = models.CharField(max_length = 255, primary_key = True) | 14 | vid_name = models.CharField(max_length = 255, primary_key = True) |
15 | created = models.DateTimeField(default = timezone.now) | 15 | created = models.DateTimeField(default = timezone.now) |
16 | + thumb = models.CharField(max_length = 255) | ||
16 | 17 | ||
17 | class Lock(models.Model) : | 18 | class Lock(models.Model) : |
18 | id = models.IntegerField(primary_key = True) | 19 | id = models.IntegerField(primary_key = True) | ... | ... |
1 | import os | 1 | import os |
2 | -#import boto3 | 2 | +import boto3 |
3 | -#import botocore | 3 | +import botocore |
4 | import time | 4 | import time |
5 | import datetime | 5 | import datetime |
6 | 6 | ||
7 | -''' | ||
8 | from django.core import serializers | 7 | from django.core import serializers |
9 | from api.models import Video, Record | 8 | from api.models import Video, Record |
10 | from api.serializers import VideoSerializer, RecordSerializer | 9 | from api.serializers import VideoSerializer, RecordSerializer |
10 | +''' | ||
11 | from boto3.session import Session | 11 | from boto3.session import Session |
12 | from src.settings import AWS_REGION, S3_ACCESS_URL, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCKET_NAME | 12 | from src.settings import AWS_REGION, S3_ACCESS_URL, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCKET_NAME |
13 | -''' | ||
14 | import RPi.GPIO as GPIO | 13 | import RPi.GPIO as GPIO |
15 | from picamera import PiCamera | 14 | from picamera import PiCamera |
16 | - | 15 | +''' |
17 | 16 | ||
18 | def record() : | 17 | def record() : |
19 | path = '/home/pi/recorded' # save path | 18 | path = '/home/pi/recorded' # save path |
20 | - state = True | 19 | + |
20 | + target = Record.objects.get(id = 1) | ||
21 | + serializer = RecordSerializer(target, many = False) | ||
22 | + state = serializer.data['recording'] | ||
21 | #''' | 23 | #''' |
22 | # rpi setting | 24 | # rpi setting |
23 | GPIO.setmode(GPIO.BCM) | 25 | GPIO.setmode(GPIO.BCM) |
... | @@ -28,11 +30,6 @@ def record() : | ... | @@ -28,11 +30,6 @@ def record() : |
28 | 30 | ||
29 | try: | 31 | try: |
30 | while state : | 32 | while state : |
31 | - ''' | ||
32 | - target = Record.objects.get(id = 1) | ||
33 | - serializer = RecordSerializer(target, many = False) | ||
34 | - state = serializer.data['recording'] | ||
35 | - ''' | ||
36 | if GPIO.input(pir_pin): # motion detected | 33 | if GPIO.input(pir_pin): # motion detected |
37 | # take a video | 34 | # take a video |
38 | camera.resolution = [320, 240] | 35 | camera.resolution = [320, 240] |
... | @@ -44,22 +41,24 @@ def record() : | ... | @@ -44,22 +41,24 @@ def record() : |
44 | camera.start_recording(output=vid_path) | 41 | camera.start_recording(output=vid_path) |
45 | time.sleep(1) | 42 | time.sleep(1) |
46 | camera.capture(thumbnail_path) | 43 | camera.capture(thumbnail_path) |
47 | - while GPIO.input(pir_pin): | 44 | + while GPIO.input(pir_pin) : |
48 | - print("recoring..") | 45 | + print("recoring..") |
49 | - time.sleep(2) | 46 | + time.sleep(2) |
50 | camera.stop_recording() | 47 | camera.stop_recording() |
51 | camera.stop_preview() | 48 | camera.stop_preview() |
52 | 49 | ||
53 | # s3 upload | 50 | # s3 upload |
54 | ''' | 51 | ''' |
55 | - s3 = boto3.client('s3', region_name = 'ap-northeast-2') | 52 | + s3 = boto3.client('s3', region_name = 'ap-northeast-2') |
56 | - s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name) | 53 | + s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name) |
54 | + s3.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb') | ||
57 | 55 | ||
58 | - uploadVideo = {} | 56 | + uploadVideo = {} |
59 | - uploadVideo['vid_name'] = vid_name | 57 | + uploadVideo['vid_name'] = vid_name |
60 | - uploadVideo['created'] = now | 58 | + uploadVideo['created'] = now |
61 | - serializer = VideoSerializer(data = uploadVideo) | 59 | + uploadVideo['thumb'] = 'http://' + S3_STORAGE_BUCKET_NAME + 's3.ap-northeast-2.amazonaws.com/' + vid_name + '_thumb' |
62 | - serializer.save() | 60 | + serializer = VideoSerializer(data = uploadVideo) |
61 | + serializer.save() | ||
63 | ''' | 62 | ''' |
64 | print(vid_path, "upload success") | 63 | print(vid_path, "upload success") |
65 | os.remove(vid_path) | 64 | os.remove(vid_path) | ... | ... |
... | @@ -110,10 +110,6 @@ class Recording(APIView) : | ... | @@ -110,10 +110,6 @@ class Recording(APIView) : |
110 | try : | 110 | try : |
111 | target = Record.objects.filter(id = 1) | 111 | target = Record.objects.filter(id = 1) |
112 | target.update(recording = request.data['recording']) | 112 | target.update(recording = request.data['recording']) |
113 | - | ||
114 | - if request.data['recording'] : | ||
115 | - threading.Thread(target=record).start() | ||
116 | - | ||
117 | return Response(status = status.HTTP_200_OK) | 113 | return Response(status = status.HTTP_200_OK) |
118 | except FieldDoesNotExist as error : | 114 | except FieldDoesNotExist as error : |
119 | return Response({ | 115 | return Response({ | ... | ... |
-
Please register or login to post a comment