From e49583d1af064481ce17412d456c0de044af6d51 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Thu, 17 Oct 2013 12:48:04 -0700 Subject: [PATCH] Made close() generic. Added ssl property to SocketDevice. Cleanup. --- pyad2usb/devices.py | 56 ++++++++++++++++++++++++---------------- pyad2usb/zonetracking.py | 2 +- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/pyad2usb/devices.py b/pyad2usb/devices.py index 8ca3ca0..c805292 100644 --- a/pyad2usb/devices.py +++ b/pyad2usb/devices.py @@ -72,6 +72,20 @@ class Device(object): """ self._read_thread.stop() + def close(self): + """ + Closes the device. + """ + try: + self._running = False + self._read_thread.stop() + self._device.close() + + except: + pass + + self.on_close() + class ReadThread(threading.Thread): """ Reader thread which processes messages from the device. @@ -113,7 +127,7 @@ class Device(object): except Exception, err: self._running = False - raise err + #raise err time.sleep(0.01) @@ -226,19 +240,14 @@ class USBDevice(Device): Closes the device. """ try: - self._running = False - self._read_thread.stop() - - self._device.close() - # HACK: Probably should fork pyftdi and make this call in .close(). self._device.usb_dev.attach_kernel_driver(self._interface) + super(USBDevice, self).close() + except: pass - self.on_close() - def write(self, data): """ Writes data to the device. @@ -439,16 +448,11 @@ class SerialDevice(Device): Closes the device. """ try: - self._running = False - self._read_thread.stop() - - self._device.close() + super(SerialDevice, self).close() except: pass - self.on_close() - def write(self, data): """ Writes data to the device. @@ -557,6 +561,15 @@ class SocketDevice(Device): Serial to IP interface. """ + @property + def ssl(self): + """ + Retrieves whether or not the device is using SSL. + + :returns: Whether or not the device is using SSL. + """ + return self._use_ssl + @property def ssl_certificate(self): """ @@ -681,17 +694,16 @@ class SocketDevice(Device): """ Closes the device. """ - self._running = False - try: - self._read_thread.stop() - self._device.shutdown(socket.SHUT_RDWR) # Make sure that it closes immediately. - self._device.close() + if self.ssl: + self._device.shutdown() + else: + self._device.shutdown(socket.SHUT_RDWR) # Make sure that it closes immediately. - except: - pass + super(SocketDevice, self).close() - self.on_close() + except Exception, ex: + pass def write(self, data): """ diff --git a/pyad2usb/zonetracking.py b/pyad2usb/zonetracking.py index ce0a1e4..dca315c 100644 --- a/pyad2usb/zonetracking.py +++ b/pyad2usb/zonetracking.py @@ -79,7 +79,7 @@ class Zonetracker(object): if isinstance(message, messages.ExpanderMessage): if message.type == messages.ExpanderMessage.ZONE: - zone = self._expander_to_zone(int(message.address), int(message.channel)) + zone = self._expander_to_zone(message.address, message.channel) status = Zone.CLEAR if message.value == 1: