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.

322 lines
7.4 KiB

  1. #!/usr/bin/env python
  2. import pyad2usb.ad2usb
  3. import time
  4. import signal
  5. import traceback
  6. import sys
  7. import logging
  8. running = True
  9. def signal_handler(signal, frame):
  10. global running
  11. running = False
  12. def handle_open(sender, args):
  13. print 'O', args
  14. def handle_close(sender, args):
  15. print 'C', args
  16. def handle_read(sender, args):
  17. print '<', args
  18. def handle_write(sender, args):
  19. print '>', args
  20. def handle_attached(sender, args):
  21. print '+', args
  22. def handle_detached(sender, args):
  23. print '-', args
  24. def handle_power_changed(sender, args):
  25. print 'power changed', args
  26. def handle_alarm_bell(sender, args):
  27. print 'alarm', args
  28. def handle_bypass(sender, args):
  29. print 'bypass', args
  30. def handle_message(sender, args):
  31. print args
  32. def handle_arm(sender, args):
  33. print 'armed', args
  34. def handle_disarm(sender, args):
  35. print 'disarmed', args
  36. def handle_firmware(stage):
  37. if stage == pyad2usb.ad2usb.util.Firmware.STAGE_START:
  38. handle_firmware.wait_tick = 0
  39. handle_firmware.upload_tick = 0
  40. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_WAITING:
  41. if handle_firmware.wait_tick == 0:
  42. sys.stdout.write('Waiting for device.')
  43. handle_firmware.wait_tick += 1
  44. sys.stdout.write('.')
  45. sys.stdout.flush()
  46. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT:
  47. if handle_firmware.wait_tick > 0: print ""
  48. print "Rebooting device.."
  49. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
  50. print 'Waiting for boot loader..'
  51. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
  52. if handle_firmware.upload_tick == 0:
  53. sys.stdout.write('Uploading firmware.')
  54. handle_firmware.upload_tick += 1
  55. if handle_firmware.upload_tick % 30 == 0:
  56. sys.stdout.write('.')
  57. sys.stdout.flush()
  58. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE:
  59. print "\r\nDone!"
  60. def handle_boot(sender, args):
  61. print 'boot', args
  62. def handle_config(sender, args):
  63. print 'config', args
  64. def upload_usb():
  65. dev = pyad2usb.ad2usb.devices.USBDevice()
  66. dev.open(no_reader_thread=True)
  67. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  68. dev.close()
  69. def upload_serial():
  70. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB2')
  71. dev.open()
  72. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  73. dev.close()
  74. def upload_usb_serial():
  75. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  76. dev.open(baudrate=115200)
  77. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  78. dev.close()
  79. def upload_socket():
  80. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  81. dev.open()
  82. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  83. dev.close()
  84. def test_usb():
  85. dev = pyad2usb.ad2usb.devices.USBDevice()
  86. a2u = pyad2usb.ad2usb.AD2USB(dev)
  87. a2u.on_open += handle_open
  88. a2u.on_close += handle_close
  89. a2u.on_read += handle_read
  90. a2u.on_write += handle_write
  91. a2u.on_power_changed += handle_power_changed
  92. a2u.on_alarm += handle_alarm_bell
  93. a2u.on_bypass += handle_bypass
  94. a2u.open()
  95. print dev._id
  96. while running:
  97. time.sleep(0.1)
  98. a2u.close()
  99. def test_serial():
  100. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  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.on_message += handle_message
  107. a2u.on_power_changed += handle_power_changed
  108. a2u.on_alarm += handle_alarm_bell
  109. a2u.on_bypass += handle_bypass
  110. a2u.open(no_reader_thread=False)
  111. print a2u._device._device
  112. #print a2u._device.read_line()
  113. #dev.open()
  114. print dev._id
  115. while running:
  116. time.sleep(0.1)
  117. a2u.close()
  118. #dev.close()
  119. def test_usb_serial():
  120. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  121. a2u = pyad2usb.ad2usb.AD2USB(dev)
  122. a2u.on_open += handle_open
  123. a2u.on_close += handle_close
  124. a2u.on_read += handle_read
  125. a2u.on_write += handle_write
  126. a2u.open(baudrate=115200)
  127. print dev._id
  128. while running:
  129. time.sleep(0.1)
  130. a2u.close()
  131. def test_factory():
  132. a2u = pyad2usb.ad2usb.Overseer.create()
  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.open()
  138. while running:
  139. time.sleep(0.1)
  140. a2u.close()
  141. def test_factory_watcher():
  142. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  143. a2u = overseer.get_device()
  144. a2u.on_open += handle_open
  145. a2u.on_close += handle_close
  146. a2u.on_read += handle_read
  147. a2u.on_write += handle_write
  148. a2u.open()
  149. while running:
  150. time.sleep(0.1)
  151. a2u.close()
  152. overseer.close()
  153. def test_socket():
  154. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("singularity.corp.nutech.com", 10000))
  155. a2u = pyad2usb.ad2usb.AD2USB(dev)
  156. a2u.on_open += handle_open
  157. a2u.on_close += handle_close
  158. #a2u.on_read += handle_read
  159. #a2u.on_write += handle_write
  160. a2u.on_message += handle_message
  161. a2u.on_power_changed += handle_power_changed
  162. a2u.on_alarm += handle_alarm_bell
  163. a2u.on_bypass += handle_bypass
  164. a2u.on_boot += handle_boot
  165. a2u.on_config_received += handle_config
  166. a2u.on_arm += handle_arm
  167. a2u.on_disarm += handle_disarm
  168. a2u.open()
  169. #a2u.save_config()
  170. #a2u.reboot()
  171. a2u.get_config()
  172. while running:
  173. time.sleep(0.1)
  174. a2u.close()
  175. def test_no_read_thread():
  176. #a2u = pyad2usb.ad2usb.Overseer.create()
  177. #a2u.on_open += handle_open
  178. #a2u.on_close += handle_close
  179. #a2u.on_read += handle_read
  180. #a2u.on_write += handle_write
  181. #a2u.open(no_reader_thread=True)
  182. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  183. dev.open(no_reader_thread=True)
  184. #print 'alive?', a2u._device._read_thread.is_alive()
  185. while 1:
  186. line = dev.read_line(timeout=5)
  187. print line
  188. line = dev.read_line(timeout=5)
  189. print line
  190. #time.sleep(0.1)
  191. dev.close()
  192. def test_serial_grep():
  193. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840')
  194. for x in re:
  195. print x
  196. def test_double_panel_write():
  197. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  198. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  199. dev.open(no_reader_thread=True)
  200. print dev._device
  201. dev2.open(no_reader_thread=True)
  202. print dev2._device
  203. #print a2u._device.read_line()
  204. #dev.open()
  205. print 'Writing characters..'
  206. dev.write('*****')
  207. dev2.write('*****')
  208. print 'Reading..'
  209. dev_res = dev.read_line()
  210. print dev.id, dev_res
  211. dev2_res = dev2.read_line()
  212. print dev2.id, dev2_res
  213. dev.close()
  214. dev2.close()
  215. try:
  216. logging.basicConfig(level=logging.DEBUG)
  217. signal.signal(signal.SIGINT, signal_handler)
  218. #test_serial()
  219. #upload_serial()
  220. #test_usb()
  221. #test_usb_serial()
  222. #test_factory()
  223. #test_factory_watcher()
  224. #upload_usb()
  225. #upload_usb_serial()
  226. test_socket()
  227. #upload_socket()
  228. #test_no_read_thread()
  229. #test_serial_grep()
  230. #test_double_panel_write()
  231. except Exception, err:
  232. traceback.print_exc(err)