김유현

code refactory

......@@ -3,11 +3,11 @@ import os
#import botocore
import time
import datetime
#from django.core import serializers
#from api.models import Video, Record
#from api.serializers import VideoSerializer, RecordSerializer
'''
from django.core import serializers
from api.models import Video, Record
from api.serializers import VideoSerializer, RecordSerializer
from boto3.session import Session
from src.settings import AWS_REGION, S3_ACCESS_URL, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCKET_NAME
'''
......@@ -19,6 +19,7 @@ def record() :
path = '/home/pi/recorded' # save path
state = True
#'''
# rpi setting
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
......@@ -26,42 +27,44 @@ def record() :
#'''
try:
while state :
#target = Record.objects.get(id = 1)
#serializer = RecordSerializer(target, many = False)
#state = serializer.data['recording']
if GPIO.input(PIR_PIN): # motion detected
# take a video
camera.resolution = [320,240]
camera.start_preview()
now = datetime.datetime.now()
vid_name = now.strftime('%Y%m%d-%H%M%S')
vid_path = path + '/' + vid_name + '.h264'
thumbnail_path = path + '/' + vid_name + '.jpg'
camera.start_recording(output = vid_path)
time.sleep(1)
camera.capture(thumbnail_path)
time.sleep(5)
camera.stop_recording()
camera.stop_preview()
# s3 upload
'''
s3 = boto3.client('s3', region_name = 'ap-northeast-2')
s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
while state :
'''
target = Record.objects.get(id = 1)
serializer = RecordSerializer(target, many = False)
state = serializer.data['recording']
'''
if GPIO.input(PIR_PIN): # motion detected
# take a video
camera.resolution = [320, 240]
camera.start_preview()
now = datetime.datetime.now()
vid_name = now.strftime('%Y%m%d-%H%M%S')
vid_path = path + '/' + vid_name + '.h264'
thumbnail_path = path + '/' + vid_name + '.jpg'
camera.start_recording(output=vid_path)
time.sleep(1)
camera.capture(thumbnail_path)
time.sleep(5)
camera.stop_recording()
camera.stop_preview()
uploadVideo = {}
uploadVideo['vid_name'] = vid_name
uploadVideo['created'] = now
serializer = VideoSerializer(data = uploadVideo)
serializer.save()
'''
print(vid_path, "upload success")
os.remove(vid_path)
else:
camera.stop_preview()
# s3 upload
'''
s3 = boto3.client('s3', region_name = 'ap-northeast-2')
s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
uploadVideo = {}
uploadVideo['vid_name'] = vid_name
uploadVideo['created'] = now
serializer = VideoSerializer(data = uploadVideo)
serializer.save()
'''
print(vid_path, "upload success")
os.remove(vid_path)
else:
camera.stop_preview()
except KeyboardInterrupt:
print("quit")
print("quit")
GPIO.cleanup()
......