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.

221 lines
5.1 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_power_changed(sender, args):
  24. print 'power changed', args
  25. def handle_alarm_bell(sender, args):
  26. print 'alarm', args
  27. def handle_bypass(sender, args):
  28. print 'bypass', args
  29. def handle_firmware(stage):
  30. if stage == pyad2usb.ad2usb.util.Firmware.STAGE_START:
  31. handle_firmware.wait_tick = 0
  32. handle_firmware.upload_tick = 0
  33. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_WAITING:
  34. if handle_firmware.wait_tick == 0:
  35. sys.stdout.write('Waiting for device.')
  36. handle_firmware.wait_tick += 1
  37. sys.stdout.write('.')
  38. sys.stdout.flush()
  39. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT:
  40. print "\r\nRebooting device.."
  41. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
  42. print 'Waiting for boot loader..'
  43. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
  44. if handle_firmware.upload_tick == 0:
  45. sys.stdout.write('Uploading firmware.')
  46. handle_firmware.upload_tick += 1
  47. if handle_firmware.upload_tick % 30 == 0:
  48. sys.stdout.write('.')
  49. sys.stdout.flush()
  50. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE:
  51. print "\r\nDone!"
  52. def upload_usb():
  53. dev = pyad2usb.ad2usb.devices.USBDevice()
  54. dev.open()
  55. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  56. dev.close()
  57. def upload_serial():
  58. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  59. dev.open()
  60. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  61. dev.close()
  62. def upload_usb_serial():
  63. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  64. dev.open(baudrate=115200)
  65. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  66. dev.close()
  67. def upload_socket():
  68. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  69. dev.open()
  70. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  71. dev.close()
  72. def test_usb():
  73. dev = pyad2usb.ad2usb.devices.USBDevice()
  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.on_power_changed += handle_power_changed
  80. a2u.on_alarm += handle_alarm_bell
  81. a2u.on_bypass += handle_bypass
  82. a2u.open()
  83. while running:
  84. time.sleep(0.1)
  85. a2u.close()
  86. def test_serial():
  87. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  88. a2u = pyad2usb.ad2usb.AD2USB(dev)
  89. a2u.on_open += handle_open
  90. a2u.on_close += handle_close
  91. a2u.on_read += handle_read
  92. a2u.on_write += handle_write
  93. a2u.open()
  94. while running:
  95. time.sleep(0.1)
  96. a2u.close()
  97. def test_usb_serial():
  98. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  99. a2u = pyad2usb.ad2usb.AD2USB(dev)
  100. a2u.on_open += handle_open
  101. a2u.on_close += handle_close
  102. a2u.on_read += handle_read
  103. a2u.on_write += handle_write
  104. a2u.open(baudrate=115200)
  105. while running:
  106. time.sleep(0.1)
  107. a2u.close()
  108. def test_factory():
  109. a2u = pyad2usb.ad2usb.Overseer.create()
  110. a2u.on_open += handle_open
  111. a2u.on_close += handle_close
  112. a2u.on_read += handle_read
  113. a2u.on_write += handle_write
  114. a2u.open()
  115. while running:
  116. time.sleep(0.1)
  117. a2u.close()
  118. def test_factory_watcher():
  119. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  120. a2u = overseer.get_device()
  121. a2u.on_open += handle_open
  122. a2u.on_close += handle_close
  123. a2u.on_read += handle_read
  124. a2u.on_write += handle_write
  125. a2u.open()
  126. while running:
  127. time.sleep(0.1)
  128. a2u.close()
  129. overseer.close()
  130. def test_socket():
  131. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("localhost", 10000))
  132. a2u = pyad2usb.ad2usb.AD2USB(dev)
  133. a2u.on_open += handle_open
  134. a2u.on_close += handle_close
  135. a2u.on_read += handle_read
  136. a2u.on_write += handle_write
  137. a2u.on_power_changed += handle_power_changed
  138. a2u.on_alarm += handle_alarm_bell
  139. a2u.on_bypass += handle_bypass
  140. a2u.open()
  141. while running:
  142. time.sleep(0.1)
  143. a2u.close()
  144. try:
  145. signal.signal(signal.SIGINT, signal_handler)
  146. #test_serial()
  147. #upload_serial()
  148. #test_usb()
  149. #test_usb_serial()
  150. #test_factory()
  151. #test_factory_watcher()
  152. #upload_usb()
  153. #upload_usb_serial()
  154. test_socket()
  155. #upload_socket()
  156. except Exception, err:
  157. traceback.print_exc(err)