김유현

Modify doorlock process and Modify api(delete auth)

......@@ -77,8 +77,6 @@ class Devices(APIView) :
# 기기 목록 조회
def get(self, request, format = None) :
try :
if request.auth == None :
raise PermissionDenied
queryset = Device.objects.all()
serializer = DeviceSerializer(queryset, many = True)
res = {
......@@ -90,19 +88,12 @@ class Devices(APIView) :
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
except PermissionDenied as error :
return Response({
'error' : "PermissionDenied",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
# 기기 추가 요청
def put(self, request, device_id, format = None) :
def put(self, request, format = None) :
try :
if request.auth == None :
raise PermissionDenied
if device_id != "request" :
raise FieldDoesNotExist
print(request.body)
target = AddDevice.objects.get(id=1)
serializer = AddDeviceSerializer(target, many=False)
......@@ -114,11 +105,6 @@ class Devices(APIView) :
return Response({
'msg' : 'changed state successfully'
}, status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
except PermissionDenied as error :
return Response({
'error' : "PermissionDenied",
......@@ -128,8 +114,6 @@ class Devices(APIView) :
# 기기 추가
def post(self, request, format = None) : # request body에 rfid_id 포함되어있음 
try :
if request.auth == None :
raise PermissionDenied
print(request.data)
request_id = request.data.get('rfid_id', None)
if request_id == None :
......@@ -152,11 +136,6 @@ class Devices(APIView) :
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
except PermissionDenied as error :
return Response({
'error' : "PermissionDenied",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
......
No preview for this file type
No preview for this file type
......@@ -9,6 +9,7 @@ import os
import django
import datetime
import sys
import subprocess
sys.path.append('/home/pi/Desktop/smartdoorlock-backend')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
......@@ -22,38 +23,41 @@ from api.serializers import LockSerializer, AddDeviceSerializer, DeviceSerialize
GPIO.setmode(GPIO.BCM)
MFIAREReader = mfrc522.MFRC522() # RFID Reader
BASE_URL = "http://127.0.0.1:8000"
PIN = {
'Motor_MT_N': 12,
'Motor_MT_P': 6
}
PIN = 18
class Motor:
LEFT = 0
RIGHT = 1
class Sound:
def __init__(self):
self.right_sound = "asset/dingdongdang.wav"
self.wrong_sound = "asset/beep.wav"
def print_right_sound(self):
print("딩동댕 ~ 완료하였습니다")
result = subprocess.check_output("sudo aplay {}".format(self.right_sound), shell=True)
def print_wrong_sound(self):
print("경고음 삑 -!")
result = subprocess.check_output("sudo aplay {}".format(self.wrong_sound), shell=True)
class Motor:
def __init__(self):
self.pwmN = GPIO.PWM(PIN['Motor_MT_N'], 100)
self.pwmP = GPIO.PWM(PIN['Motor_MT_P'], 100)
self.pwmN.start(0)
self.pwmP.start(0)
def rotate(self, direction):
if direction == Motor.LEFT:
GPIO.output(PIN['Motor_MT_N'], GPIO.HIGH)
GPIO.output(PIN['Motor_MT_P'], GPIO.LOW)
self.pwmN.ChangeDutyCycle(50)
self.p = GPIO.PWM(PIN, 50)
self.p.start(0)
def rotate(self, command):
if command == "open":
self.p.ChangeDutyCycle(12.5)
else:
GPIO.output(PIN['Motor_MT_N'], GPIO.LOW)
GPIO.output(PIN['Motor_MT_P'], GPIO.HIGH)
self.pwmP.ChangeDutyCycle(50)
self.p.ChangeDutyCycle(2.5)
def stop(self):
self.pwmP.ChangeDutyCycle(0)
self.pwmN.ChangeDutyCycle(0)
self.p.ChangeDutyCycle(0)
def RFIDProcess(signalQueue):
sound = Sound()
while True:
success = False
try:
......@@ -94,10 +98,10 @@ def RFIDProcess(signalQueue):
else:
# callApi(POST /api/device, {rfid_id:deviceId}) # 기기 추가
requests.post(BASE_URL+"/api/device", data={"rfid_id": deviceId})
print("딩동댕 ~ 완료하였습니다") # 소리 출력
sound.print_right_sound()
pass
except:
print("경고음 삑 -!")
sound.print_wrong_sound()
pass
finally: # setToIPC(기기 추가 여부, 기기 추가 아님)
target.state = False
......@@ -111,7 +115,7 @@ def RFIDProcess(signalQueue):
else:
success = True
except:
print("경고음 삑 -!") # 소리 출력
sound.print_wrong_sound()
pass
if success:
......@@ -146,27 +150,26 @@ def signalProcess(signalQueue):
def doorProcess(doorQueue):
# motor = Motor()
motor = Motor()
while True:
signal = doorQueue.get()
print("{} 신호를 받아 문 열기 동작 수행 시작".format(signal))
if signal is not None:
print("문 열림")
#motor.rotate(Motor.LEFT)
motor.rotate("open")
time.sleep(0.5)
#motor.stop()
motor.stop()
time.sleep(5) # 열린 후 5초 지나면 닫힘
print("문 닫힘")
#motor.rotate(Motor.RIGHT)
motor.rotate("close")
time.sleep(0.5)
#motor.stop()
motor.stop()
if __name__ == '__main__':
try:
GPIO.setmode(GPIO.BCM)
#GPIO.setup(PIN['Motor_MT_N'], GPIO.OUT, initial=GPIO.LOW)
#GPIO.setup(PIN['Motor_MT_P'], GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN, GPIO.OUT)
signalQueue = Queue()
pid = os.fork()
......