From ae8f3f8249333c463a7cd500e27b1acc9274e655 Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Sun, 15 Oct 2017 20:51:46 -0700 Subject: [PATCH 1/5] Improved error handling in open --- alarmdecoder/decoder.py | 37 +++++++++++++++++++++++++++---------- test/test_devices.py | 5 ----- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 84fa521..0cdee41 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -152,7 +152,7 @@ class AlarmDecoder(object): self._panic_status = False self._relay_status = {} self._internal_address_mask = 0xFFFFFFFF - + self.last_fault_expansion = 0 self.fault_expansion_time_limit = 30 # Seconds @@ -251,17 +251,26 @@ class AlarmDecoder(object): self._internal_address_mask = value def open(self, baudrate=None, no_reader_thread=False): - """ - Opens the device. + """Opens the device. + + If the device cannot be opened, an except is thrown. In that + case, open() can be called repeatedly to try and open the + connection. :param baudrate: baudrate used for the device. Defaults to the lower-level device default. :type baudrate: int :param no_reader_thread: Specifies whether or not the automatic reader thread should be started. :type no_reader_thread: bool + """ self._wire_events() - self._device.open(baudrate=baudrate, no_reader_thread=no_reader_thread) + try: + self._device.open(baudrate=baudrate, + no_reader_thread=no_reader_thread) + except: + self._unwire_events + raise return self @@ -269,11 +278,8 @@ class AlarmDecoder(object): """ Closes the device. """ - if self._device: - self._device.close() - - del self._device - self._device = None + self._device.close() + self._unwire_events() def send(self, data): """ @@ -387,6 +393,17 @@ class AlarmDecoder(object): self._zonetracker.on_fault += self._on_zone_fault self._zonetracker.on_restore += self._on_zone_restore + def _unwire_events(self): + """ + Wires up the internal device events. + """ + self._device.on_open -= self._on_open + self._device.on_close -= self._on_close + self._device.on_read -= self._on_read + self._device.on_write -= self._on_write + self._zonetracker.on_fault -= self._on_zone_fault + self._zonetracker.on_restore -= self._on_zone_restore + def _handle_message(self, data): """ Parses keypad messages from the panel. @@ -792,7 +809,7 @@ class AlarmDecoder(object): # Handle bouncing status changes and timeout in order to revert back to NONE. if last_status != fire_status or fire_status == True: self._fire_status = (fire_status, time.time()) - + if fire_status == False and time.time() > last_update + self._fire_timeout: self._fire_state = FireState.NONE self.on_fire(status=FireState.NONE) diff --git a/test/test_devices.py b/test/test_devices.py index 604af8d..a46b10a 100644 --- a/test/test_devices.py +++ b/test/test_devices.py @@ -1,11 +1,6 @@ from unittest import TestCase from mock import Mock, MagicMock, patch from serial import Serial, SerialException -try: - from pyftdi.pyftdi.ftdi import Ftdi, FtdiError -except: - from pyftdi.ftdi import Ftdi, FtdiError -from usb.core import USBError, Device as USBCoreDevice import sys import socket import time From c52a61bf9e312378722519b699f0485038aeb5a8 Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Sun, 5 Nov 2017 16:13:58 -0800 Subject: [PATCH 2/5] Removed some unnecessary changes --- alarmdecoder/decoder.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 0cdee41..b73351c 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -152,7 +152,6 @@ class AlarmDecoder(object): self._panic_status = False self._relay_status = {} self._internal_address_mask = 0xFFFFFFFF - self.last_fault_expansion = 0 self.fault_expansion_time_limit = 30 # Seconds @@ -253,7 +252,7 @@ class AlarmDecoder(object): def open(self, baudrate=None, no_reader_thread=False): """Opens the device. - If the device cannot be opened, an except is thrown. In that + If the device cannot be opened, an exception is thrown. In that case, open() can be called repeatedly to try and open the connection. @@ -796,7 +795,6 @@ class AlarmDecoder(object): if fire_status == True: self._fire_state = FireState.ALARM self._fire_status = (fire_status, time.time()) - self.on_fire(status=FireState.ALARM) elif self._fire_state == FireState.ALARM: From 9ccba59d455737bab852f3c8d2465cf5facf42eb Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Sun, 5 Nov 2017 16:16:19 -0800 Subject: [PATCH 3/5] Fixed spacing --- alarmdecoder/decoder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index b73351c..29e0465 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -152,6 +152,7 @@ class AlarmDecoder(object): self._panic_status = False self._relay_status = {} self._internal_address_mask = 0xFFFFFFFF + self.last_fault_expansion = 0 self.fault_expansion_time_limit = 30 # Seconds @@ -795,6 +796,7 @@ class AlarmDecoder(object): if fire_status == True: self._fire_state = FireState.ALARM self._fire_status = (fire_status, time.time()) + self.on_fire(status=FireState.ALARM) elif self._fire_state == FireState.ALARM: @@ -807,7 +809,7 @@ class AlarmDecoder(object): # Handle bouncing status changes and timeout in order to revert back to NONE. if last_status != fire_status or fire_status == True: self._fire_status = (fire_status, time.time()) - + if fire_status == False and time.time() > last_update + self._fire_timeout: self._fire_state = FireState.NONE self.on_fire(status=FireState.NONE) From 8f60f553c30fe54342f3f9cc89b16faeefb91a36 Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Sun, 5 Nov 2017 16:25:03 -0800 Subject: [PATCH 4/5] Cleaning up for merge. --- alarmdecoder/decoder.py | 1 - test/test_devices.py | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 29e0465..f53a624 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -262,7 +262,6 @@ class AlarmDecoder(object): :param no_reader_thread: Specifies whether or not the automatic reader thread should be started. :type no_reader_thread: bool - """ self._wire_events() try: diff --git a/test/test_devices.py b/test/test_devices.py index a46b10a..604af8d 100644 --- a/test/test_devices.py +++ b/test/test_devices.py @@ -1,6 +1,11 @@ from unittest import TestCase from mock import Mock, MagicMock, patch from serial import Serial, SerialException +try: + from pyftdi.pyftdi.ftdi import Ftdi, FtdiError +except: + from pyftdi.ftdi import Ftdi, FtdiError +from usb.core import USBError, Device as USBCoreDevice import sys import socket import time From ee756f65a909dfa17afbc289d407216b23d6ab6b Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Mon, 6 Nov 2017 20:10:13 -0800 Subject: [PATCH 5/5] Fixed extra spaces --- alarmdecoder/decoder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index f53a624..123fbe4 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -795,7 +795,7 @@ class AlarmDecoder(object): if fire_status == True: self._fire_state = FireState.ALARM self._fire_status = (fire_status, time.time()) - + self.on_fire(status=FireState.ALARM) elif self._fire_state == FireState.ALARM: