Kdoor functional, split web api into seperate file
This commit is contained in:
parent
93fad4f97f
commit
d006ba2915
@ -29,6 +29,8 @@ class Decoder:
|
||||
|
||||
self.gpio_0 = 17 #settings.WIEGAND[0]
|
||||
self.gpio_1 = 18 #settings.WIEGAND[1]
|
||||
self.door_pin = 21 # from settings.py
|
||||
self.button_pin = 13 # from settings.py
|
||||
|
||||
self.callback = callback
|
||||
|
||||
@ -39,12 +41,16 @@ class Decoder:
|
||||
if self.pi:
|
||||
self.pi.set_mode(self.gpio_0, pigpio.INPUT)
|
||||
self.pi.set_mode(self.gpio_1, pigpio.INPUT)
|
||||
self.pi.set_mode(self.door_pin, pigpio.OUTPUT)
|
||||
self.pi.set_mode(self.button_pin, pigpio.INPUT)
|
||||
|
||||
self.pi.set_pull_up_down(self.gpio_0, pigpio.PUD_UP)
|
||||
self.pi.set_pull_up_down(self.gpio_1, pigpio.PUD_UP)
|
||||
self.pi.set_pull_up_down(self.button_pin, pigpio.PUD_UP)
|
||||
|
||||
self.cb_0 = self.pi.callback(self.gpio_0, pigpio.FALLING_EDGE, self._cb)
|
||||
self.cb_1 = self.pi.callback(self.gpio_1, pigpio.FALLING_EDGE, self._cb)
|
||||
self.button_cb_h = self.pi.callback(self.button_pin, pigpio.FALLING_EDGE, self._cb)
|
||||
|
||||
def cut_empty(self, item):
|
||||
if item[0:8] == "00000000":
|
||||
@ -125,6 +131,14 @@ class Decoder:
|
||||
except Exception as e:
|
||||
logging.error("Wiegand callback error: " + str(e))
|
||||
|
||||
def button_cb(self, gpio_pin, level, tick):
|
||||
print("button: gpio_pin:{}, level:{}, tick:{}".format(gpio_pin, level, tick))
|
||||
|
||||
def open_door(self):
|
||||
self.pi.write(self.door_pin, 1)
|
||||
sleep(3)
|
||||
self.pi.write(self.door_pin, 0)
|
||||
|
||||
def cancel(self):
|
||||
|
||||
"""
|
||||
@ -133,11 +147,10 @@ class Decoder:
|
||||
|
||||
self.cb_0.cancel()
|
||||
self.cb_1.cancel()
|
||||
self.button_cb_h.cancel()
|
||||
self.pi.stop()
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from urllib.request import urlopen
|
||||
import json
|
||||
@ -150,9 +163,11 @@ if __name__ == "__main__":
|
||||
cards[user["card_uid"].strip()] = user
|
||||
|
||||
def wiegand_callback(bits, value):
|
||||
print("bits", bits)
|
||||
print("value", value)
|
||||
print("user", cards.get(value))
|
||||
print("bits", bits, "value", value)
|
||||
u = cards.get(value)
|
||||
if u:
|
||||
print("user", u)
|
||||
w.open_door()
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
@ -160,6 +175,10 @@ if __name__ == "__main__":
|
||||
w = Decoder(wiegand_callback)
|
||||
from time import sleep
|
||||
while 1:
|
||||
try:
|
||||
sleep(1)
|
||||
except KeyboardInterrupt as e:
|
||||
w.cancel()
|
||||
break
|
||||
|
||||
|
||||
|
34
kdoorweb/kdoorweb/api.py
Normal file
34
kdoorweb/kdoorweb/api.py
Normal file
@ -0,0 +1,34 @@
|
||||
from bottle import Bottle, request, response
|
||||
|
||||
|
||||
api = Bottle()
|
||||
|
||||
|
||||
# FIXME: Fix door api auth
|
||||
def check_api_auth(callback):
|
||||
def wrapper(*args, **kwargs):
|
||||
print("check api auth")
|
||||
if "db" not in kwargs:
|
||||
request.current_user = None
|
||||
return callback(*args, **kwargs)
|
||||
user = None
|
||||
request.current_user = user
|
||||
if user:
|
||||
print(f"logged in as {user['user']}")
|
||||
print(request.current_user)
|
||||
return callback(*args, **kwargs)
|
||||
else:
|
||||
print("not logged in")
|
||||
return "Invalid authentication"
|
||||
return wrapper
|
||||
|
||||
# FIXME: db plugin not available yet
|
||||
api.install(check_api_auth)
|
||||
|
||||
@api.route("/")
|
||||
def index():
|
||||
return "api v1"
|
||||
|
||||
@api.route("/cards")
|
||||
def api_list_cards(db):
|
||||
return {"keycards":[dict(card) for card in db.list_all_keycards()]}
|
@ -5,6 +5,7 @@ from bottle import Bottle, view, TEMPLATE_PATH, static_file, \
|
||||
request, redirect, response, HTTPError
|
||||
|
||||
from .db import SQLitePlugin
|
||||
from .api import api
|
||||
|
||||
application = app = Bottle()
|
||||
|
||||
@ -49,9 +50,14 @@ def check_auth(callback):
|
||||
return wrapper
|
||||
|
||||
|
||||
app.install(SQLitePlugin(SQLITE_PATH))
|
||||
db_plugin = SQLitePlugin(SQLITE_PATH)
|
||||
|
||||
app.install(db_plugin)
|
||||
app.install(check_auth)
|
||||
|
||||
api.install(db_plugin)
|
||||
app.mount("/api/v1", api)
|
||||
|
||||
|
||||
@app.route('/static/<path:path>', skip=[check_auth])
|
||||
def callback(path):
|
||||
@ -152,9 +158,3 @@ def log(db):
|
||||
@view("doors.html")
|
||||
def doors(db):
|
||||
return {"doors":[]}
|
||||
|
||||
|
||||
# FIXME: Add door api auth
|
||||
@app.route("/api/v1/cards", skip=[check_auth])
|
||||
def api_list_cards(db):
|
||||
return {"keycards":[dict(card) for card in db.list_all_keycards()]}
|
||||
|
Loading…
Reference in New Issue
Block a user