From 7a5966b46d45da2fc0f3acc79af9e60a43f4a279 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Sun, 9 Jun 2013 16:56:00 -0700 Subject: [PATCH] Config support added. --- pyad2usb/ad2usb.py | 63 ++++++++++++++++++++++++++++++++++++++-------- test.py | 6 ++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/pyad2usb/ad2usb.py b/pyad2usb/ad2usb.py index c69c336..4ae7225 100644 --- a/pyad2usb/ad2usb.py +++ b/pyad2usb/ad2usb.py @@ -5,6 +5,7 @@ Provides the full AD2USB class and factory. import time import threading import re +import logging from .event import event from . import devices from . import util @@ -181,9 +182,13 @@ class AD2USB(object): self._alarm_status = None self._bypass_status = None - self._settings = {} - - self._address_mask = 0xFF80 # TEMP + self.address = 18 + self.configbits = 0xFF00 + self.address_mask = 0x00000000 + self.emulate_zone = [False for x in range(5)] + self.emulate_relay = [False for x in range(4)] + self.emulate_lrr = False + self.deduplicate = False def open(self, baudrate=None, interface=None, index=None, no_reader_thread=False): """ @@ -205,11 +210,34 @@ class AD2USB(object): """ self._device.write("C\r") - def set_config(self, settings): + def save_config(self): """ - + Sets configuration entries on the device. """ - pass + config_string = '' + + # HACK: Both of these methods are ugly.. but I can't think of an elegant way of doing it. + + #config_string += 'ADDRESS={0}&'.format(self.address) + #config_string += 'CONFIGBITS={0:x}&'.format(self.configbits) + #config_string += 'MASK={0:x}&'.format(self.address_mask) + #config_string += 'EXP={0}&'.format(''.join(['Y' if z else 'N' for z in self.emulate_zone])) + #config_string += 'REL={0}&'.format(''.join(['Y' if r else 'N' for r in self.emulate_relay])) + #config_string += 'LRR={0}&'.format('Y' if self.emulate_lrr else 'N') + #config_string += 'DEDUPLICATE={0}'.format('Y' if self.deduplicate else 'N') + + config_entries = [] + config_entries.append(('ADDRESS', '{0}'.format(self.address))) + config_entries.append(('CONFIGBITS', '{0:x}'.format(self.configbits))) + config_entries.append(('MASK', '{0:x}'.format(self.address_mask))) + config_entries.append(('EXP', ''.join(['Y' if z else 'N' for z in self.emulate_zone]))) + config_entries.append(('REL', ''.join(['Y' if r else 'N' for r in self.emulate_relay]))) + config_entries.append(('LRR', 'Y' if self.emulate_lrr else 'N')) + config_entries.append(('DEDUPLICATE', 'Y' if self.deduplicate else 'N')) + + config_string = '&'.join(['='.join(t) for t in config_entries]) + + self._device.write("C{0}\r".format(config_string)) def reboot(self): """ @@ -242,7 +270,7 @@ class AD2USB(object): if data[0] != '!': msg = Message(data) - if self._address_mask & msg.mask > 0: + if self.address_mask & msg.mask > 0: self._update_internal_states(msg) else: # specialty messages @@ -266,9 +294,24 @@ class AD2USB(object): for setting in config_string.split('&'): k, v = setting.split('=') - self._settings[k] = v - - self.on_config_received(self._settings) + if k == 'ADDRESS': + self.address = int(v) + elif k == 'CONFIGBITS': + self.configbits = int(v, 16) + elif k == 'MASK': + self.address_mask = int(v, 16) + elif k == 'EXP': + for z in range(5): + self.emulate_zone[z] = True if v[z] == 'Y' else False + elif k == 'REL': + for r in range(4): + self.emulate_relay[r] = True if v[r] == 'Y' else False + elif k == 'LRR': + self.emulate_lrr = True if v == 'Y' else False + elif k == 'DEDUPLICATE': + self.deduplicate = True if v == 'Y' else False + + self.on_config_received() def _update_internal_states(self, message): if message.ac_power != self._power_status: diff --git a/test.py b/test.py index 4a7ccd7..02e70df 100755 --- a/test.py +++ b/test.py @@ -5,6 +5,7 @@ import time import signal import traceback import sys +import logging running = True @@ -221,11 +222,9 @@ def test_socket(): a2u.on_config_received += handle_config a2u.open() + #a2u.save_config() #a2u.reboot() a2u.get_config() - print pyad2usb.ad2usb.AD2USB.F1 - - print dev._id while running: time.sleep(0.1) @@ -289,6 +288,7 @@ def test_double_panel_write(): dev2.close() try: + logging.basicConfig(level=logging.DEBUG) signal.signal(signal.SIGINT, signal_handler) #test_serial()