From 15115432bd1663efb6b30f785243d35bf2778e47 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Thu, 5 Dec 2013 15:54:49 -0800 Subject: [PATCH] More example cleanup. --- examples/serialport.py | 8 ++++++-- examples/socket_example.py | 8 ++++++-- examples/ssl_socket.py | 13 ++++++++++--- examples/virtual_zone_expander.py | 11 +++++++---- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/examples/serialport.py b/examples/serialport.py index cdc71eb..583d39c 100644 --- a/examples/serialport.py +++ b/examples/serialport.py @@ -2,17 +2,21 @@ import time from pyad2 import AD2 from pyad2.devices import SerialDevice +# Configuration values +SERIAL_DEVICE = '/dev/ttyUSB0' +BAUDRATE = 115200 + def main(): """ Example application that opens a serial device and prints messages to the terminal. """ try: # Retrieve the specified serial device. - device = AD2(SerialDevice(interface='/dev/ttyUSB0')) + device = AD2(SerialDevice(interface=SERIAL_DEVICE)) # Set up an event handler and open the device device.on_message += handle_message - device.open(baudrate=115200) # Override the default SerialDevice baudrate. + device.open(baudrate=BAUDRATE) # Override the default SerialDevice baudrate. device.get_config() # Wait for events. diff --git a/examples/socket_example.py b/examples/socket_example.py index 88509ef..f0fb4e2 100644 --- a/examples/socket_example.py +++ b/examples/socket_example.py @@ -2,14 +2,18 @@ import time from pyad2 import AD2 from pyad2.devices import SocketDevice +# Configuration values +HOSTNAME = 'localhost' +PORT = 10000 + def main(): """ Example application that opens a device that has been exposed to the network - with ser2sock or similar serial->ip software. + with ser2sock or similar serial-to-IP software. """ try: # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000. - device = AD2(SocketDevice(interface=('localhost', 10000))) + device = AD2(SocketDevice(interface=(HOSTNAME, PORT))) # Set up an event handler and open the device device.on_message += handle_message diff --git a/examples/ssl_socket.py b/examples/ssl_socket.py index 26625a2..d6eff02 100644 --- a/examples/ssl_socket.py +++ b/examples/ssl_socket.py @@ -2,6 +2,13 @@ import time from pyad2 import AD2 from pyad2.devices import SocketDevice +# Configuration values +HOSTNAME = 'localhost' +PORT = 10000 +SSL_KEY = 'cert.key' +SSL_CERT = 'cert.pem' +SSL_CA = 'ca.pem' + def main(): """ Example application that opens a device that has been exposed to the network @@ -16,9 +23,9 @@ def main(): # The key/cert attributes can either be a filesystem path or an X509/PKey # object from pyopenssl. ssl_device.ssl = True - ssl_device.ssl_key = 'cert.key' # Client private key - ssl_device.ssl_certificate = 'cert.pem' # Client certificate - ssl_device.ssl_ca = 'ca.pem' # CA certificate + ssl_device.ssl_ca = SSL_CA # CA certificate + ssl_device.ssl_key = SSL_KEY # Client private key + ssl_device.ssl_certificate = SSL_CERT # Client certificate device = AD2(ssl_device) diff --git a/examples/virtual_zone_expander.py b/examples/virtual_zone_expander.py index 304e3b9..930eef3 100644 --- a/examples/virtual_zone_expander.py +++ b/examples/virtual_zone_expander.py @@ -2,6 +2,10 @@ import time from pyad2 import AD2 from pyad2.devices import USBDevice +# Configuration values +TARGET_ZONE = 41 +WAIT_TIME = 10 + def main(): """ Example application that periodically faults a virtual zone and then @@ -36,12 +40,11 @@ def main(): # Wait for events. last_update = time.time() while True: - if time.time() - last_update > 10: - # Fault zone 41 every 10 seconds. - device.fault_zone(41) - + if time.time() - last_update > WAIT_TIME: last_update = time.time() + device.fault_zone(TARGET_ZONE) + time.sleep(1) except Exception, ex: