1
0
mirror of http://git.k-space.ee/arti/doors.git synced 2024-12-12 15:40:58 +02:00

Kdoor functional, split web api into seperate file

This commit is contained in:
Arti Zirk 2020-10-04 17:24:44 +03:00
parent 93fad4f97f
commit d006ba2915
3 changed files with 68 additions and 15 deletions

View File

@ -27,8 +27,10 @@ class Decoder:
else: else:
self.pi = False self.pi = False
self.gpio_0 = 17 #settings.WIEGAND[0] self.gpio_0 = 17 #settings.WIEGAND[0]
self.gpio_1 = 18 #settings.WIEGAND[1] self.gpio_1 = 18 #settings.WIEGAND[1]
self.door_pin = 21 # from settings.py
self.button_pin = 13 # from settings.py
self.callback = callback self.callback = callback
@ -39,12 +41,16 @@ class Decoder:
if self.pi: if self.pi:
self.pi.set_mode(self.gpio_0, pigpio.INPUT) self.pi.set_mode(self.gpio_0, pigpio.INPUT)
self.pi.set_mode(self.gpio_1, 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_0, pigpio.PUD_UP)
self.pi.set_pull_up_down(self.gpio_1, 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_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.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): def cut_empty(self, item):
if item[0:8] == "00000000": if item[0:8] == "00000000":
@ -125,6 +131,14 @@ class Decoder:
except Exception as e: except Exception as e:
logging.error("Wiegand callback error: " + str(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): def cancel(self):
""" """
@ -133,11 +147,10 @@ class Decoder:
self.cb_0.cancel() self.cb_0.cancel()
self.cb_1.cancel() self.cb_1.cancel()
self.button_cb_h.cancel()
self.pi.stop() self.pi.stop()
if __name__ == "__main__": if __name__ == "__main__":
from urllib.request import urlopen from urllib.request import urlopen
import json import json
@ -150,9 +163,11 @@ if __name__ == "__main__":
cards[user["card_uid"].strip()] = user cards[user["card_uid"].strip()] = user
def wiegand_callback(bits, value): def wiegand_callback(bits, value):
print("bits", bits) print("bits", bits, "value", value)
print("value", value) u = cards.get(value)
print("user", cards.get(value)) if u:
print("user", u)
w.open_door()
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -160,6 +175,10 @@ if __name__ == "__main__":
w = Decoder(wiegand_callback) w = Decoder(wiegand_callback)
from time import sleep from time import sleep
while 1: while 1:
sleep(1) try:
sleep(1)
except KeyboardInterrupt as e:
w.cancel()
break

34
kdoorweb/kdoorweb/api.py Normal file
View 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()]}

View File

@ -5,6 +5,7 @@ from bottle import Bottle, view, TEMPLATE_PATH, static_file, \
request, redirect, response, HTTPError request, redirect, response, HTTPError
from .db import SQLitePlugin from .db import SQLitePlugin
from .api import api
application = app = Bottle() application = app = Bottle()
@ -49,9 +50,14 @@ def check_auth(callback):
return wrapper return wrapper
app.install(SQLitePlugin(SQLITE_PATH)) db_plugin = SQLitePlugin(SQLITE_PATH)
app.install(db_plugin)
app.install(check_auth) app.install(check_auth)
api.install(db_plugin)
app.mount("/api/v1", api)
@app.route('/static/<path:path>', skip=[check_auth]) @app.route('/static/<path:path>', skip=[check_auth])
def callback(path): def callback(path):
@ -152,9 +158,3 @@ def log(db):
@view("doors.html") @view("doors.html")
def doors(db): def doors(db):
return {"doors":[]} 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()]}