김유현

code refactory

...@@ -3,11 +3,11 @@ import os ...@@ -3,11 +3,11 @@ import os
3 #import botocore 3 #import botocore
4 import time 4 import time
5 import datetime 5 import datetime
6 -#from django.core import serializers
7 6
8 -#from api.models import Video, Record
9 -#from api.serializers import VideoSerializer, RecordSerializer
10 ''' 7 '''
8 +from django.core import serializers
9 +from api.models import Video, Record
10 +from api.serializers import VideoSerializer, RecordSerializer
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 ''' 13 '''
...@@ -19,6 +19,7 @@ def record() : ...@@ -19,6 +19,7 @@ def record() :
19 path = '/home/pi/recorded' # save path 19 path = '/home/pi/recorded' # save path
20 state = True 20 state = True
21 #''' 21 #'''
22 + # rpi setting
22 GPIO.setmode(GPIO.BCM) 23 GPIO.setmode(GPIO.BCM)
23 PIR_PIN = 7 24 PIR_PIN = 7
24 GPIO.setup(PIR_PIN, GPIO.IN) 25 GPIO.setup(PIR_PIN, GPIO.IN)
...@@ -26,42 +27,44 @@ def record() : ...@@ -26,42 +27,44 @@ def record() :
26 #''' 27 #'''
27 28
28 try: 29 try:
29 - while state : 30 + while state :
30 - #target = Record.objects.get(id = 1) 31 + '''
31 - #serializer = RecordSerializer(target, many = False) 32 + target = Record.objects.get(id = 1)
32 - #state = serializer.data['recording'] 33 + serializer = RecordSerializer(target, many = False)
33 - if GPIO.input(PIR_PIN): # motion detected 34 + state = serializer.data['recording']
34 - # take a video 35 + '''
35 - camera.resolution = [320,240] 36 + if GPIO.input(PIR_PIN): # motion detected
36 - camera.start_preview() 37 + # take a video
37 - now = datetime.datetime.now() 38 + camera.resolution = [320, 240]
38 - vid_name = now.strftime('%Y%m%d-%H%M%S') 39 + camera.start_preview()
39 - vid_path = path + '/' + vid_name + '.h264' 40 + now = datetime.datetime.now()
40 - thumbnail_path = path + '/' + vid_name + '.jpg' 41 + vid_name = now.strftime('%Y%m%d-%H%M%S')
41 - camera.start_recording(output = vid_path) 42 + vid_path = path + '/' + vid_name + '.h264'
42 - time.sleep(1) 43 + thumbnail_path = path + '/' + vid_name + '.jpg'
43 - camera.capture(thumbnail_path) 44 + camera.start_recording(output=vid_path)
44 - time.sleep(5) 45 + time.sleep(1)
45 - camera.stop_recording() 46 + camera.capture(thumbnail_path)
46 - camera.stop_preview() 47 + time.sleep(5)
47 - 48 + camera.stop_recording()
48 - # s3 upload 49 + camera.stop_preview()
49 - '''
50 - s3 = boto3.client('s3', region_name = 'ap-northeast-2')
51 - s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
52 50
53 - uploadVideo = {} 51 + # s3 upload
54 - uploadVideo['vid_name'] = vid_name 52 + '''
55 - uploadVideo['created'] = now 53 + s3 = boto3.client('s3', region_name = 'ap-northeast-2')
56 - serializer = VideoSerializer(data = uploadVideo) 54 + s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
57 - serializer.save() 55 +
58 - ''' 56 + uploadVideo = {}
59 - print(vid_path, "upload success") 57 + uploadVideo['vid_name'] = vid_name
60 - os.remove(vid_path) 58 + uploadVideo['created'] = now
61 - else: 59 + serializer = VideoSerializer(data = uploadVideo)
62 - camera.stop_preview() 60 + serializer.save()
61 + '''
62 + print(vid_path, "upload success")
63 + os.remove(vid_path)
64 + else:
65 + camera.stop_preview()
63 except KeyboardInterrupt: 66 except KeyboardInterrupt:
64 - print("quit") 67 + print("quit")
65 GPIO.cleanup() 68 GPIO.cleanup()
66 69
67 70
......