Added unlocker classes concept

This commit is contained in:
Ondrej Mikle 2014-07-23 10:26:53 +02:00
parent 739c1a1e19
commit 5b77dff18d
3 changed files with 60 additions and 3 deletions

View file

@ -4,9 +4,20 @@
# prevents too many messages
# log_file - logs read UIDs and when was lock opened, use - for stderr
# log_level - minimum log level - one of debug, info, warn, error, fatal, default info
# unlocker - which unlocker class to use - Unlocker or UnlockerWiringPi
# Unlocker is just dummy test class.
[brmdoor]
auth_db_filename = test_uids_db.sqlite
#lock_opened_secs = 5
#unknown_uid_timeout_secs = 5
log_file = -
#log_level = info
unlocker = Unlocker
# Config section for dummy unlocker. It has no options.
[Unlocker]
# Config for wiringPi-based unlocker
# lock_pin - which pin needs to be pulled high to disengage the lock
[UnlockerWiringPi]
lock_pin = 18

View file

@ -10,6 +10,7 @@ from binascii import hexlify
from brmdoor_nfc import NFCDevice, NFCError
from brmdoor_authenticator import UidAuthenticator
import unlocker
class BrmdoorConfigError(ConfigParser.Error):
"""
@ -43,6 +44,7 @@ class BrmdoorConfig(object):
self.unknownUidTimeoutSecs = self.config.getint("brmdoor", "unknown_uid_timeout_secs")
self.logFile = self.config.get("brmdoor", "log_file")
self.logLevel = self.convertLoglevel(self.config.get("brmdoor", "log_level"))
self.unlocker = self.config.get("brmdoor", "unlocker")
def convertLoglevel(self, levelString):
"""Converts string 'debug', 'info', etc. into corresponding
@ -64,6 +66,10 @@ class NFCScanner(object):
self.authenticator = UidAuthenticator(config.authDbFilename)
self.unknownUidTimeoutSecs = config.unknownUidTimeoutSecs
self.lockOpenedSecs = config.lockOpenedSecs
unlockerClassName = config.unlocker
unlockerClass = getattr(unlocker, unlockerClassName)
self.unlocker = unlockerClass(config)
def run(self):
"""
@ -107,7 +113,7 @@ class NFCScanner(object):
return
logging.info("Unlocking for UID %s", record)
time.sleep(self.lockOpenedSecs)
self.unlocker.unlock()
if __name__ == "__main__":
@ -125,6 +131,6 @@ if __name__ == "__main__":
logging.basicConfig(filename=config.logFile, level=config.logLevel,
format="%(asctime)s %(levelname)s %(message)s [%(pathname)s:%(lineno)d]")
nfcUnlocker = NFCUnlocker(config)
nfcUnlocker.run()
nfcScanner = NFCScanner(config)
nfcScanner.run()

40
unlocker.py Normal file
View file

@ -0,0 +1,40 @@
import time
import wiringpi2 as wiringpi
class Unlocker(object):
"""Abstract class/interface for Unlocker object.
Unlocker useful for simulation, but does not actually unlock any lock.
"""
def __init__(self, config):
"""
Creates unlocked instance from config, where section named after
the class is supposed to exist.
@param config: BrmdoorConfig instance
"""
self.config = config.config
self.lockOpenedSecs = config.lockOpenedSecs
self.unlockerName = type(self).__name__
def unlock(self):
"""Unlock lock for given self.lockOpenedSecs.
In this class case, it's only simulated
"""
time.sleep(self.lockOpenedSecs)
class UnlockerWiringPi(Unlocker):
"""Uses configured pings via WiringPi to open lock.
"""
def __init__(self, config):
Unlocker.__init__(self, config)
self.lockPin = self.config.getint("UnlockerWiringPi", "lock_pin")
wiringpi.pinMode(self.lockPin, 1) #output
def unlock(self):
"""Unlocks lock at configured pin by pulling it high.
"""
wiringpi.digitalWrite(self.lockPin, 1)
time.sleep(self.lockOpenedSecs)
wiringpi.digitalWrite(self.lockPin, 0)