Showing
2 changed files
with
79 additions
and
0 deletions
... | @@ -24,6 +24,82 @@ import time | ... | @@ -24,6 +24,82 @@ import time |
24 | from datetime import datetime, timedelta | 24 | from datetime import datetime, timedelta |
25 | # Create your views here. | 25 | # Create your views here. |
26 | 26 | ||
27 | +#로그인 및 토큰 반환 | ||
28 | +class Login(APIView) : | ||
29 | + def get(self, request, format = None) : # request query에 door_id 포함되어있음 : api/auth?door_id=12345 | ||
30 | + try : | ||
31 | + request_id = request.GET.get('door_id', None) | ||
32 | + if request_id == 'None' : | ||
33 | + raise FieldDoesNotExist | ||
34 | + queryset = Door.objects.filter(door_id = request_id) # door_id 유효성 검색 | ||
35 | + if queryset.exists() # 유효할 때 | ||
36 | + res = { | ||
37 | + 'is_available' : True | ||
38 | + 'access_token' : '토큰' # 토큰 도입 후 수정 필요 | ||
39 | + } | ||
40 | + else | ||
41 | + res = { | ||
42 | + 'is_available' : False | ||
43 | + } | ||
44 | + | ||
45 | + return Response(res, status = status.HTTP_200_OK) | ||
46 | + | ||
47 | + except FieldDoesNotExist as error : | ||
48 | + return Response({ | ||
49 | + 'error' : "FieldDoesNotExist ", | ||
50 | + 'date' : datetime.now() | ||
51 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
52 | + | ||
53 | +#기기 관련 api | ||
54 | +class Device(APIView) : | ||
55 | + # 기기 추가 | ||
56 | + def post(self, request, format = None) : # request query에 device_id 포함되어있음 : api/device?device_id=12345 | ||
57 | + try : | ||
58 | + request_id = request.POST.get('device_id', None) | ||
59 | + if request_id == 'None' : | ||
60 | + raise FieldDoesNotExist | ||
61 | + queryset = Device.objects.create(rfid_id = request_id) | ||
62 | + | ||
63 | + except FieldDoesNotExist as error : | ||
64 | + return Response({ | ||
65 | + 'error' : "FieldDoesNotExist ", | ||
66 | + 'date' : datetime.now() | ||
67 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
68 | + | ||
69 | + # 기기 목록 조회 | ||
70 | + def get(self, request, format = None) : | ||
71 | + queryset = Device.objects.all() | ||
72 | + serializer = DeviceSerializer(queryset, many = True) | ||
73 | + res = { | ||
74 | + 'deviceList': serializer.data | ||
75 | + } | ||
76 | + return Response(res, status = status.HTTP_200_OK) | ||
77 | + | ||
78 | + # 기기 삭제 | ||
79 | + def delete(self, request, device_id, format = None): # request URI에 device_id 포함 | ||
80 | + try : | ||
81 | + request_id = device_id | ||
82 | + if request_id == 'None': | ||
83 | + raise FieldDoesNotExist | ||
84 | + queryset = Device.objects.get(device_id=request_id) | ||
85 | + queryset.delete() | ||
86 | + | ||
87 | + except FieldDoesNotExist as error : | ||
88 | + return Response({ | ||
89 | + 'error' : "FieldDoesNotExist ", | ||
90 | + 'date' : datetime.now() | ||
91 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
92 | + | ||
93 | +# 원격 잠금 해제 기록 조회 | ||
94 | +class History(APIView): | ||
95 | + def get(self, request, format = None) : | ||
96 | + queryset = History.objects.all() | ||
97 | + serializer = HistorySerializer(queryset, many = True) | ||
98 | + res = { | ||
99 | + 'deviceList': serializer.data | ||
100 | + } | ||
101 | + return Response(res, status = status.HTTP_200_OK) | ||
102 | + | ||
27 | # 비디오 목록 조회 | 103 | # 비디오 목록 조회 |
28 | class VideoList(APIView) : | 104 | class VideoList(APIView) : |
29 | def get(self, request, format = None) : | 105 | def get(self, request, format = None) : | ... | ... |
... | @@ -24,4 +24,7 @@ urlpatterns = [ | ... | @@ -24,4 +24,7 @@ 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.Device.as_view()), | ||
29 | + path('api/history', view.History.as_view()), | ||
27 | ] | 30 | ] | ... | ... |
-
Please register or login to post a comment