Refactor ChipUnlocker

This commit is contained in:
2019-10-02 15:26:09 +08:00
parent f435e8d9e0
commit 99d850a305
3 changed files with 23 additions and 20 deletions

8
app.py
View File

@ -1,13 +1,13 @@
import os
from flask import Flask, request, abort, jsonify
import unlocker
from chip_unlocker import ChipUnlocker
PORT = os.environ.get('PORT') or 5000
CODE = os.environ.get('CODE') or '8787'
UNLOCKER_PIN = os.environ.get('UNLOCKER_PIN') or 'XIO-P7'
UNLOCK_PIN = os.environ.get('UNLOCK_PIN') or 'XIO-P7'
app = Flask(__name__)
unlocker = ChipUnlocker(UNLOCK_PIN)
@app.route('/')
@ -22,7 +22,7 @@ def unlock():
code = request.args.get('code')
if (code == CODE):
unlocker.run(UNLOCKER_PIN)
unlocker.unlock()
return "OK"
else:
abort(404)

19
chip_unlocker.py Normal file
View File

@ -0,0 +1,19 @@
import time
import CHIP_IO.GPIO as GPIO
class ChipUnlocker:
def __init__(self, unlock_pin: str):
self.unlock_pin = unlock_pin
def unlock(self):
# 設定接腳
GPIO.setup(self.unlock_pin, GPIO.OUT)
# 送出低電位 0.4 秒後回復到高電位
GPIO.output(self.unlock_pin, GPIO.LOW)
time.sleep(0.4)
GPIO.output(self.unlock_pin, GPIO.HIGH)
# 復歸接腳
GPIO.cleanup(self.unlock_pin)

View File

@ -1,16 +0,0 @@
import time
import CHIP_IO.GPIO as GPIO
def run(pin: str):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
print("[Door] Unlocking...")
time.sleep(0.4)
GPIO.output(pin, GPIO.HIGH)
print("[Door] Unlocked!")
GPIO.cleanup(pin)