Refactor ChipUnlocker
This commit is contained in:
8
app.py
8
app.py
@ -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
19
chip_unlocker.py
Normal 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)
|
16
unlocker.py
16
unlocker.py
@ -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)
|
Reference in New Issue
Block a user