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.

303 lines
6.9 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_reader_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/ttyUSB2')
  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. print dev._id
  87. while running:
  88. time.sleep(0.1)
  89. a2u.close()
  90. def test_serial():
  91. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  92. a2u = pyad2usb.ad2usb.AD2USB(dev)
  93. a2u.on_open += handle_open
  94. a2u.on_close += handle_close
  95. #a2u.on_read += handle_read
  96. #a2u.on_write += handle_write
  97. a2u.on_message += handle_message
  98. a2u.on_power_changed += handle_power_changed
  99. a2u.on_alarm += handle_alarm_bell
  100. a2u.on_bypass += handle_bypass
  101. a2u.open(no_reader_thread=False)
  102. print a2u._device._device
  103. #print a2u._device.read_line()
  104. #dev.open()
  105. print dev._id
  106. while running:
  107. time.sleep(0.1)
  108. a2u.close()
  109. #dev.close()
  110. def test_usb_serial():
  111. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  112. a2u = pyad2usb.ad2usb.AD2USB(dev)
  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(baudrate=115200)
  118. print dev._id
  119. while running:
  120. time.sleep(0.1)
  121. a2u.close()
  122. def test_factory():
  123. a2u = pyad2usb.ad2usb.Overseer.create()
  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. def test_factory_watcher():
  133. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  134. a2u = overseer.get_device()
  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.open()
  140. while running:
  141. time.sleep(0.1)
  142. a2u.close()
  143. overseer.close()
  144. def test_socket():
  145. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("localhost", 10000))
  146. a2u = pyad2usb.ad2usb.AD2USB(dev)
  147. a2u.on_open += handle_open
  148. a2u.on_close += handle_close
  149. #a2u.on_read += handle_read
  150. #a2u.on_write += handle_write
  151. a2u.on_message += handle_message
  152. a2u.on_power_changed += handle_power_changed
  153. a2u.on_alarm += handle_alarm_bell
  154. a2u.on_bypass += handle_bypass
  155. a2u.open()
  156. print dev._id
  157. while running:
  158. time.sleep(0.1)
  159. a2u.close()
  160. def test_no_read_thread():
  161. #a2u = pyad2usb.ad2usb.Overseer.create()
  162. #a2u.on_open += handle_open
  163. #a2u.on_close += handle_close
  164. #a2u.on_read += handle_read
  165. #a2u.on_write += handle_write
  166. #a2u.open(no_reader_thread=True)
  167. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  168. dev.open(no_reader_thread=True)
  169. #print 'alive?', a2u._device._read_thread.is_alive()
  170. while 1:
  171. line = dev.read_line(timeout=5)
  172. print line
  173. line = dev.read_line(timeout=5)
  174. print line
  175. #time.sleep(0.1)
  176. dev.close()
  177. def test_serial_grep():
  178. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840')
  179. for x in re:
  180. print x
  181. def test_double_panel_write():
  182. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  183. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  184. dev.open(no_reader_thread=True)
  185. print dev._device
  186. dev2.open(no_reader_thread=True)
  187. print dev2._device
  188. #print a2u._device.read_line()
  189. #dev.open()
  190. print 'Writing characters..'
  191. dev.write('*****')
  192. dev2.write('*****')
  193. print 'Reading..'
  194. dev_res = dev.read_line()
  195. print dev.id, dev_res
  196. dev2_res = dev2.read_line()
  197. print dev2.id, dev2_res
  198. dev.close()
  199. dev2.close()
  200. try:
  201. signal.signal(signal.SIGINT, signal_handler)
  202. #test_serial()
  203. upload_serial()
  204. #test_usb()
  205. #test_usb_serial()
  206. #test_factory()
  207. #test_factory_watcher()
  208. #upload_usb()
  209. #upload_usb_serial()
  210. #test_socket()
  211. #upload_socket()
  212. #test_no_read_thread()
  213. #test_serial_grep()
  214. #test_double_panel_write()
  215. except Exception, err:
  216. traceback.print_exc(err)