김유현

Merge branch 'master' into yhkim

1 -# Generated by Django 3.1.2 on 2020-11-13 10:30
2 -
3 -from django.db import migrations, models
4 -import django.utils.timezone
5 -
6 -
7 -class Migration(migrations.Migration):
8 -
9 - initial = True
10 -
11 - dependencies = [
12 - ]
13 -
14 - operations = [
15 - migrations.CreateModel(
16 - name='Device',
17 - fields=[
18 - ('rfid_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
19 - ('created', models.DateTimeField(default=django.utils.timezone.now)),
20 - ],
21 - ),
22 - migrations.CreateModel(
23 - name='Door',
24 - fields=[
25 - ('door_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
26 - ],
27 - ),
28 - migrations.CreateModel(
29 - name='History',
30 - fields=[
31 - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
32 - ('device_name', models.CharField(max_length=255)),
33 - ('ctrtime', models.DateTimeField(default=django.utils.timezone.now)),
34 - ],
35 - ),
36 - migrations.CreateModel(
37 - name='Lock',
38 - fields=[
39 - ('id', models.IntegerField(primary_key=True, serialize=False)),
40 - ('state', models.BooleanField(default=True)),
41 - ],
42 - ),
43 - migrations.CreateModel(
44 - name='Record',
45 - fields=[
46 - ('id', models.IntegerField(primary_key=True, serialize=False)),
47 - ('recording', models.BooleanField(default=True)),
48 - ],
49 - ),
50 - migrations.CreateModel(
51 - name='Video',
52 - fields=[
53 - ('vid_name', models.CharField(max_length=255, primary_key=True, serialize=False)),
54 - ('created', models.DateTimeField(default=django.utils.timezone.now)),
55 - ],
56 - ),
57 - ]
...@@ -13,6 +13,8 @@ class Device(models.Model) : ...@@ -13,6 +13,8 @@ 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 + vid_time = models.CharField(max_length = 255)
17 + thumb = models.CharField(max_length = 255)
16 18
17 class Lock(models.Model) : 19 class Lock(models.Model) :
18 id = models.IntegerField(primary_key = True) 20 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,38 +30,42 @@ def record() : ...@@ -28,38 +30,42 @@ 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]
39 camera.start_preview() 36 camera.start_preview()
37 +
40 now = datetime.datetime.now() 38 now = datetime.datetime.now()
39 + start_time = time.time()
40 +
41 vid_name = now.strftime('%Y%m%d-%H%M%S') 41 vid_name = now.strftime('%Y%m%d-%H%M%S')
42 vid_path = path + '/' + vid_name + '.h264' 42 vid_path = path + '/' + vid_name + '.h264'
43 thumbnail_path = path + '/' + vid_name + '.jpg' 43 thumbnail_path = path + '/' + vid_name + '.jpg'
44 +
44 camera.start_recording(output=vid_path) 45 camera.start_recording(output=vid_path)
45 time.sleep(1) 46 time.sleep(1)
46 camera.capture(thumbnail_path) 47 camera.capture(thumbnail_path)
47 - while GPIO.input(pir_pin): 48 + while GPIO.input(pir_pin) :
48 - print("recoring..") 49 + print("recoring..")
49 - time.sleep(2) 50 + time.sleep(2)
50 camera.stop_recording() 51 camera.stop_recording()
51 camera.stop_preview() 52 camera.stop_preview()
53 +
54 + vid_time = time.strftime("%M:%S", time.gmtime(time.time()-start_time))
52 55
53 # s3 upload 56 # s3 upload
54 ''' 57 '''
55 - s3 = boto3.client('s3', region_name = 'ap-northeast-2') 58 + s3 = boto3.client('s3', region_name = 'ap-northeast-2')
56 - s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name) 59 + s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
60 + s3.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb')
57 61
58 - uploadVideo = {} 62 + uploadVideo = {}
59 - uploadVideo['vid_name'] = vid_name 63 + uploadVideo['vid_name'] = vid_name
60 - uploadVideo['created'] = now 64 + uploadVideo['created'] = now
61 - serializer = VideoSerializer(data = uploadVideo) 65 + uploadVideo['vid_time'] = vid_time
62 - serializer.save() 66 + uploadVideo['thumb'] = S3_ACCESS_URL + vid_name + '_thumb'
67 + serializer = VideoSerializer(data = uploadVideo)
68 + serializer.save()
63 ''' 69 '''
64 print(vid_path, "upload success") 70 print(vid_path, "upload success")
65 os.remove(vid_path) 71 os.remove(vid_path)
......
...@@ -64,7 +64,7 @@ class VideoDownload(APIView) : ...@@ -64,7 +64,7 @@ class VideoDownload(APIView) :
64 if not download_url : 64 if not download_url :
65 raise ObjectDoesNotExist 65 raise ObjectDoesNotExist
66 res = { 66 res = {
67 - 's3_link' : download_url 67 + 's3link' : download_url
68 } # 응답 코드에 보낼 데이터 68 } # 응답 코드에 보낼 데이터
69 return Response(res, status = status.HTTP_200_OK) 69 return Response(res, status = status.HTTP_200_OK)
70 except FieldDoesNotExist as error : 70 except FieldDoesNotExist as error :
...@@ -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({
......