김유현

Modify doorlock process and Modify api(delete auth)

...@@ -77,8 +77,6 @@ class Devices(APIView) : ...@@ -77,8 +77,6 @@ class Devices(APIView) :
77 # 기기 목록 조회 77 # 기기 목록 조회
78 def get(self, request, format = None) : 78 def get(self, request, format = None) :
79 try : 79 try :
80 - if request.auth == None :
81 - raise PermissionDenied
82 queryset = Device.objects.all() 80 queryset = Device.objects.all()
83 serializer = DeviceSerializer(queryset, many = True) 81 serializer = DeviceSerializer(queryset, many = True)
84 res = { 82 res = {
...@@ -89,20 +87,13 @@ class Devices(APIView) : ...@@ -89,20 +87,13 @@ class Devices(APIView) :
89 return Response({ 87 return Response({
90 'error' : "FieldDoesNotExist ", 88 'error' : "FieldDoesNotExist ",
91 'date' : datetime.now() 89 'date' : datetime.now()
92 - }, status = status.HTTP_400_BAD_REQUEST) 90 + }, status = status.HTTP_400_BAD_REQUEST)
93 - except PermissionDenied as error :
94 - return Response({
95 - 'error' : "PermissionDenied",
96 - 'date' : datetime.now()
97 - }, status = status.HTTP_400_BAD_REQUEST)
98 91
99 # 기기 추가 요청 92 # 기기 추가 요청
100 - def put(self, request, device_id, format = None) : 93 + def put(self, request, format = None) :
101 try : 94 try :
102 if request.auth == None : 95 if request.auth == None :
103 raise PermissionDenied 96 raise PermissionDenied
104 - if device_id != "request" :
105 - raise FieldDoesNotExist
106 print(request.body) 97 print(request.body)
107 target = AddDevice.objects.get(id=1) 98 target = AddDevice.objects.get(id=1)
108 serializer = AddDeviceSerializer(target, many=False) 99 serializer = AddDeviceSerializer(target, many=False)
...@@ -114,11 +105,6 @@ class Devices(APIView) : ...@@ -114,11 +105,6 @@ class Devices(APIView) :
114 return Response({ 105 return Response({
115 'msg' : 'changed state successfully' 106 'msg' : 'changed state successfully'
116 }, status = status.HTTP_200_OK) 107 }, status = status.HTTP_200_OK)
117 - except FieldDoesNotExist as error :
118 - return Response({
119 - 'error' : "FieldDoesNotExist ",
120 - 'date' : datetime.now()
121 - }, status = status.HTTP_400_BAD_REQUEST)
122 except PermissionDenied as error : 108 except PermissionDenied as error :
123 return Response({ 109 return Response({
124 'error' : "PermissionDenied", 110 'error' : "PermissionDenied",
...@@ -127,9 +113,7 @@ class Devices(APIView) : ...@@ -127,9 +113,7 @@ class Devices(APIView) :
127 113
128 # 기기 추가 114 # 기기 추가
129 def post(self, request, format = None) : # request body에 rfid_id 포함되어있음  115 def post(self, request, format = None) : # request body에 rfid_id 포함되어있음 
130 - try : 116 + try :
131 - if request.auth == None :
132 - raise PermissionDenied
133 print(request.data) 117 print(request.data)
134 request_id = request.data.get('rfid_id', None) 118 request_id = request.data.get('rfid_id', None)
135 if request_id == None : 119 if request_id == None :
...@@ -152,11 +136,6 @@ class Devices(APIView) : ...@@ -152,11 +136,6 @@ class Devices(APIView) :
152 'error' : "FieldDoesNotExist ", 136 'error' : "FieldDoesNotExist ",
153 'date' : datetime.now() 137 'date' : datetime.now()
154 }, status = status.HTTP_400_BAD_REQUEST) 138 }, status = status.HTTP_400_BAD_REQUEST)
155 - except PermissionDenied as error :
156 - return Response({
157 - 'error' : "PermissionDenied",
158 - 'date' : datetime.now()
159 - }, status = status.HTTP_400_BAD_REQUEST)
160 139
161 140
162 141
......
No preview for this file type
No preview for this file type
...@@ -9,6 +9,7 @@ import os ...@@ -9,6 +9,7 @@ import os
9 import django 9 import django
10 import datetime 10 import datetime
11 import sys 11 import sys
12 +import subprocess
12 13
13 sys.path.append('/home/pi/Desktop/smartdoorlock-backend') 14 sys.path.append('/home/pi/Desktop/smartdoorlock-backend')
14 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings') 15 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
...@@ -22,45 +23,48 @@ from api.serializers import LockSerializer, AddDeviceSerializer, DeviceSerialize ...@@ -22,45 +23,48 @@ from api.serializers import LockSerializer, AddDeviceSerializer, DeviceSerialize
22 GPIO.setmode(GPIO.BCM) 23 GPIO.setmode(GPIO.BCM)
23 MFIAREReader = mfrc522.MFRC522() # RFID Reader 24 MFIAREReader = mfrc522.MFRC522() # RFID Reader
24 BASE_URL = "http://127.0.0.1:8000" 25 BASE_URL = "http://127.0.0.1:8000"
25 -PIN = { 26 +PIN = 18
26 - 'Motor_MT_N': 12,
27 - 'Motor_MT_P': 6
28 -}
29 27
30 28
31 -class Motor: 29 +class Sound:
32 - LEFT = 0 30 + def __init__(self):
33 - RIGHT = 1 31 + self.right_sound = "asset/dingdongdang.wav"
32 + self.wrong_sound = "asset/beep.wav"
33 +
34 + def print_right_sound(self):
35 + print("딩동댕 ~ 완료하였습니다")
36 + result = subprocess.check_output("sudo aplay {}".format(self.right_sound), shell=True)
37 +
38 + def print_wrong_sound(self):
39 + print("경고음 삑 -!")
40 + result = subprocess.check_output("sudo aplay {}".format(self.wrong_sound), shell=True)
41 +
34 42
43 +class Motor:
35 def __init__(self): 44 def __init__(self):
36 - self.pwmN = GPIO.PWM(PIN['Motor_MT_N'], 100) 45 + self.p = GPIO.PWM(PIN, 50)
37 - self.pwmP = GPIO.PWM(PIN['Motor_MT_P'], 100) 46 + self.p.start(0)
38 - self.pwmN.start(0) 47 +
39 - self.pwmP.start(0) 48 + def rotate(self, command):
40 - 49 + if command == "open":
41 - def rotate(self, direction): 50 + self.p.ChangeDutyCycle(12.5)
42 - if direction == Motor.LEFT:
43 - GPIO.output(PIN['Motor_MT_N'], GPIO.HIGH)
44 - GPIO.output(PIN['Motor_MT_P'], GPIO.LOW)
45 - self.pwmN.ChangeDutyCycle(50)
46 else: 51 else:
47 - GPIO.output(PIN['Motor_MT_N'], GPIO.LOW) 52 + self.p.ChangeDutyCycle(2.5)
48 - GPIO.output(PIN['Motor_MT_P'], GPIO.HIGH)
49 - self.pwmP.ChangeDutyCycle(50)
50 53
51 def stop(self): 54 def stop(self):
52 - self.pwmP.ChangeDutyCycle(0) 55 + self.p.ChangeDutyCycle(0)
53 - self.pwmN.ChangeDutyCycle(0) 56 +
54 57
55 58
56 def RFIDProcess(signalQueue): 59 def RFIDProcess(signalQueue):
60 + sound = Sound()
57 while True: 61 while True:
58 success = False 62 success = False
59 try: 63 try:
60 (readerStatus, tagType) = MFIAREReader.MFRC522_Request(MFIAREReader.PICC_REQIDL) 64 (readerStatus, tagType) = MFIAREReader.MFRC522_Request(MFIAREReader.PICC_REQIDL)
61 (readerStatus, uid) = MFIAREReader.MFRC522_Anticoll() # uid = [1, 2, 3, 4, 5] 65 (readerStatus, uid) = MFIAREReader.MFRC522_Anticoll() # uid = [1, 2, 3, 4, 5]
62 if readerStatus == MFIAREReader.MI_OK: # if RFID 태그가 됨: 66 if readerStatus == MFIAREReader.MI_OK: # if RFID 태그가 됨:
63 - time.sleep(1) 67 + time.sleep(1)
64 print("RIFD tag") 68 print("RIFD tag")
65 deviceId = "" # 방금 태그된 RFID 장치의 ID. 69 deviceId = "" # 방금 태그된 RFID 장치의 ID.
66 for i in uid: 70 for i in uid:
...@@ -94,10 +98,10 @@ def RFIDProcess(signalQueue): ...@@ -94,10 +98,10 @@ def RFIDProcess(signalQueue):
94 else: 98 else:
95 # callApi(POST /api/device, {rfid_id:deviceId}) # 기기 추가 99 # callApi(POST /api/device, {rfid_id:deviceId}) # 기기 추가
96 requests.post(BASE_URL+"/api/device", data={"rfid_id": deviceId}) 100 requests.post(BASE_URL+"/api/device", data={"rfid_id": deviceId})
97 - print("딩동댕 ~ 완료하였습니다") # 소리 출력 101 + sound.print_right_sound()
98 pass 102 pass
99 except: 103 except:
100 - print("경고음 삑 -!") 104 + sound.print_wrong_sound()
101 pass 105 pass
102 finally: # setToIPC(기기 추가 여부, 기기 추가 아님) 106 finally: # setToIPC(기기 추가 여부, 기기 추가 아님)
103 target.state = False 107 target.state = False
...@@ -111,7 +115,7 @@ def RFIDProcess(signalQueue): ...@@ -111,7 +115,7 @@ def RFIDProcess(signalQueue):
111 else: 115 else:
112 success = True 116 success = True
113 except: 117 except:
114 - print("경고음 삑 -!") # 소리 출력 118 + sound.print_wrong_sound()
115 pass 119 pass
116 120
117 if success: 121 if success:
...@@ -146,27 +150,26 @@ def signalProcess(signalQueue): ...@@ -146,27 +150,26 @@ def signalProcess(signalQueue):
146 150
147 151
148 def doorProcess(doorQueue): 152 def doorProcess(doorQueue):
149 - # motor = Motor() 153 + motor = Motor()
150 while True: 154 while True:
151 signal = doorQueue.get() 155 signal = doorQueue.get()
152 print("{} 신호를 받아 문 열기 동작 수행 시작".format(signal)) 156 print("{} 신호를 받아 문 열기 동작 수행 시작".format(signal))
153 if signal is not None: 157 if signal is not None:
154 print("문 열림") 158 print("문 열림")
155 - #motor.rotate(Motor.LEFT) 159 + motor.rotate("open")
156 time.sleep(0.5) 160 time.sleep(0.5)
157 - #motor.stop() 161 + motor.stop()
158 time.sleep(5) # 열린 후 5초 지나면 닫힘 162 time.sleep(5) # 열린 후 5초 지나면 닫힘
159 print("문 닫힘") 163 print("문 닫힘")
160 - #motor.rotate(Motor.RIGHT) 164 + motor.rotate("close")
161 time.sleep(0.5) 165 time.sleep(0.5)
162 - #motor.stop() 166 + motor.stop()
163 167
164 168
165 if __name__ == '__main__': 169 if __name__ == '__main__':
166 try: 170 try:
167 GPIO.setmode(GPIO.BCM) 171 GPIO.setmode(GPIO.BCM)
168 - #GPIO.setup(PIN['Motor_MT_N'], GPIO.OUT, initial=GPIO.LOW) 172 + GPIO.setup(PIN, GPIO.OUT)
169 - #GPIO.setup(PIN['Motor_MT_P'], GPIO.OUT, initial=GPIO.LOW)
170 173
171 signalQueue = Queue() 174 signalQueue = Queue()
172 pid = os.fork() 175 pid = os.fork()
......