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-20 10:44:43 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
2165416d0c4f5fc03cc211a1c5925c582d7649ed
2165416d
2 parents
8842bc3f
e04a43a5
merge
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
116 additions
and
10 deletions
api/models.py
api/videorecord.py
api/views.py
src/settings.py
videorecord.py
api/models.py
View file @
2165416
from
django.db
import
models
from
django.conf
import
settings
from
django.utils
import
timezone
from
django.contrib.auth.models
import
User
# Create your models here.
class
Door
(
models
.
Model
)
:
...
...
@@ -31,4 +32,4 @@ class Record(models.Model) :
class
AddDevice
(
models
.
Model
)
:
id
=
models
.
IntegerField
(
primary_key
=
True
)
add
=
models
.
BooleanField
(
default
=
False
)
\ No newline at end of file
state
=
models
.
BooleanField
(
default
=
False
)
\ No newline at end of file
...
...
api/videorecord.py
View file @
2165416
...
...
@@ -10,12 +10,12 @@ django.setup()
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
...
...
@@ -61,7 +61,6 @@ def record() :
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
.
upload_file
(
Filename
=
thumbnail_path
,
Bucket
=
S3_STORAGE_BUCKET_NAME
,
Key
=
vid_name
+
'_thumb'
)
...
...
@@ -73,7 +72,6 @@ def record() :
uploadVideo
[
'thumb'
]
=
S3_ACCESS_URL
+
vid_name
+
'_thumb'
serializer
=
VideoSerializer
(
data
=
uploadVideo
)
serializer
.
save
()
'''
print
(
vid_path
,
"upload success"
)
os
.
remove
(
vid_path
)
else
:
...
...
api/views.py
View file @
2165416
...
...
@@ -5,6 +5,7 @@ from django.http import HttpResponse
from
django.core
import
serializers
from
django.core.exceptions
import
FieldDoesNotExist
,
ObjectDoesNotExist
from
django.shortcuts
import
render
from
django.contrib.auth.models
import
User
from
api.videorecord
import
record
from
api.models
import
Video
,
Device
,
RemoteHistory
,
Lock
,
Record
,
Door
,
AddDevice
...
...
@@ -15,19 +16,22 @@ from rest_framework import status
from
rest_framework.views
import
APIView
from
rest_framework.request
import
Request
from
rest_framework.response
import
Response
"""
from
rest_framework.authtoken.models
import
Token
from
boto3.session
import
Session
from
src.settings
import
AWS_REGION
from
src.settings
import
S3_ACCESS_URL
from
src.settings
import
S3_ACCESS_KEY_ID
,
S3_SECRET_ACCESS_KEY
,
S3_STORAGE_BUCKET_NAME
"""
import
time
from
datetime
import
datetime
,
timedelta
import
json
import
uuid
# Create your views here.
#로그인 및 토큰 반환
class
Login
(
APIView
)
:
def
get
(
self
,
request
,
format
=
None
)
:
# request query에 door_id 포함되어있음 : api/auth?door_id=12345
try
:
request_id
=
request
.
GET
.
get
(
'door_id'
,
None
)
...
...
@@ -35,9 +39,13 @@ class Login(APIView) :
raise
FieldDoesNotExist
queryset
=
Door
.
objects
.
filter
(
door_id
=
request_id
)
# door_id 유효성 검색
if
queryset
.
exists
()
:
# 유효할 때
userid
=
uuid
.
uuid4
()
pw
=
uuid
.
uuid4
()
user
=
User
.
objects
.
create_user
(
username
=
str
(
userid
),
password
=
str
(
pw
))
token
=
Token
.
objects
.
create
(
user
=
user
)
res
=
{
'is_available'
:
True
,
'access_token'
:
'토큰'
# 토큰 도입 후 수정 필요
'access_token'
:
token
.
key
}
else
:
res
=
{
...
...
@@ -52,6 +60,18 @@ class Login(APIView) :
'date'
:
datetime
.
now
()
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
'''
def post(self, request, format = None) :
queryset = Door.objects.create(door_id = 12345)
return Response({
'msg' : 'doorid값 삽입 완료',
})
'''
#기기 관련 api
class
Devices
(
APIView
)
:
# 기기 목록 조회
...
...
src/settings.py
View file @
2165416
...
...
@@ -41,6 +41,7 @@ INSTALLED_APPS = [
'api'
,
'rest_framework'
,
'corsheaders'
,
'rest_framework.authtoken'
,
]
MIDDLEWARE
=
[
...
...
@@ -123,7 +124,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL
=
'/static/'
"""
AWS_REGION
=
'ap-northeast-2'
AWS_SETTINGS
=
os
.
path
.
join
(
BASE_DIR
,
'.aws_key.json'
)
awskey
=
json
.
loads
(
open
(
AWS_SETTINGS
)
.
read
())
...
...
@@ -132,4 +133,3 @@ S3_ACCESS_KEY_ID = awskey['aws']['access_key_id']
S3_SECRET_ACCESS_KEY
=
awskey
[
'aws'
][
'secret_access_key'
]
S3_STORAGE_BUCKET_NAME
=
awskey
[
'aws'
][
's3_bucket_name'
]
S3_ACCESS_URL
=
awskey
[
'aws'
][
's3_access_url'
]
"""
\ No newline at end of file
...
...
videorecord.py
0 → 100644
View file @
2165416
import
os
import
boto3
import
botocore
import
time
import
datetime
import
django
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'src.settings'
)
django
.
setup
()
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
target
=
Record
.
objects
.
get
(
id
=
1
)
serializer
=
RecordSerializer
(
target
,
many
=
False
)
state
=
serializer
.
data
[
'recording'
]
#'''
# rpi setting
GPIO
.
setmode
(
GPIO
.
BCM
)
pir_pin
=
7
GPIO
.
setup
(
pir_pin
,
GPIO
.
IN
)
camera
=
PiCamera
()
#'''
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
)
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.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb')
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
)
else
:
camera
.
stop_preview
()
except
KeyboardInterrupt
:
print
(
"quit"
)
GPIO
.
cleanup
()
if
__name__
==
'__main__'
:
record
()
Please
register
or
login
to post a comment