Browse Source

Made close() generic. Added ssl property to SocketDevice. Cleanup.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
e49583d1af
2 changed files with 35 additions and 23 deletions
  1. +34
    -22
      pyad2usb/devices.py
  2. +1
    -1
      pyad2usb/zonetracking.py

+ 34
- 22
pyad2usb/devices.py View File

@@ -72,6 +72,20 @@ class Device(object):
""" """
self._read_thread.stop() 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): class ReadThread(threading.Thread):
""" """
Reader thread which processes messages from the device. Reader thread which processes messages from the device.
@@ -113,7 +127,7 @@ class Device(object):
except Exception, err: except Exception, err:
self._running = False self._running = False


raise err
#raise err


time.sleep(0.01) time.sleep(0.01)


@@ -226,19 +240,14 @@ class USBDevice(Device):
Closes the device. Closes the device.
""" """
try: try:
self._running = False
self._read_thread.stop()

self._device.close()

# HACK: Probably should fork pyftdi and make this call in .close(). # HACK: Probably should fork pyftdi and make this call in .close().
self._device.usb_dev.attach_kernel_driver(self._interface) self._device.usb_dev.attach_kernel_driver(self._interface)


super(USBDevice, self).close()

except: except:
pass pass


self.on_close()

def write(self, data): def write(self, data):
""" """
Writes data to the device. Writes data to the device.
@@ -439,16 +448,11 @@ class SerialDevice(Device):
Closes the device. Closes the device.
""" """
try: try:
self._running = False
self._read_thread.stop()

self._device.close()
super(SerialDevice, self).close()


except: except:
pass pass


self.on_close()

def write(self, data): def write(self, data):
""" """
Writes data to the device. Writes data to the device.
@@ -557,6 +561,15 @@ class SocketDevice(Device):
Serial to IP interface. 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 @property
def ssl_certificate(self): def ssl_certificate(self):
""" """
@@ -681,17 +694,16 @@ class SocketDevice(Device):
""" """
Closes the device. Closes the device.
""" """
self._running = False

try: 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): def write(self, data):
""" """


+ 1
- 1
pyad2usb/zonetracking.py View File

@@ -79,7 +79,7 @@ class Zonetracker(object):


if isinstance(message, messages.ExpanderMessage): if isinstance(message, messages.ExpanderMessage):
if message.type == messages.ExpanderMessage.ZONE: 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 status = Zone.CLEAR
if message.value == 1: if message.value == 1:


Loading…
Cancel
Save