mirror of
https://github.com/brmlab/brmdoor_libnfc.git
synced 2025-06-08 08:34:00 +02:00
Removed threads, this better works in one daemon.
This commit is contained in:
parent
b3d03b1f69
commit
5770eea7ca
1 changed files with 34 additions and 47 deletions
|
@ -1,8 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import threading
|
|
||||||
import Queue
|
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
@ -55,64 +53,60 @@ class BrmdoorConfig(object):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise BrmdoorConfigError("No such loglevel - %s" % levelString)
|
raise BrmdoorConfigError("No such loglevel - %s" % levelString)
|
||||||
|
|
||||||
class NfcThread(threading.Thread):
|
class NFCUnlocker(object):
|
||||||
"""Thread reading data from NFC reader"""
|
"""Thread reading data from NFC reader"""
|
||||||
|
|
||||||
def __init__(self, uidQueue):
|
def __init__(self, config):
|
||||||
"""Create thread reading UIDs from PN53x reader.
|
"""Create worker reading UIDs from PN53x reader.
|
||||||
"""
|
"""
|
||||||
self.uidQueue = uidQueue
|
self.authenticator = UidAuthenticator(config.authDbFilename)
|
||||||
threading.Thread.__init__(self)
|
self.lockOpenedSecs = config.lockOpenedSecs
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""
|
"""
|
||||||
Waits for a card to get into reader field. Reads its UID and
|
Waits for a card to get into reader field. Reads its UID and
|
||||||
stores it into uidQueue for later authentication check.
|
compares to database of authorized users. Unlocks lock if
|
||||||
|
authorized.
|
||||||
"""
|
"""
|
||||||
self.nfc = NFCDevice()
|
self.nfc = NFCDevice()
|
||||||
|
#self.nfc.pollNr = 0xFF #poll indefinitely
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
uid_hex = hexlify(self.nfc.scanUID())
|
uid_hex = hexlify(self.nfc.scanUID())
|
||||||
logging.debug("Got UID %s" % uid_hex)
|
logging.debug("Got UID %s", uid_hex)
|
||||||
if len(uid_hex) > 0:
|
if len(uid_hex) > 0:
|
||||||
self.uidQueue.put(uid_hex)
|
self.actOnUid(uid_hex)
|
||||||
|
else:
|
||||||
|
#prevent busy loop if reader goes awry
|
||||||
time.sleep(0.3)
|
time.sleep(0.3)
|
||||||
except NFCError, e:
|
except NFCError, e:
|
||||||
#this exception happens also when scanUID times out
|
#this exception happens also when scanUID times out
|
||||||
logging.debug("Failed to wait for RFID card: %s", e)
|
logging.debug("Failed to wait for RFID card: %s", e)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logging.info("Exiting on keyboard interrupt")
|
||||||
|
self.nfc.close()
|
||||||
|
self.nfc.unload()
|
||||||
|
sys.exit(2)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Exception in main unlock thread")
|
||||||
|
|
||||||
|
def actOnUid(self, uid_hex):
|
||||||
class UnlockThread(threading.Thread):
|
|
||||||
"""Thread checking UIDs whether they are authorized"""
|
|
||||||
|
|
||||||
def __init__(self, uidQueue, authenticatorDBFname, lockOpenedSecs):
|
|
||||||
"""Create thread reading UIDs from PN53x reader.
|
|
||||||
"""
|
"""
|
||||||
self.uidQueue = uidQueue
|
Do something with the UID scanned. Try to authenticate it against
|
||||||
self.authenticatorDBFname = authenticatorDBFname
|
database and open lock if authorized.
|
||||||
self.lockOpenedSecs = lockOpenedSecs
|
|
||||||
threading.Thread.__init__(self)
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
"""
|
"""
|
||||||
Reads hex UIDs from queue, tries to find them in sqlite database.
|
|
||||||
|
|
||||||
If match in database is found, then unlock the lock (for now
|
|
||||||
only logs).
|
|
||||||
"""
|
|
||||||
self.authenticator = UidAuthenticator(self.authenticatorDBFname)
|
|
||||||
while True:
|
|
||||||
uid_hex = self.uidQueue.get()
|
|
||||||
|
|
||||||
record = self.authenticator.fetchUidRecord(uid_hex)
|
record = self.authenticator.fetchUidRecord(uid_hex)
|
||||||
|
|
||||||
|
#no match
|
||||||
if record is None:
|
if record is None:
|
||||||
logging.info("Unknown UID %s", uid_hex)
|
logging.info("Unknown UID")
|
||||||
time.sleep(1)
|
time.sleep(0.3)
|
||||||
else:
|
return
|
||||||
logging.info("Unlocking for %s", record)
|
|
||||||
|
logging.info("Unlocking for UID %s", record)
|
||||||
time.sleep(self.lockOpenedSecs)
|
time.sleep(self.lockOpenedSecs)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
|
@ -128,13 +122,6 @@ if __name__ == "__main__":
|
||||||
logging.basicConfig(filename=config.logFile, level=config.logLevel,
|
logging.basicConfig(filename=config.logFile, level=config.logLevel,
|
||||||
format="%(asctime)s %(levelname)s %(message)s [%(pathname)s:%(lineno)d]")
|
format="%(asctime)s %(levelname)s %(message)s [%(pathname)s:%(lineno)d]")
|
||||||
|
|
||||||
uidQueue = Queue.Queue(1)
|
nfcUnlocker = NFCUnlocker(config)
|
||||||
|
nfcUnlocker.run()
|
||||||
nfcThread = NfcThread(uidQueue)
|
|
||||||
nfcThread.start()
|
|
||||||
|
|
||||||
unlockThread = UnlockThread(uidQueue, config.authDbFilename, config.lockOpenedSecs)
|
|
||||||
unlockThread.start()
|
|
||||||
|
|
||||||
uidQueue.join()
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue