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.

204 lines
4.6 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 upload_socket():
  62. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  63. dev.open()
  64. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  65. dev.close()
  66. def test_usb():
  67. dev = pyad2usb.ad2usb.devices.USBDevice()
  68. a2u = pyad2usb.ad2usb.AD2USB(dev)
  69. a2u.on_open += handle_open
  70. a2u.on_close += handle_close
  71. a2u.on_read += handle_read
  72. a2u.on_write += handle_write
  73. a2u.open()
  74. while running:
  75. time.sleep(0.1)
  76. a2u.close()
  77. def test_serial():
  78. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  79. a2u = pyad2usb.ad2usb.AD2USB(dev)
  80. a2u.on_open += handle_open
  81. a2u.on_close += handle_close
  82. a2u.on_read += handle_read
  83. a2u.on_write += handle_write
  84. a2u.open()
  85. while running:
  86. time.sleep(0.1)
  87. a2u.close()
  88. def test_usb_serial():
  89. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  90. a2u = pyad2usb.ad2usb.AD2USB(dev)
  91. a2u.on_open += handle_open
  92. a2u.on_close += handle_close
  93. a2u.on_read += handle_read
  94. a2u.on_write += handle_write
  95. a2u.open(baudrate=115200)
  96. while running:
  97. time.sleep(0.1)
  98. a2u.close()
  99. def test_factory():
  100. a2u = pyad2usb.ad2usb.Overseer.create()
  101. a2u.on_open += handle_open
  102. a2u.on_close += handle_close
  103. a2u.on_read += handle_read
  104. a2u.on_write += handle_write
  105. a2u.open()
  106. while running:
  107. time.sleep(0.1)
  108. a2u.close()
  109. def test_factory_watcher():
  110. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  111. a2u = overseer.get_device()
  112. a2u.on_open += handle_open
  113. a2u.on_close += handle_close
  114. a2u.on_read += handle_read
  115. a2u.on_write += handle_write
  116. a2u.open()
  117. while running:
  118. time.sleep(0.1)
  119. a2u.close()
  120. overseer.close()
  121. def test_socket():
  122. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("localhost", 10000))
  123. a2u = pyad2usb.ad2usb.AD2USB(dev)
  124. a2u.on_open += handle_open
  125. a2u.on_close += handle_close
  126. a2u.on_read += handle_read
  127. a2u.on_write += handle_write
  128. a2u.open()
  129. while running:
  130. time.sleep(0.1)
  131. a2u.close()
  132. try:
  133. signal.signal(signal.SIGINT, signal_handler)
  134. #test_serial()
  135. #upload_serial()
  136. #test_usb()
  137. #test_usb_serial()
  138. #test_factory()
  139. #test_factory_watcher()
  140. #upload_usb()
  141. #upload_usb_serial()
  142. test_socket()
  143. #upload_socket()
  144. except Exception, err:
  145. traceback.print_exc(err)