Compare commits

...

4 Commits

Author SHA1 Message Date
Valdur Kana f43cfcf131 Handle button correctly. get_hex refactored as hex getter. 2022-03-11 20:06:40 +02:00
Valdur Kana 0920df1fb0 Code cleanup 2022-03-11 19:59:01 +02:00
Valdur Kana a9f7638c14 Rename uid_hash init param as uid_salt.
uid_h renamed to uid_hash.
2022-03-11 19:51:12 +02:00
Valdur Kana 7cc479f468 Use f-string literal.
DoorController init call has now named parameters.
2022-03-11 19:48:52 +02:00
2 changed files with 28 additions and 30 deletions

View File

@ -29,12 +29,12 @@ class DoorController:
api_longpoll, api_longpoll,
api_swipe, api_swipe,
api_key, api_key,
uid_hash): uid_salt):
self.door = door self.door = door
self.api_allowed = api_allowed self.api_allowed = api_allowed
self.api_longpoll = api_longpoll self.api_longpoll = api_longpoll
self.api_swipe = api_swipe self.api_swipe = api_swipe
self.uid_hash = uid_hash self.uid_salt = uid_salt
self.uids = {} self.uids = {}
@ -67,20 +67,19 @@ class DoorController:
uids.add(token["token"]["uid_hash"].strip()) uids.add(token["token"]["uid_hash"].strip())
self.uids = uids self.uids = uids
def wiegand_callback(self, bits, value): def wiegand_callback(self, value):
uid_h = hash_uid(value, self.uid_hash) uid_hash = hash_uid(value, self.uid_salt)
logging.debug("hash %s", uid_h) logging.debug(f"hash {uid_hash}")
if uid_h in self.uids: if uid_hash in self.uids:
logging.info("Opening door for UID hash trail %s", uid_h[-10:]) logging.info(f"Opening door for UID hash trail {uid_hash[-10:]}")
self.wiegand.open_door() self.wiegand.open_door()
success = True success = True
else: else:
logging.info("Access card not in allow list, hash trail %s", logging.info(f"Access card not in allow list, hash trail {uid_hash[-10:]}")
uid_h[-10:])
success = False success = False
data = { data = {
"uid": value, "uid": value,
"uid_hash": uid_h, "uid_hash": uid_hash,
"door": self.door, "door": self.door,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
} }
@ -127,9 +126,10 @@ class DoorController:
if __name__ == "__main__": if __name__ == "__main__":
DoorController( DoorController(
os.environ["KDOORPI_DOOR"], door=os.environ["KDOORPI_DOOR"],
os.environ["KDOORPI_API_ALLOWED"], api_allowed=os.environ["KDOORPI_API_ALLOWED"],
os.environ["KDOORPI_API_LONGPOLL"], api_longpoll=os.environ["KDOORPI_API_LONGPOLL"],
os.environ["KDOORPI_API_SWIPE"], api_swipe=os.environ["KDOORPI_API_SWIPE"],
os.environ["KDOORPI_API_KEY"], api_key=os.environ["KDOORPI_API_KEY"],
os.environ["KDOORPI_UID_SALT"]) uid_salt=os.environ["KDOORPI_UID_SALT"],
)

View File

@ -56,7 +56,7 @@ class Decoder:
self._cb) self._cb)
self.button_cb_h = self.pi.callback(self.button_pin, self.button_cb_h = self.pi.callback(self.button_pin,
pigpio.FALLING_EDGE, pigpio.FALLING_EDGE,
self._cb) self.button_cb)
def cut_empty(self, item): def cut_empty(self, item):
if item[0:8] == "00000000": if item[0:8] == "00000000":
@ -64,7 +64,8 @@ class Decoder:
else: else:
return item return item
def get_hex(self): @property
def hex(self):
try: try:
items = self.items items = self.items
if len(self.items) == 26: if len(self.items) == 26:
@ -79,12 +80,11 @@ class Decoder:
bits))).rstrip() bits))).rstrip()
except ValueError: except ValueError:
logging.error("Failed to convert binary to hex: %s" % self.items) logging.error(f"Failed to convert binary to hex: {self.items}")
return False return False
except Exception as e: except Exception as e:
logging.error("Wiegand convert error (raw: %s): %s" % ( logging.error(f"Wiegand convert error (raw: {self.items}): {e}")
self.items, e))
return False return False
def _cb(self, gpio_pin, level, tick): def _cb(self, gpio_pin, level, tick):
@ -99,7 +99,7 @@ class Decoder:
if self.in_code: if self.in_code:
self.bits += 1 self.bits += 1
else: else:
logging.debug("Wiegand data transfer start") logging.debug(f"Wiegand data transfer start")
self.bits = 1 self.bits = 1
self.items = "" self.items = ""
self.in_code = True self.in_code = True
@ -129,20 +129,18 @@ class Decoder:
self.in_code = False self.in_code = False
if self.bits >= 26: if self.bits >= 26:
hex = self.get_hex() hex = self.hex
if hex: if hex:
self.callback(self.bits, hex) self.callback(hex)
else: else:
logging.error("Expected 26 bits, but got %i: %s" % logging.error(f"Expected 26 bits, but got {self.bits:d}: {self.items}")
(self.bits, self.items))
except Exception as e: except Exception as e:
logging.error("Wiegand callback error: %s" % e) logging.error(f"Wiegand callback error: {e}")
def button_cb(self, gpio_pin, level, tick): def button_cb(self, gpio_pin, level, tick):
print("button: gpio_pin:{}, level:{}, tick:{}".format(gpio_pin, print(f"button: gpio_pin:{gpio_pin}, level:{level}, tick:{tick}")
level, self.open_door()
tick))
def open_door(self): def open_door(self):
self.pi.write(self.door_pin, 1) self.pi.write(self.door_pin, 1)