@@ -24,18 +24,13 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_alarm(sender, *args, **kwargs): | |||
""" | |||
Handles alarm events from the AD2. | |||
@@ -12,18 +12,13 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_message(sender, *args, **kwargs): | |||
""" | |||
Handles message events from the AD2. | |||
@@ -22,18 +22,13 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_rfx(sender, *args, **kwargs): | |||
""" | |||
Handles RF message events from the AD2. | |||
@@ -16,20 +16,16 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_message(sender, *args, **kwargs): | |||
""" | |||
Handles message events from the AD2. | |||
@@ -17,18 +17,13 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_message(sender, *args, **kwargs): | |||
""" | |||
Handles message events from the AD2. | |||
@@ -31,18 +31,13 @@ def main(): | |||
# Set up an event handler and open the device | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_message(sender, *args, **kwargs): | |||
""" | |||
Handles message events from the AD2. | |||
@@ -34,24 +34,19 @@ def main(): | |||
device.on_zone_fault += handle_zone_fault | |||
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: | |||
print 'Exception:', ex | |||
finally: | |||
device.close() | |||
def handle_zone_fault(sender, *args, **kwargs): | |||
""" | |||
Handles zone fault messages. | |||
@@ -85,6 +85,20 @@ class AD2(object): | |||
self.emulate_lrr = 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 | |||
def id(self): | |||
""" | |||
@@ -107,6 +121,8 @@ class AD2(object): | |||
self._device.open(baudrate=baudrate, no_reader_thread=no_reader_thread) | |||
self.get_config() | |||
return self | |||
def close(self): | |||
""" | |||
Closes the device. | |||
@@ -37,6 +37,20 @@ class Device(object): | |||
self._running = False | |||
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 | |||
def id(self): | |||
""" | |||
@@ -338,10 +352,12 @@ class USBDevice(Device): | |||
else: | |||
self._running = True | |||
self.on_open() | |||
if not no_reader_thread: | |||
self._read_thread.start() | |||
self.on_open() | |||
return self | |||
def close(self): | |||
""" | |||
@@ -630,6 +646,8 @@ class SerialDevice(Device): | |||
if not no_reader_thread: | |||
self._read_thread.start() | |||
return self | |||
def close(self): | |||
""" | |||
Closes the device. | |||
@@ -890,12 +908,13 @@ class SocketDevice(Device): | |||
else: | |||
self._running = True | |||
self.on_open() | |||
if not no_reader_thread: | |||
self._read_thread.start() | |||
return self | |||
def close(self): | |||
""" | |||
Closes the device. | |||