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.

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