mirror of
https://github.com/brmlab/brmdoor_libnfc.git
synced 2025-06-08 08:34:00 +02:00
Added config and config parser.
This commit is contained in:
parent
810c383283
commit
ceb1692e1a
2 changed files with 48 additions and 7 deletions
5
brmdoor_nfc.config.sample
Normal file
5
brmdoor_nfc.config.sample
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# auth_db_filename - sqlite filename of authorized UIDs, create with create_authenticator_db.py
|
||||||
|
# lock_opened_secs - how long lock should be held open in seconds, default 5
|
||||||
|
[brmdoor]
|
||||||
|
auth_db_filename = test_uids_db.sqlite
|
||||||
|
#lock_opened_secs = 5
|
|
@ -5,14 +5,42 @@ import threading
|
||||||
import Queue
|
import Queue
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import ConfigParser
|
||||||
|
|
||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
|
|
||||||
from ConfigParser import SafeConfigParser
|
|
||||||
|
|
||||||
from brmdoor_nfc import NFCDevice, NFCError
|
from brmdoor_nfc import NFCDevice, NFCError
|
||||||
from brmdoor_authenticator import UidAuthenticator
|
from brmdoor_authenticator import UidAuthenticator
|
||||||
|
|
||||||
|
class BrmdoorConfigError(ConfigParser.Error):
|
||||||
|
"""
|
||||||
|
Signifies that config has missing or bad values.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BrmdoorConfig(object):
|
||||||
|
"""
|
||||||
|
Configuration parser. Holds config variables from config file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_defaults = {
|
||||||
|
"lock_opened_secs": "5",
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, filename):
|
||||||
|
"""
|
||||||
|
Parse and read config from given filename.
|
||||||
|
|
||||||
|
@throws ConfigParser.Error if parsing failed
|
||||||
|
@throws BrmdoorConfigError if some value was missing or invalid
|
||||||
|
"""
|
||||||
|
self.config = ConfigParser.SafeConfigParser(defaults=BrmdoorConfig._defaults)
|
||||||
|
self.config.read(filename)
|
||||||
|
|
||||||
|
self.authDbFilename = self.config.get("brmdoor", "auth_db_filename")
|
||||||
|
self.lockOpenedSecs = self.config.getint("brmdoor", "lock_opened_secs")
|
||||||
|
|
||||||
class NfcThread(threading.Thread):
|
class NfcThread(threading.Thread):
|
||||||
"""Thread reading data from NFC reader"""
|
"""Thread reading data from NFC reader"""
|
||||||
|
|
||||||
|
@ -31,22 +59,24 @@ class NfcThread(threading.Thread):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
uid_hex = hexlify(self.nfc.scanUID())
|
uid_hex = hexlify(self.nfc.scanUID())
|
||||||
logging.info("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.uidQueue.put(uid_hex)
|
||||||
time.sleep(0.3)
|
time.sleep(0.3)
|
||||||
except NFCError, e:
|
except NFCError, e:
|
||||||
logging.warn("Failed to wait for RFID card: %s", e)
|
#this exception happens also when scanUID times out
|
||||||
|
logging.debug("Failed to wait for RFID card: %s", e)
|
||||||
|
|
||||||
|
|
||||||
class UnlockThread(threading.Thread):
|
class UnlockThread(threading.Thread):
|
||||||
"""Thread checking UIDs whether they are authorized"""
|
"""Thread checking UIDs whether they are authorized"""
|
||||||
|
|
||||||
def __init__(self, uidQueue, authenticatorDBFname):
|
def __init__(self, uidQueue, authenticatorDBFname, lockOpenedSecs):
|
||||||
"""Create thread reading UIDs from PN53x reader.
|
"""Create thread reading UIDs from PN53x reader.
|
||||||
"""
|
"""
|
||||||
self.uidQueue = uidQueue
|
self.uidQueue = uidQueue
|
||||||
self.authenticatorDBFname = authenticatorDBFname
|
self.authenticatorDBFname = authenticatorDBFname
|
||||||
|
self.lockOpenedSecs = lockOpenedSecs
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
@ -67,20 +97,26 @@ class UnlockThread(threading.Thread):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
logging.info("Unlocking for %s", record)
|
logging.info("Unlocking for %s", record)
|
||||||
time.sleep(1)
|
time.sleep(self.lockOpenedSecs)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print >> sys.stderr, "Syntax: brmdoor_nfc_daemon.py brmdoor_nfc.config"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
config = BrmdoorConfig(sys.argv[1])
|
||||||
|
|
||||||
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
|
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
|
||||||
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)
|
uidQueue = Queue.Queue(1)
|
||||||
#TODO use SafeConfigParser to get actual config data
|
|
||||||
|
|
||||||
nfcThread = NfcThread(uidQueue)
|
nfcThread = NfcThread(uidQueue)
|
||||||
nfcThread.setDaemon(True)
|
nfcThread.setDaemon(True)
|
||||||
nfcThread.start()
|
nfcThread.start()
|
||||||
|
|
||||||
unlockThread = UnlockThread(uidQueue, "test_uids_db.sqlite")
|
unlockThread = UnlockThread(uidQueue, config.authDbFilename, config.lockOpenedSecs)
|
||||||
unlockThread.setDaemon(True)
|
unlockThread.setDaemon(True)
|
||||||
unlockThread.start()
|
unlockThread.start()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue