From 5b77dff18dff06c55a4f4227a48ebcaabd9eb075 Mon Sep 17 00:00:00 2001 From: Ondrej Mikle Date: Wed, 23 Jul 2014 10:26:53 +0200 Subject: [PATCH] Added unlocker classes concept --- brmdoor_nfc.config.sample | 11 +++++++++++ brmdoor_nfc_daemon.py | 12 +++++++++--- unlocker.py | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 unlocker.py diff --git a/brmdoor_nfc.config.sample b/brmdoor_nfc.config.sample index 731b816..f229697 100644 --- a/brmdoor_nfc.config.sample +++ b/brmdoor_nfc.config.sample @@ -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 diff --git a/brmdoor_nfc_daemon.py b/brmdoor_nfc_daemon.py index 7703142..affc8e1 100755 --- a/brmdoor_nfc_daemon.py +++ b/brmdoor_nfc_daemon.py @@ -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() diff --git a/unlocker.py b/unlocker.py new file mode 100644 index 0000000..e7fcf54 --- /dev/null +++ b/unlocker.py @@ -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)