Toggle navigation
Toggle navigation
This project
Loading...
Sign in
cse437_e
/
smartdoorlock-backend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
김유현
2020-11-18 20:06:35 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
9d437fb898470ffe66c36aa4dc2204986bc9a665
9d437fb8
2 parents
580565d0
d219ccdd
Merge branch 'master' into yhkim
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
83 deletions
api/migrations/0001_initial.py
api/models.py
api/videorecord.py
api/views.py
api/migrations/0001_initial.py
deleted
100644 → 0
View file @
580565d
# Generated by Django 3.1.2 on 2020-11-13 10:30
from
django.db
import
migrations
,
models
import
django.utils.timezone
class
Migration
(
migrations
.
Migration
):
initial
=
True
dependencies
=
[
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Device'
,
fields
=
[
(
'rfid_id'
,
models
.
CharField
(
max_length
=
255
,
primary_key
=
True
,
serialize
=
False
)),
(
'created'
,
models
.
DateTimeField
(
default
=
django
.
utils
.
timezone
.
now
)),
],
),
migrations
.
CreateModel
(
name
=
'Door'
,
fields
=
[
(
'door_id'
,
models
.
CharField
(
max_length
=
255
,
primary_key
=
True
,
serialize
=
False
)),
],
),
migrations
.
CreateModel
(
name
=
'History'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'device_name'
,
models
.
CharField
(
max_length
=
255
)),
(
'ctrtime'
,
models
.
DateTimeField
(
default
=
django
.
utils
.
timezone
.
now
)),
],
),
migrations
.
CreateModel
(
name
=
'Lock'
,
fields
=
[
(
'id'
,
models
.
IntegerField
(
primary_key
=
True
,
serialize
=
False
)),
(
'state'
,
models
.
BooleanField
(
default
=
True
)),
],
),
migrations
.
CreateModel
(
name
=
'Record'
,
fields
=
[
(
'id'
,
models
.
IntegerField
(
primary_key
=
True
,
serialize
=
False
)),
(
'recording'
,
models
.
BooleanField
(
default
=
True
)),
],
),
migrations
.
CreateModel
(
name
=
'Video'
,
fields
=
[
(
'vid_name'
,
models
.
CharField
(
max_length
=
255
,
primary_key
=
True
,
serialize
=
False
)),
(
'created'
,
models
.
DateTimeField
(
default
=
django
.
utils
.
timezone
.
now
)),
],
),
]
api/models.py
View file @
9d437fb
...
...
@@ -13,6 +13,8 @@ class Device(models.Model) :
class
Video
(
models
.
Model
)
:
vid_name
=
models
.
CharField
(
max_length
=
255
,
primary_key
=
True
)
created
=
models
.
DateTimeField
(
default
=
timezone
.
now
)
vid_time
=
models
.
CharField
(
max_length
=
255
)
thumb
=
models
.
CharField
(
max_length
=
255
)
class
Lock
(
models
.
Model
)
:
id
=
models
.
IntegerField
(
primary_key
=
True
)
...
...
api/videorecord.py
View file @
9d437fb
import
os
#
import boto3
#
import botocore
import
boto3
import
botocore
import
time
import
datetime
'''
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
'''
import RPi.GPIO as GPIO
from picamera import PiCamera
'''
def
record
()
:
path
=
'/home/pi/recorded'
# save path
state
=
True
target
=
Record
.
objects
.
get
(
id
=
1
)
serializer
=
RecordSerializer
(
target
,
many
=
False
)
state
=
serializer
.
data
[
'recording'
]
#'''
# rpi setting
GPIO
.
setmode
(
GPIO
.
BCM
)
...
...
@@ -28,38 +30,42 @@ 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
()
start_time
=
time
.
time
()
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
)
while
GPIO
.
input
(
pir_pin
):
print
(
"recoring.."
)
time
.
sleep
(
2
)
while
GPIO
.
input
(
pir_pin
)
:
print
(
"recoring.."
)
time
.
sleep
(
2
)
camera
.
stop_recording
()
camera
.
stop_preview
()
vid_time
=
time
.
strftime
(
"
%
M:
%
S"
,
time
.
gmtime
(
time
.
time
()
-
start_time
))
# 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)
s3 = boto3.client('s3', region_name = 'ap-northeast-2')
s3.upload_file(Filename = vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name)
s3.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb')
uploadVideo = {}
uploadVideo['vid_name'] = vid_name
uploadVideo['created'] = now
serializer = VideoSerializer(data = uploadVideo)
serializer.save()
uploadVideo = {}
uploadVideo['vid_name'] = vid_name
uploadVideo['created'] = now
uploadVideo['vid_time'] = vid_time
uploadVideo['thumb'] = S3_ACCESS_URL + vid_name + '_thumb'
serializer = VideoSerializer(data = uploadVideo)
serializer.save()
'''
print
(
vid_path
,
"upload success"
)
os
.
remove
(
vid_path
)
...
...
api/views.py
View file @
9d437fb
...
...
@@ -64,7 +64,7 @@ class VideoDownload(APIView) :
if
not
download_url
:
raise
ObjectDoesNotExist
res
=
{
's3
_
link'
:
download_url
's3link'
:
download_url
}
# 응답 코드에 보낼 데이터
return
Response
(
res
,
status
=
status
.
HTTP_200_OK
)
except
FieldDoesNotExist
as
error
:
...
...
@@ -110,10 +110,6 @@ class Recording(APIView) :
try
:
target
=
Record
.
objects
.
filter
(
id
=
1
)
target
.
update
(
recording
=
request
.
data
[
'recording'
])
if
request
.
data
[
'recording'
]
:
threading
.
Thread
(
target
=
record
)
.
start
()
return
Response
(
status
=
status
.
HTTP_200_OK
)
except
FieldDoesNotExist
as
error
:
return
Response
({
...
...
Please
register
or
login
to post a comment