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.

308 lines
7.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. 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 handle_boot(sender, args):
  56. print 'boot', args
  57. def upload_usb():
  58. dev = pyad2usb.ad2usb.devices.USBDevice()
  59. dev.open(no_reader_thread=True)
  60. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  61. dev.close()
  62. def upload_serial():
  63. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB2')
  64. dev.open()
  65. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  66. dev.close()
  67. def upload_usb_serial():
  68. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  69. dev.open(baudrate=115200)
  70. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  71. dev.close()
  72. def upload_socket():
  73. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  74. dev.open()
  75. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  76. dev.close()
  77. def test_usb():
  78. dev = pyad2usb.ad2usb.devices.USBDevice()
  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.on_power_changed += handle_power_changed
  85. a2u.on_alarm += handle_alarm_bell
  86. a2u.on_bypass += handle_bypass
  87. a2u.open()
  88. print dev._id
  89. while running:
  90. time.sleep(0.1)
  91. a2u.close()
  92. def test_serial():
  93. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  94. a2u = pyad2usb.ad2usb.AD2USB(dev)
  95. a2u.on_open += handle_open
  96. a2u.on_close += handle_close
  97. #a2u.on_read += handle_read
  98. #a2u.on_write += handle_write
  99. a2u.on_message += handle_message
  100. a2u.on_power_changed += handle_power_changed
  101. a2u.on_alarm += handle_alarm_bell
  102. a2u.on_bypass += handle_bypass
  103. a2u.open(no_reader_thread=False)
  104. print a2u._device._device
  105. #print a2u._device.read_line()
  106. #dev.open()
  107. print dev._id
  108. while running:
  109. time.sleep(0.1)
  110. a2u.close()
  111. #dev.close()
  112. def test_usb_serial():
  113. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  114. a2u = pyad2usb.ad2usb.AD2USB(dev)
  115. a2u.on_open += handle_open
  116. a2u.on_close += handle_close
  117. a2u.on_read += handle_read
  118. a2u.on_write += handle_write
  119. a2u.open(baudrate=115200)
  120. print dev._id
  121. while running:
  122. time.sleep(0.1)
  123. a2u.close()
  124. def test_factory():
  125. a2u = pyad2usb.ad2usb.Overseer.create()
  126. a2u.on_open += handle_open
  127. a2u.on_close += handle_close
  128. a2u.on_read += handle_read
  129. a2u.on_write += handle_write
  130. a2u.open()
  131. while running:
  132. time.sleep(0.1)
  133. a2u.close()
  134. def test_factory_watcher():
  135. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  136. a2u = overseer.get_device()
  137. a2u.on_open += handle_open
  138. a2u.on_close += handle_close
  139. a2u.on_read += handle_read
  140. a2u.on_write += handle_write
  141. a2u.open()
  142. while running:
  143. time.sleep(0.1)
  144. a2u.close()
  145. overseer.close()
  146. def test_socket():
  147. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("singularity.corp.nutech.com", 10000))
  148. a2u = pyad2usb.ad2usb.AD2USB(dev)
  149. a2u.on_open += handle_open
  150. a2u.on_close += handle_close
  151. #a2u.on_read += handle_read
  152. #a2u.on_write += handle_write
  153. a2u.on_message += handle_message
  154. a2u.on_power_changed += handle_power_changed
  155. a2u.on_alarm += handle_alarm_bell
  156. a2u.on_bypass += handle_bypass
  157. a2u.on_boot += handle_boot
  158. a2u.open()
  159. a2u.reboot()
  160. print dev._id
  161. while running:
  162. time.sleep(0.1)
  163. a2u.close()
  164. def test_no_read_thread():
  165. #a2u = pyad2usb.ad2usb.Overseer.create()
  166. #a2u.on_open += handle_open
  167. #a2u.on_close += handle_close
  168. #a2u.on_read += handle_read
  169. #a2u.on_write += handle_write
  170. #a2u.open(no_reader_thread=True)
  171. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  172. dev.open(no_reader_thread=True)
  173. #print 'alive?', a2u._device._read_thread.is_alive()
  174. while 1:
  175. line = dev.read_line(timeout=5)
  176. print line
  177. line = dev.read_line(timeout=5)
  178. print line
  179. #time.sleep(0.1)
  180. dev.close()
  181. def test_serial_grep():
  182. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840')
  183. for x in re:
  184. print x
  185. def test_double_panel_write():
  186. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  187. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  188. dev.open(no_reader_thread=True)
  189. print dev._device
  190. dev2.open(no_reader_thread=True)
  191. print dev2._device
  192. #print a2u._device.read_line()
  193. #dev.open()
  194. print 'Writing characters..'
  195. dev.write('*****')
  196. dev2.write('*****')
  197. print 'Reading..'
  198. dev_res = dev.read_line()
  199. print dev.id, dev_res
  200. dev2_res = dev2.read_line()
  201. print dev2.id, dev2_res
  202. dev.close()
  203. dev2.close()
  204. try:
  205. signal.signal(signal.SIGINT, signal_handler)
  206. #test_serial()
  207. #upload_serial()
  208. #test_usb()
  209. #test_usb_serial()
  210. #test_factory()
  211. #test_factory_watcher()
  212. #upload_usb()
  213. #upload_usb_serial()
  214. test_socket()
  215. #upload_socket()
  216. #test_no_read_thread()
  217. #test_serial_grep()
  218. #test_double_panel_write()
  219. except Exception, err:
  220. traceback.print_exc(err)