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.

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