From 57d68db806d4e8f496d5c61e2928e67e9b848146 Mon Sep 17 00:00:00 2001 From: Randy Topliffe Date: Thu, 19 Jun 2014 09:14:13 -0400 Subject: [PATCH] Folder Structure clean up - Removed the drivers folder --- door.py | 2 +- rpi_door/{drivers => }/GPIO.py | 0 rpi_door/__init__.py | 129 +++++++++++++++++++++++++++++++++ rpi_door/drivers/__init__.py | 129 --------------------------------- tests/__init__.py | 8 +- 5 files changed, 134 insertions(+), 134 deletions(-) rename rpi_door/{drivers => }/GPIO.py (100%) delete mode 100644 rpi_door/drivers/__init__.py diff --git a/door.py b/door.py index 0a48717..f3b800f 100755 --- a/door.py +++ b/door.py @@ -1,5 +1,5 @@ #!/usr/bin/python -from rpi_door.drivers.GPIO import RPiDoor +from rpi_door.GPIO import RPiDoor rpi_door = RPiDoor(**{ "sqlalchemy.url": "sqlite:///database.db", diff --git a/rpi_door/drivers/GPIO.py b/rpi_door/GPIO.py similarity index 100% rename from rpi_door/drivers/GPIO.py rename to rpi_door/GPIO.py diff --git a/rpi_door/__init__.py b/rpi_door/__init__.py index e69de29..8c34fc9 100644 --- a/rpi_door/__init__.py +++ b/rpi_door/__init__.py @@ -0,0 +1,129 @@ +# Copyright (C) 2013 Windsor Hackforge +# +# This module is part of RPi Door and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php + +import re +import serial +from time import sleep + + +class SerialConnectionError(Exception): + + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + + +class AbstractDoor(): + + code_re = re.compile("\\n(.+)\\r", re.UNICODE) + + def __init__(self, *args, **kwargs): + + port = kwargs.get("port", "/dev/ttyAMA0") + baudrate = kwargs.get("baudrate", 2400) + + self.serial_conn = serial.Serial(port, baudrate, timeout=0) + + if not self.serial_conn.isOpen(): + raise SerialConnectionError("Serial connection couldn't be open.") + + # Makes sure the state of the door is locked when first started. This + # is mostly for security reasons. For example, if the power goes out we + # want to door to lock when the power comes back on. Trying to remember + # the door's state in should events would be difficult and not worth + # the effort. + self.lock() + self.toggle_red_led(on=True) + + @property + def data(self): + return self._data + + @data.setter + def data(self, data): + """Check to see if the data to be set is greater than 41. If so it sets + its self to an empty bytearray + """ + if len(data) > 41: + self._data = b"" + else: + self._data = data + return self._data + + def main_loop(self): + while True: + data = self.read_RFID() + if data and self.validate_key_code(data): + self.toggle_red_led() + self.toggle_green_led(on=True) + self.unlock() + sleep(1) + self.toggle_red_led(on=True) + self.toggle_green_led() + self.check_for_lock_request() + + def find_key_code(self, data): + """ Checks the given string to see if it contains a code (valid or not) + + Args: + data (str): data to be checked + + Returns: + None or str:: + None if there isn't a match or the code if there is a match + + """ + match = re.match(self.code_re, data) + if match: + return match.groups()[0] + return None + + def read_RFID(self): + """reads one byte at a time until it finds a key code + """ + # flushes to remove any remaining bytes + self.serial_conn.flushInput() + self.data = b"" + + while True: + while self.serial_conn.inWaiting() > 0: + self.data += self.serial_conn.read(1) + + if self.data: + str_data = str(self.data, 'utf-8') + code = self.find_key_code(str_data) + if code: + return code + + def check_for_lock_request(self): + """continuously checks to see if the state is true. If so it calls the + `lock` method + """ + while True: + sleep(0.1) + if self.get_state(): + sleep(5) + self.lock() + break + + def get_state(self): + raise NotImplementedError("Not implemented.") + + def validate_key_code(self, data): + raise NotImplementedError("Not implemented.") + + def unlock(self): + raise NotImplementedError("Not implemented.") + + def lock(self): + raise NotImplementedError("Not implemented.") + + def toggle_red_led(self, on=False): + raise NotImplementedError("Not implemented.") + + def toggle_green_led(self, on=False): + raise NotImplementedError("Not implemented.") diff --git a/rpi_door/drivers/__init__.py b/rpi_door/drivers/__init__.py deleted file mode 100644 index 8c34fc9..0000000 --- a/rpi_door/drivers/__init__.py +++ /dev/null @@ -1,129 +0,0 @@ -# Copyright (C) 2013 Windsor Hackforge -# -# This module is part of RPi Door and is released under -# the MIT License: http://www.opensource.org/licenses/mit-license.php - -import re -import serial -from time import sleep - - -class SerialConnectionError(Exception): - - def __init__(self, value): - self.value = value - - def __str__(self): - return repr(self.value) - - -class AbstractDoor(): - - code_re = re.compile("\\n(.+)\\r", re.UNICODE) - - def __init__(self, *args, **kwargs): - - port = kwargs.get("port", "/dev/ttyAMA0") - baudrate = kwargs.get("baudrate", 2400) - - self.serial_conn = serial.Serial(port, baudrate, timeout=0) - - if not self.serial_conn.isOpen(): - raise SerialConnectionError("Serial connection couldn't be open.") - - # Makes sure the state of the door is locked when first started. This - # is mostly for security reasons. For example, if the power goes out we - # want to door to lock when the power comes back on. Trying to remember - # the door's state in should events would be difficult and not worth - # the effort. - self.lock() - self.toggle_red_led(on=True) - - @property - def data(self): - return self._data - - @data.setter - def data(self, data): - """Check to see if the data to be set is greater than 41. If so it sets - its self to an empty bytearray - """ - if len(data) > 41: - self._data = b"" - else: - self._data = data - return self._data - - def main_loop(self): - while True: - data = self.read_RFID() - if data and self.validate_key_code(data): - self.toggle_red_led() - self.toggle_green_led(on=True) - self.unlock() - sleep(1) - self.toggle_red_led(on=True) - self.toggle_green_led() - self.check_for_lock_request() - - def find_key_code(self, data): - """ Checks the given string to see if it contains a code (valid or not) - - Args: - data (str): data to be checked - - Returns: - None or str:: - None if there isn't a match or the code if there is a match - - """ - match = re.match(self.code_re, data) - if match: - return match.groups()[0] - return None - - def read_RFID(self): - """reads one byte at a time until it finds a key code - """ - # flushes to remove any remaining bytes - self.serial_conn.flushInput() - self.data = b"" - - while True: - while self.serial_conn.inWaiting() > 0: - self.data += self.serial_conn.read(1) - - if self.data: - str_data = str(self.data, 'utf-8') - code = self.find_key_code(str_data) - if code: - return code - - def check_for_lock_request(self): - """continuously checks to see if the state is true. If so it calls the - `lock` method - """ - while True: - sleep(0.1) - if self.get_state(): - sleep(5) - self.lock() - break - - def get_state(self): - raise NotImplementedError("Not implemented.") - - def validate_key_code(self, data): - raise NotImplementedError("Not implemented.") - - def unlock(self): - raise NotImplementedError("Not implemented.") - - def lock(self): - raise NotImplementedError("Not implemented.") - - def toggle_red_led(self, on=False): - raise NotImplementedError("Not implemented.") - - def toggle_green_led(self, on=False): - raise NotImplementedError("Not implemented.") diff --git a/tests/__init__.py b/tests/__init__.py index 25509ad..ebc5872 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,7 +2,7 @@ from unittest.mock import patch from testfixtures import ShouldRaise from rpi_database.models import SQLAlchemyMixin -from rpi_door.drivers import AbstractDoor +from rpi_door import AbstractDoor class TestDoor(SQLAlchemyMixin, AbstractDoor): @@ -60,7 +60,7 @@ def patch_serial_read(self, data): time (from a give string). This allows to fully control what data is read and procressed.""" - with patch("rpi_door.drivers.serial.Serial"): + with patch("rpi_door.serial.Serial"): import serial ser = serial.Serial(0) @@ -83,9 +83,9 @@ def tearDown(self): self.door.drop_db() def test_serial_connection_error(self): - with patch("rpi_door.drivers.serial.Serial"): + with patch("rpi_door.serial.Serial"): import serial - from rpi_door.drivers import SerialConnectionError + from rpi_door import SerialConnectionError ser = serial.Serial(0) ser.isOpen.return_value = False