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.

178 lines
4.0 KiB

  1. #!/usr/bin/env python
  2. import pyad2usb.ad2usb
  3. import time
  4. import signal
  5. import traceback
  6. import sys
  7. running = True
  8. def signal_handler(signal, frame):
  9. global running
  10. running = False
  11. def handle_open(sender, args):
  12. print 'O', args
  13. def handle_close(sender, args):
  14. print 'C', args
  15. def handle_read(sender, args):
  16. print '<', args
  17. def handle_write(sender, args):
  18. print '>', args
  19. def handle_attached(sender, args):
  20. print '+', args
  21. def handle_detached(sender, args):
  22. print '-', args
  23. def handle_firmware(stage):
  24. if stage == pyad2usb.ad2usb.util.Firmware.STAGE_START:
  25. handle_firmware.wait_tick = 0
  26. handle_firmware.upload_tick = 0
  27. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_WAITING:
  28. if handle_firmware.wait_tick == 0:
  29. sys.stdout.write('Waiting for device.')
  30. handle_firmware.wait_tick += 1
  31. sys.stdout.write('.')
  32. sys.stdout.flush()
  33. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT:
  34. print "\r\nRebooting device.."
  35. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
  36. print 'Waiting for boot loader..'
  37. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
  38. if handle_firmware.upload_tick == 0:
  39. sys.stdout.write('Uploading firmware.')
  40. handle_firmware.upload_tick += 1
  41. if handle_firmware.upload_tick % 30 == 0:
  42. sys.stdout.write('.')
  43. sys.stdout.flush()
  44. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE:
  45. print "\r\nDone!"
  46. def upload_usb():
  47. dev = pyad2usb.ad2usb.devices.USBDevice()
  48. dev.open()
  49. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  50. dev.close()
  51. def upload_serial():
  52. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  53. dev.open()
  54. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  55. dev.close()
  56. def upload_usb_serial():
  57. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  58. dev.open(baudrate=115200)
  59. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  60. dev.close()
  61. def test_usb():
  62. dev = pyad2usb.ad2usb.devices.USBDevice()
  63. a2u = pyad2usb.ad2usb.AD2USB(dev)
  64. a2u.on_open += handle_open
  65. a2u.on_close += handle_close
  66. a2u.on_read += handle_read
  67. a2u.on_write += handle_write
  68. a2u.open()
  69. while running:
  70. time.sleep(0.1)
  71. a2u.close()
  72. def test_serial():
  73. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  74. a2u = pyad2usb.ad2usb.AD2USB(dev)
  75. a2u.on_open += handle_open
  76. a2u.on_close += handle_close
  77. a2u.on_read += handle_read
  78. a2u.on_write += handle_write
  79. a2u.open()
  80. while running:
  81. time.sleep(0.1)
  82. a2u.close()
  83. def test_usb_serial():
  84. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  85. a2u = pyad2usb.ad2usb.AD2USB(dev)
  86. a2u.on_open += handle_open
  87. a2u.on_close += handle_close
  88. a2u.on_read += handle_read
  89. a2u.on_write += handle_write
  90. a2u.open(baudrate=115200)
  91. while running:
  92. time.sleep(0.1)
  93. a2u.close()
  94. def test_factory():
  95. a2u = pyad2usb.ad2usb.Overseer.create()
  96. a2u.on_open += handle_open
  97. a2u.on_close += handle_close
  98. a2u.on_read += handle_read
  99. a2u.on_write += handle_write
  100. a2u.open()
  101. while running:
  102. time.sleep(0.1)
  103. a2u.close()
  104. def test_factory_watcher():
  105. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  106. a2u = overseer.get_device()
  107. a2u.on_open += handle_open
  108. a2u.on_close += handle_close
  109. a2u.on_read += handle_read
  110. a2u.on_write += handle_write
  111. a2u.open()
  112. while running:
  113. time.sleep(0.1)
  114. a2u.close()
  115. overseer.close()
  116. try:
  117. signal.signal(signal.SIGINT, signal_handler)
  118. #test_serial()
  119. #upload_serial()
  120. test_usb()
  121. #test_usb_serial()
  122. #test_factory()
  123. #test_factory_watcher()
  124. #upload_usb()
  125. #upload_usb_serial()
  126. except Exception, err:
  127. traceback.print_exc(err)