A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.6 KiB

  1. import ad2usb
  2. import time
  3. import traceback
  4. class NoDeviceError(Exception):
  5. pass
  6. class CommError(Exception):
  7. pass
  8. class TimeoutError(Exception):
  9. pass
  10. class Firmware(object):
  11. STAGE_START = 0
  12. STAGE_WAITING = 1
  13. STAGE_BOOT = 2
  14. STAGE_LOAD = 3
  15. STAGE_UPLOADING = 4
  16. STAGE_DONE = 5
  17. def __init__(self):
  18. pass
  19. def __del__(self):
  20. pass
  21. @staticmethod
  22. def upload(dev, filename, progress_callback=None):
  23. def do_upload():
  24. with open(filename) as f:
  25. for line in f:
  26. line = line.rstrip()
  27. if line[0] == ':':
  28. dev.write(line + "\r")
  29. res = dev.read_line()
  30. if progress_callback is not None:
  31. progress_callback(Firmware.STAGE_UPLOADING)
  32. time.sleep(0.05)
  33. def read_until(pattern, timeout=0.0):
  34. start_time = time.time()
  35. buf = ''
  36. position = 0
  37. while True:
  38. try:
  39. char = dev.read()
  40. if char is not None and char != '':
  41. if char == pattern[position]:
  42. position = position + 1
  43. if position == len(pattern):
  44. break
  45. else:
  46. position = 0
  47. except Exception, err:
  48. traceback.print_exc(err)
  49. if timeout > 0 and time.time() - start_time > timeout:
  50. raise TimeoutError('Timed out waiting for pattern: {0}'.format(pattern))
  51. if dev is None:
  52. raise NoDeviceError('No device specified for firmware upload.')
  53. if progress_callback is not None:
  54. progress_callback(Firmware.STAGE_START)
  55. dev.close_reader()
  56. while dev._read_thread.is_alive():
  57. if progress_callback is not None:
  58. progress_callback(Firmware.STAGE_WAITING)
  59. time.sleep(1)
  60. try:
  61. if progress_callback is not None:
  62. progress_callback(Firmware.STAGE_BOOT)
  63. dev.write("=")
  64. read_until('!boot', timeout=10.0)
  65. if progress_callback is not None:
  66. progress_callback(Firmware.STAGE_LOAD)
  67. dev.write("=")
  68. read_until('!load', timeout=10.0)
  69. do_upload()
  70. if progress_callback is not None:
  71. progress_callback(Firmware.STAGE_DONE)
  72. except TimeoutError, err:
  73. print traceback.print_exc(err)
  74. pass