diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index ac2198c..69de8e0 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -243,7 +243,9 @@ class AlarmDecoder(object): """ Sets configuration entries on the device. """ - config_string = '' + self.send("C{0}\r".format(self.get_config_string())) + + def get_config_string(self): config_entries = [] # HACK: This is ugly.. but I can't think of an elegant way of doing it. @@ -258,9 +260,7 @@ class AlarmDecoder(object): config_entries.append(('DEDUPLICATE', 'Y' if self.deduplicate else 'N')) config_entries.append(('MODE', PANEL_TYPES.keys()[PANEL_TYPES.values().index(self.mode)])) - config_string = '&'.join(['='.join(t) for t in config_entries]) - - self.send("C{0}\r".format(config_string)) + return '&'.join(['='.join(t) for t in config_entries]) def reboot(self): """ diff --git a/alarmdecoder/devices.py b/alarmdecoder/devices.py index 6b36b60..bddd107 100644 --- a/alarmdecoder/devices.py +++ b/alarmdecoder/devices.py @@ -20,6 +20,7 @@ import threading import serial import serial.tools.list_ports import socket +import select from .util import CommError, TimeoutError, NoDeviceError, InvalidMessageError from .event import event @@ -1002,7 +1003,6 @@ class SocketDevice(Device): self._init_ssl() self._device.connect((self._host, self._port)) - #self._device.setblocking(1) if self._use_ssl: while True: @@ -1082,7 +1082,10 @@ class SocketDevice(Device): data = None try: - data = self._device.recv(1) + read_ready, _, _ = select.select([self._device], [], [], 0) + + if (len(read_ready) != 0): + data = self._device.recv(1) except socket.error, err: raise CommError('Error while reading from device: {0}'.format(str(err)), err) @@ -1119,6 +1122,12 @@ class SocketDevice(Device): try: while timeout_event.reading: + read_ready, _, _ = select.select([self._device], [], [], 0) + + if (len(read_ready) == 0): + time.sleep(0.01) + continue + buf = self._device.recv(1) if buf != '': @@ -1130,6 +1139,7 @@ class SocketDevice(Device): if len(self._buffer) > 0: got_line = True break + else: time.sleep(0.01) diff --git a/alarmdecoder/util.py b/alarmdecoder/util.py index 27b90f2..afd7ebc 100644 --- a/alarmdecoder/util.py +++ b/alarmdecoder/util.py @@ -44,6 +44,7 @@ class UploadError(Exception): """ pass + class UploadChecksumError(UploadError): """ The firmware upload failed due to a checksum error. @@ -172,11 +173,12 @@ class Firmware(object): # Reboot the device and wait for the boot loader. retry = 3 + found_loader = False while retry > 0: try: stage_callback(Firmware.STAGE_BOOT) dev.write("=") - read_until('......', timeout=15.0) + read_until('!boot', timeout=15.0) # Get ourselves into the boot loader and wait for indication # that it's ready for the firmware upload. @@ -188,11 +190,15 @@ class Firmware(object): retry -= 1 else: retry = 0 + found_loader = True # And finally do the upload. - try: - do_upload() - except UploadError, err: - stage_callback(Firmware.STAGE_ERROR, error=err) + if found_loader: + try: + do_upload() + except UploadError, err: + stage_callback(Firmware.STAGE_ERROR, error=str(err)) + else: + stage_callback(Firmware.STAGE_DONE) else: - stage_callback(Firmware.STAGE_DONE) + stage_callback(Firmware.STAGE_ERROR, error="Error entering bootloader.")