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.

242 lines
5.5 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. if handle_firmware.wait_tick > 0: print ""
  43. print "Rebooting device.."
  44. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
  45. print 'Waiting for boot loader..'
  46. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
  47. if handle_firmware.upload_tick == 0:
  48. sys.stdout.write('Uploading firmware.')
  49. handle_firmware.upload_tick += 1
  50. if handle_firmware.upload_tick % 30 == 0:
  51. sys.stdout.write('.')
  52. sys.stdout.flush()
  53. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE:
  54. print "\r\nDone!"
  55. def upload_usb():
  56. dev = pyad2usb.ad2usb.devices.USBDevice()
  57. dev.open(no_read_thread=True)
  58. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  59. dev.close()
  60. def upload_serial():
  61. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  62. dev.open()
  63. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  64. dev.close()
  65. def upload_usb_serial():
  66. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  67. dev.open(baudrate=115200)
  68. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  69. dev.close()
  70. def upload_socket():
  71. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  72. dev.open()
  73. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  74. dev.close()
  75. def test_usb():
  76. dev = pyad2usb.ad2usb.devices.USBDevice()
  77. a2u = pyad2usb.ad2usb.AD2USB(dev)
  78. a2u.on_open += handle_open
  79. a2u.on_close += handle_close
  80. a2u.on_read += handle_read
  81. a2u.on_write += handle_write
  82. a2u.on_power_changed += handle_power_changed
  83. a2u.on_alarm += handle_alarm_bell
  84. a2u.on_bypass += handle_bypass
  85. a2u.open()
  86. while running:
  87. time.sleep(0.1)
  88. a2u.close()
  89. def test_serial():
  90. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  91. a2u = pyad2usb.ad2usb.AD2USB(dev)
  92. a2u.on_open += handle_open
  93. a2u.on_close += handle_close
  94. a2u.on_read += handle_read
  95. a2u.on_write += handle_write
  96. a2u.open()
  97. while running:
  98. time.sleep(0.1)
  99. a2u.close()
  100. def test_usb_serial():
  101. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  102. a2u = pyad2usb.ad2usb.AD2USB(dev)
  103. a2u.on_open += handle_open
  104. a2u.on_close += handle_close
  105. a2u.on_read += handle_read
  106. a2u.on_write += handle_write
  107. a2u.open(baudrate=115200)
  108. while running:
  109. time.sleep(0.1)
  110. a2u.close()
  111. def test_factory():
  112. a2u = pyad2usb.ad2usb.Overseer.create()
  113. a2u.on_open += handle_open
  114. a2u.on_close += handle_close
  115. a2u.on_read += handle_read
  116. a2u.on_write += handle_write
  117. a2u.open()
  118. while running:
  119. time.sleep(0.1)
  120. a2u.close()
  121. def test_factory_watcher():
  122. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  123. a2u = overseer.get_device()
  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. overseer.close()
  133. def test_socket():
  134. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("localhost", 10000))
  135. a2u = pyad2usb.ad2usb.AD2USB(dev)
  136. a2u.on_open += handle_open
  137. a2u.on_close += handle_close
  138. #a2u.on_read += handle_read
  139. #a2u.on_write += handle_write
  140. a2u.on_message += handle_message
  141. a2u.on_power_changed += handle_power_changed
  142. a2u.on_alarm += handle_alarm_bell
  143. a2u.on_bypass += handle_bypass
  144. a2u.open()
  145. while running:
  146. time.sleep(0.1)
  147. a2u.close()
  148. def test_no_read_thread():
  149. a2u = pyad2usb.ad2usb.Overseer.create()
  150. a2u.on_open += handle_open
  151. a2u.on_close += handle_close
  152. a2u.on_read += handle_read
  153. a2u.on_write += handle_write
  154. a2u.open(no_read_thread=True)
  155. print 'alive?', a2u._device._read_thread.is_alive()
  156. a2u.close()
  157. try:
  158. signal.signal(signal.SIGINT, signal_handler)
  159. #test_serial()
  160. #upload_serial()
  161. #test_usb()
  162. #test_usb_serial()
  163. #test_factory()
  164. #test_factory_watcher()
  165. upload_usb()
  166. #upload_usb_serial()
  167. #test_socket()
  168. #upload_socket()
  169. #test_no_read_thread()
  170. except Exception, err:
  171. traceback.print_exc(err)