Browse Source

Better error handling in firmware upload.

Made SocketDevice behave similarly to the others with regards to blocking.
Added ability to retrieve current configuration string.
pyserial_fix
Scott Petersen 9 years ago
parent
commit
d75749b612
3 changed files with 28 additions and 12 deletions
  1. +4
    -4
      alarmdecoder/decoder.py
  2. +12
    -2
      alarmdecoder/devices.py
  3. +12
    -6
      alarmdecoder/util.py

+ 4
- 4
alarmdecoder/decoder.py View File

@@ -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):
"""


+ 12
- 2
alarmdecoder/devices.py View File

@@ -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)



+ 12
- 6
alarmdecoder/util.py View File

@@ -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.")

Loading…
Cancel
Save