Add RFIDProcess, RemoteProcess & Add 기기추가요청api, 원격잠금해제api
Showing
8 changed files
with
163 additions
and
19 deletions
1 | from django.contrib import admin | 1 | from django.contrib import admin |
2 | -from .models import Video, Device, History, Lock, Record, Door | 2 | +from .models import Video, Device, RemoteHistory, Lock, Record, Door |
3 | # Register your models here. | 3 | # Register your models here. |
4 | admin.site.register(Door) | 4 | admin.site.register(Door) |
5 | admin.site.register(Video) | 5 | admin.site.register(Video) |
6 | admin.site.register(Device) | 6 | admin.site.register(Device) |
7 | -admin.site.register(History) | 7 | +admin.site.register(RemoteHistory) |
8 | admin.site.register(Lock) | 8 | admin.site.register(Lock) |
9 | admin.site.register(Record) | 9 | admin.site.register(Record) |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -15,7 +15,8 @@ class Migration(migrations.Migration): | ... | @@ -15,7 +15,8 @@ class Migration(migrations.Migration): |
15 | migrations.CreateModel( | 15 | migrations.CreateModel( |
16 | name='Device', | 16 | name='Device', |
17 | fields=[ | 17 | fields=[ |
18 | - ('rfid_id', models.CharField(max_length=255, primary_key=True, serialize=False)), | 18 | + ('device_id', models.AutoField(primary_key=True, serialize=False)), |
19 | + ('rfid_id', models.CharField(max_length=255)), | ||
19 | ('created', models.DateTimeField(default=django.utils.timezone.now)), | 20 | ('created', models.DateTimeField(default=django.utils.timezone.now)), |
20 | ], | 21 | ], |
21 | ), | 22 | ), |
... | @@ -26,14 +27,6 @@ class Migration(migrations.Migration): | ... | @@ -26,14 +27,6 @@ class Migration(migrations.Migration): |
26 | ], | 27 | ], |
27 | ), | 28 | ), |
28 | migrations.CreateModel( | 29 | 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', | 30 | name='Lock', |
38 | fields=[ | 31 | fields=[ |
39 | ('id', models.IntegerField(primary_key=True, serialize=False)), | 32 | ('id', models.IntegerField(primary_key=True, serialize=False)), |
... | @@ -48,6 +41,14 @@ class Migration(migrations.Migration): | ... | @@ -48,6 +41,14 @@ class Migration(migrations.Migration): |
48 | ], | 41 | ], |
49 | ), | 42 | ), |
50 | migrations.CreateModel( | 43 | migrations.CreateModel( |
44 | + name='RemoteHistory', | ||
45 | + fields=[ | ||
46 | + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
47 | + ('device_name', models.CharField(max_length=255)), | ||
48 | + ('ctrtime', models.DateTimeField(default=django.utils.timezone.now)), | ||
49 | + ], | ||
50 | + ), | ||
51 | + migrations.CreateModel( | ||
51 | name='Video', | 52 | name='Video', |
52 | fields=[ | 53 | fields=[ |
53 | ('vid_name', models.CharField(max_length=255, primary_key=True, serialize=False)), | 54 | ('vid_name', models.CharField(max_length=255, primary_key=True, serialize=False)), | ... | ... |
... | @@ -7,7 +7,8 @@ class Door(models.Model) : | ... | @@ -7,7 +7,8 @@ class Door(models.Model) : |
7 | door_id = models.CharField(max_length = 255, primary_key = True) | 7 | door_id = models.CharField(max_length = 255, primary_key = True) |
8 | 8 | ||
9 | class Device(models.Model) : | 9 | class Device(models.Model) : |
10 | - rfid_id = models.CharField(max_length = 255, primary_key = True) | 10 | + device_id = models.AutoField(primary_key=True) |
11 | + rfid_id = models.CharField(max_length = 255) | ||
11 | created = models.DateTimeField(default = timezone.now) | 12 | created = models.DateTimeField(default = timezone.now) |
12 | 13 | ||
13 | class Video(models.Model) : | 14 | class Video(models.Model) : |
... | @@ -20,9 +21,9 @@ class Lock(models.Model) : | ... | @@ -20,9 +21,9 @@ class Lock(models.Model) : |
20 | id = models.IntegerField(primary_key = True) | 21 | id = models.IntegerField(primary_key = True) |
21 | state = models.BooleanField(default = True) | 22 | state = models.BooleanField(default = True) |
22 | 23 | ||
23 | -class History(models.Model) : | 24 | +class RemoteHistory(models.Model) : |
24 | device_name = models.CharField(max_length = 255) | 25 | device_name = models.CharField(max_length = 255) |
25 | - ctrtime = models.DateTimeField(default = timezone.now) | 26 | + created = models.DateTimeField(default = timezone.now) |
26 | 27 | ||
27 | class Record(models.Model) : | 28 | class Record(models.Model) : |
28 | id = models.IntegerField(primary_key = True) | 29 | id = models.IntegerField(primary_key = True) | ... | ... |
1 | -from api.models import Device, Video, Lock, History, Record, Door, AddDevice | 1 | +from api.models import Device, Video, Lock, RemoteHistory, Record, Door, AddDevice |
2 | from rest_framework import serializers | 2 | from rest_framework import serializers |
3 | 3 | ||
4 | class DoorSerializer(serializers.ModelSerializer) : | 4 | class DoorSerializer(serializers.ModelSerializer) : |
... | @@ -16,9 +16,9 @@ class VideoSerializer(serializers.ModelSerializer) : | ... | @@ -16,9 +16,9 @@ class VideoSerializer(serializers.ModelSerializer) : |
16 | model = Video | 16 | model = Video |
17 | fields = '__all__' | 17 | fields = '__all__' |
18 | 18 | ||
19 | -class HistorySerializer(serializers.ModelSerializer) : | 19 | +class RemoteHistorySerializer(serializers.ModelSerializer) : |
20 | class Meta : | 20 | class Meta : |
21 | - model = History | 21 | + model = RemoteHistory |
22 | fields = '__all__' | 22 | fields = '__all__' |
23 | 23 | ||
24 | class RecordSerializer(serializers.ModelSerializer) : | 24 | class RecordSerializer(serializers.ModelSerializer) : | ... | ... |
... | @@ -7,8 +7,9 @@ from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist | ... | @@ -7,8 +7,9 @@ from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist |
7 | from django.shortcuts import render | 7 | from django.shortcuts import render |
8 | 8 | ||
9 | from api.videorecord import record | 9 | from api.videorecord import record |
10 | -from api.models import Video, Device, History, Lock, Record, Door, AddDevice | 10 | +from api.models import Video, Device, RemoteHistory, Lock, Record, Door |
11 | -from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer, RecordSerializer | 11 | +from api.serializers import VideoSerializer, DeviceSerializer, RemoteHistorySerializer, RecordSerializer, LockSerializer |
12 | + | ||
12 | 13 | ||
13 | from rest_framework import status | 14 | from rest_framework import status |
14 | from rest_framework.views import APIView | 15 | from rest_framework.views import APIView |
... | @@ -22,8 +23,143 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK | ... | @@ -22,8 +23,143 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK |
22 | """ | 23 | """ |
23 | import time | 24 | import time |
24 | from datetime import datetime, timedelta | 25 | from datetime import datetime, timedelta |
26 | +import json | ||
25 | # Create your views here. | 27 | # Create your views here. |
26 | 28 | ||
29 | +#로그인 및 토큰 반환 | ||
30 | +class Login(APIView) : | ||
31 | + def get(self, request, format = None) : # request query에 door_id 포함되어있음 : api/auth?door_id=12345 | ||
32 | + try : | ||
33 | + request_id = request.GET.get('door_id', None) | ||
34 | + if request_id == None : | ||
35 | + raise FieldDoesNotExist | ||
36 | + queryset = Door.objects.filter(door_id = request_id) # door_id 유효성 검색 | ||
37 | + if queryset.exists() :# 유효할 때 | ||
38 | + res = { | ||
39 | + 'is_available' : True, | ||
40 | + 'access_token' : '토큰' # 토큰 도입 후 수정 필요 | ||
41 | + } | ||
42 | + else : | ||
43 | + res = { | ||
44 | + 'is_available' : False | ||
45 | + } | ||
46 | + | ||
47 | + return Response(res, status = status.HTTP_200_OK) | ||
48 | + | ||
49 | + except FieldDoesNotExist as error : | ||
50 | + return Response({ | ||
51 | + 'error' : "FieldDoesNotExist ", | ||
52 | + 'date' : datetime.now() | ||
53 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
54 | + | ||
55 | +#기기 관련 api | ||
56 | +class Devices(APIView) : | ||
57 | + # 기기 목록 조회 | ||
58 | + def get(self, request, format = None) : | ||
59 | + queryset = Device.objects.all() | ||
60 | + serializer = DeviceSerializer(queryset, many = True) | ||
61 | + res = { | ||
62 | + 'deviceList': serializer.data | ||
63 | + } | ||
64 | + return Response(res, status = status.HTTP_200_OK) | ||
65 | + | ||
66 | + # 기기 추가 요청 | ||
67 | + def put(self, request, format = None) : | ||
68 | + try : | ||
69 | + print(request.body) | ||
70 | + data = json.loads(request.body) | ||
71 | + rfid_id = data.get('rfid_id', None) | ||
72 | + res = { | ||
73 | + 'rfid_id': rfid_id | ||
74 | + } | ||
75 | + if rfid_id == None: | ||
76 | + raise FieldDoesNotExist | ||
77 | + return Response(res, status = status.HTTP_200_OK) | ||
78 | + except FieldDoesNotExist as error : | ||
79 | + return Response({ | ||
80 | + 'error' : "FieldDoesNotExist ", | ||
81 | + 'date' : datetime.now() | ||
82 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
83 | + | ||
84 | + # 기기 추가 | ||
85 | + def post(self, request, format = None) : # request body에 rfid_id 포함되어있음 | ||
86 | + try : | ||
87 | + print(request.body) | ||
88 | + data = json.loads(request.body) | ||
89 | + request_id = data.get('rfid_id', None) | ||
90 | + if request_id == None : | ||
91 | + raise FieldDoesNotExist | ||
92 | + queryset = Device.objects.create(rfid_id = request_id) | ||
93 | + return Response({ | ||
94 | + 'msg' : 'success device add' | ||
95 | + }) | ||
96 | + | ||
97 | + except FieldDoesNotExist as error : | ||
98 | + return Response({ | ||
99 | + 'error' : "FieldDoesNotExist ", | ||
100 | + 'date' : datetime.now() | ||
101 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
102 | + | ||
103 | + | ||
104 | + | ||
105 | + # 기기 삭제 | ||
106 | + def delete(self, request, device_id, format = None): # request URI에 device_id(자동생성되는 기기 고유 번호 != rfid_id) 포함 | ||
107 | + try : | ||
108 | + request_id = device_id | ||
109 | + if request_id == None: | ||
110 | + raise FieldDoesNotExist | ||
111 | + queryset = Device.objects.get(device_id=request_id) | ||
112 | + queryset.delete() | ||
113 | + return Response({ | ||
114 | + 'msg' : 'success delete device' | ||
115 | + }) | ||
116 | + | ||
117 | + except FieldDoesNotExist as error : | ||
118 | + return Response({ | ||
119 | + 'error' : "FieldDoesNotExist ", | ||
120 | + 'date' : datetime.now() | ||
121 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
122 | + | ||
123 | +# 원격 잠금 해제 | ||
124 | +class Remote(APIView): | ||
125 | + # 원격 잠금 해제 기록 조회 | ||
126 | + def get(self, request, format = None) : | ||
127 | + #models.py의 class History 사용. | ||
128 | + queryset = RemoteHistory.objects.all() | ||
129 | + serializer = RemoteHistorySerializer(queryset, many = True) | ||
130 | + res = { | ||
131 | + "remoteHistoryList": serializer.data | ||
132 | + } | ||
133 | + return Response(res, status = status.HTTP_200_OK) | ||
134 | + | ||
135 | + # 원격 잠금 해제 | ||
136 | + def post(self, request, format = None) : | ||
137 | + try: | ||
138 | + print(request.body) | ||
139 | + data = json.loads(request.body) | ||
140 | + device_name = data.get('device_name', None) | ||
141 | + if device_name == None : | ||
142 | + raise FieldDoesNotExist | ||
143 | + else: | ||
144 | + # 잠금 상태 변경 | ||
145 | + target = Lock.objects.get(id=1) | ||
146 | + serializer = LockSerializer(target, many=False) | ||
147 | + state = serializer.data['state'] | ||
148 | + if state == True: | ||
149 | + print(">> 원격 잠금해제 요청이 들어옴") | ||
150 | + target.state = False | ||
151 | + target.save() | ||
152 | + return Response({ | ||
153 | + 'msg' : 'success remote unlock' | ||
154 | + }, status = status.HTTP_200_OK) | ||
155 | + except FieldDoesNotExist as error: | ||
156 | + return Response({ | ||
157 | + 'error': "FieldDoesNotExist ", | ||
158 | + 'date': datetime.now() | ||
159 | + }, status=status.HTTP_400_BAD_REQUEST) | ||
160 | + | ||
161 | + | ||
162 | + | ||
27 | # 비디오 목록 조회 | 163 | # 비디오 목록 조회 |
28 | class VideoList(APIView) : | 164 | class VideoList(APIView) : |
29 | def get(self, request, format = None) : | 165 | def get(self, request, format = None) : | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -24,4 +24,8 @@ urlpatterns = [ | ... | @@ -24,4 +24,8 @@ urlpatterns = [ |
24 | path('api/video/<str:vid_name>', views.VideoDownload.as_view()), | 24 | path('api/video/<str:vid_name>', views.VideoDownload.as_view()), |
25 | path('auto/checkDate', views.CheckDate.as_view()), | 25 | path('auto/checkDate', views.CheckDate.as_view()), |
26 | path('api/setting', views.Recording.as_view()), | 26 | path('api/setting', views.Recording.as_view()), |
27 | + path('api/auth', views.Login.as_view()), | ||
28 | + path('api/device', views.Devices.as_view()), | ||
29 | + path('api/device/<str:device_id>', views.Devices.as_view()), | ||
30 | + path('api/remote', views.Remote.as_view()), | ||
27 | ] | 31 | ] | ... | ... |
-
Please register or login to post a comment