Browse Source

Added support for context manager.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
ea06827f79
9 changed files with 64 additions and 63 deletions
  1. +3
    -8
      examples/alarm_email.py
  2. +3
    -8
      examples/basics.py
  3. +3
    -8
      examples/rf_device.py
  4. +5
    -9
      examples/serialport.py
  5. +3
    -8
      examples/socket_example.py
  6. +3
    -8
      examples/ssl_socket.py
  7. +7
    -12
      examples/virtual_zone_expander.py
  8. +16
    -0
      pyad2/ad2.py
  9. +21
    -2
      pyad2/devices.py

+ 3
- 8
examples/alarm_email.py View File

@@ -24,18 +24,13 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_alarm += handle_alarm device.on_alarm += handle_alarm
device.open()

# Wait for events
while True:
time.sleep(1)
with device.open():
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_alarm(sender, *args, **kwargs): def handle_alarm(sender, *args, **kwargs):
""" """
Handles alarm events from the AD2. Handles alarm events from the AD2.


+ 3
- 8
examples/basics.py View File

@@ -12,18 +12,13 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message
device.open()

# Wait for events.
while True:
time.sleep(1)
with device.open():
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_message(sender, *args, **kwargs): def handle_message(sender, *args, **kwargs):
""" """
Handles message events from the AD2. Handles message events from the AD2.


+ 3
- 8
examples/rf_device.py View File

@@ -22,18 +22,13 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_rfx_message += handle_rfx device.on_rfx_message += handle_rfx
device.open()

# Wait for events.
while True:
time.sleep(1)
with device.open():
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_rfx(sender, *args, **kwargs): def handle_rfx(sender, *args, **kwargs):
""" """
Handles RF message events from the AD2. Handles RF message events from the AD2.


+ 5
- 9
examples/serialport.py View File

@@ -16,20 +16,16 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message
device.open(baudrate=BAUDRATE) # Override the default SerialDevice
# baudrate since we're using a USB
# device over serial in this example.


# Wait for events.
while True:
time.sleep(1)
# Override the default SerialDevice baudrate since we're using a USB device
# over serial in this example.
with device.open(baudrate=BAUDRATE):
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_message(sender, *args, **kwargs): def handle_message(sender, *args, **kwargs):
""" """
Handles message events from the AD2. Handles message events from the AD2.


+ 3
- 8
examples/socket_example.py View File

@@ -17,18 +17,13 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message
device.open()

# Wait for events.
while True:
time.sleep(1)
with device.open():
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_message(sender, *args, **kwargs): def handle_message(sender, *args, **kwargs):
""" """
Handles message events from the AD2. Handles message events from the AD2.


+ 3
- 8
examples/ssl_socket.py View File

@@ -31,18 +31,13 @@ def main():


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message
device.open()

# Wait for events.
while True:
time.sleep(1)
with device.open():
while True:
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_message(sender, *args, **kwargs): def handle_message(sender, *args, **kwargs):
""" """
Handles message events from the AD2. Handles message events from the AD2.


+ 7
- 12
examples/virtual_zone_expander.py View File

@@ -34,24 +34,19 @@ def main():
device.on_zone_fault += handle_zone_fault device.on_zone_fault += handle_zone_fault
device.on_zone_restore += handle_zone_restore device.on_zone_restore += handle_zone_restore


device.open()
with device.open():
last_update = time.time()
while True:
if time.time() - last_update > WAIT_TIME:
last_update = time.time()


# Wait for events.
last_update = time.time()
while True:
if time.time() - last_update > WAIT_TIME:
last_update = time.time()
device.fault_zone(TARGET_ZONE)


device.fault_zone(TARGET_ZONE)

time.sleep(1)
time.sleep(1)


except Exception, ex: except Exception, ex:
print 'Exception:', ex print 'Exception:', ex


finally:
device.close()

def handle_zone_fault(sender, *args, **kwargs): def handle_zone_fault(sender, *args, **kwargs):
""" """
Handles zone fault messages. Handles zone fault messages.


+ 16
- 0
pyad2/ad2.py View File

@@ -85,6 +85,20 @@ class AD2(object):
self.emulate_lrr = False self.emulate_lrr = False
self.deduplicate = False self.deduplicate = False


def __enter__(self):
"""
Support for context manager __enter__.
"""
return self

def __exit__(self, type, value, traceback):
"""
Support for context manager __exit__.
"""
self.close()

return False

@property @property
def id(self): def id(self):
""" """
@@ -107,6 +121,8 @@ class AD2(object):
self._device.open(baudrate=baudrate, no_reader_thread=no_reader_thread) self._device.open(baudrate=baudrate, no_reader_thread=no_reader_thread)
self.get_config() self.get_config()


return self

def close(self): def close(self):
""" """
Closes the device. Closes the device.


+ 21
- 2
pyad2/devices.py View File

@@ -37,6 +37,20 @@ class Device(object):
self._running = False self._running = False
self._read_thread = Device.ReadThread(self) self._read_thread = Device.ReadThread(self)


def __enter__(self):
"""
Support for context manager __enter__.
"""
return self

def __exit__(self, type, value, traceback):
"""
Support for context manager __exit__.
"""
self.close()

return False

@property @property
def id(self): def id(self):
""" """
@@ -338,10 +352,12 @@ class USBDevice(Device):


else: else:
self._running = True self._running = True
self.on_open()

if not no_reader_thread: if not no_reader_thread:
self._read_thread.start() self._read_thread.start()


self.on_open()
return self


def close(self): def close(self):
""" """
@@ -630,6 +646,8 @@ class SerialDevice(Device):
if not no_reader_thread: if not no_reader_thread:
self._read_thread.start() self._read_thread.start()


return self

def close(self): def close(self):
""" """
Closes the device. Closes the device.
@@ -890,12 +908,13 @@ class SocketDevice(Device):


else: else:
self._running = True self._running = True

self.on_open() self.on_open()


if not no_reader_thread: if not no_reader_thread:
self._read_thread.start() self._read_thread.start()


return self

def close(self): def close(self):
""" """
Closes the device. Closes the device.


Loading…
Cancel
Save