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.

353 lines
8.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_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 handle_fault(sender, args):
  65. print 'zone fault', args
  66. def handle_restore(sender, args):
  67. print 'zone restored', args
  68. def upload_usb():
  69. dev = pyad2usb.ad2usb.devices.USBDevice()
  70. dev.open(no_reader_thread=True)
  71. #pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  72. dev.close()
  73. def upload_serial():
  74. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  75. dev.open(no_reader_thread=True)
  76. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  77. #pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu-V2-2a-5-beta20-C4.hex', handle_firmware)
  78. dev.close()
  79. def upload_usb_serial():
  80. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  81. dev.open(baudrate=115200)
  82. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  83. dev.close()
  84. def upload_socket():
  85. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  86. dev.open()
  87. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  88. dev.close()
  89. def test_usb():
  90. dev = pyad2usb.ad2usb.devices.USBDevice()
  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.on_power_changed += handle_power_changed
  97. a2u.on_alarm += handle_alarm_bell
  98. a2u.on_bypass += handle_bypass
  99. a2u.open()
  100. print dev._id
  101. while running:
  102. time.sleep(0.1)
  103. a2u.close()
  104. def test_serial():
  105. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  106. a2u = pyad2usb.ad2usb.AD2USB(dev)
  107. a2u.on_open += handle_open
  108. a2u.on_close += handle_close
  109. #a2u.on_read += handle_read
  110. #a2u.on_write += handle_write
  111. a2u.on_message += handle_message
  112. a2u.on_power_changed += handle_power_changed
  113. a2u.on_alarm += handle_alarm_bell
  114. a2u.on_bypass += handle_bypass
  115. a2u.open(no_reader_thread=False)
  116. print a2u._device._device
  117. #print a2u._device.read_line()
  118. #dev.open()
  119. print dev._id
  120. while running:
  121. time.sleep(0.1)
  122. a2u.close()
  123. #dev.close()
  124. def test_usb_serial():
  125. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  126. a2u = pyad2usb.ad2usb.AD2USB(dev)
  127. a2u.on_open += handle_open
  128. a2u.on_close += handle_close
  129. a2u.on_read += handle_read
  130. a2u.on_write += handle_write
  131. a2u.open(baudrate=115200)
  132. print dev._id
  133. while running:
  134. time.sleep(0.1)
  135. a2u.close()
  136. def test_factory():
  137. a2u = pyad2usb.ad2usb.Overseer.create()
  138. a2u.on_open += handle_open
  139. a2u.on_close += handle_close
  140. a2u.on_read += handle_read
  141. a2u.on_write += handle_write
  142. a2u.open()
  143. while running:
  144. time.sleep(0.1)
  145. a2u.close()
  146. def test_factory_watcher():
  147. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  148. a2u = overseer.get_device()
  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.open()
  154. while running:
  155. time.sleep(0.1)
  156. a2u.close()
  157. overseer.close()
  158. def test_socket():
  159. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("singularity.corp.nutech.com", 10000))
  160. a2u = pyad2usb.ad2usb.AD2USB(dev)
  161. a2u.on_open += handle_open
  162. a2u.on_close += handle_close
  163. #a2u.on_read += handle_read
  164. #a2u.on_write += handle_write
  165. a2u.on_message += handle_message
  166. a2u.on_power_changed += handle_power_changed
  167. a2u.on_alarm += handle_alarm_bell
  168. a2u.on_bypass += handle_bypass
  169. a2u.on_boot += handle_boot
  170. a2u.on_config_received += handle_config
  171. a2u.on_arm += handle_arm
  172. a2u.on_disarm += handle_disarm
  173. a2u.on_zone_fault += handle_fault
  174. a2u.on_zone_restore += handle_restore
  175. a2u.open()
  176. #a2u.save_config()
  177. #a2u.reboot()
  178. a2u.get_config()
  179. #a2u.address = 18
  180. #a2u.configbits = 0xff00
  181. #a2u.address_mask = 0xFFFFFFFF
  182. #a2u.emulate_zone[0] = False
  183. #a2u.emulate_relay[0] = False
  184. #a2u.emulate_lrr = False
  185. #a2u.deduplicate = False
  186. #time.sleep(3)
  187. #a2u.emulate_zone[1] = True
  188. #a2u.save_config()
  189. time.sleep(1)
  190. a2u.fault_zone(17, True)
  191. time.sleep(15)
  192. a2u.clear_zone(17)
  193. #time.sleep(1)
  194. #a2u.fault_zone((2, 2), True)
  195. while running:
  196. time.sleep(0.1)
  197. a2u.close()
  198. def test_no_read_thread():
  199. #a2u = pyad2usb.ad2usb.Overseer.create()
  200. #a2u.on_open += handle_open
  201. #a2u.on_close += handle_close
  202. #a2u.on_read += handle_read
  203. #a2u.on_write += handle_write
  204. #a2u.open(no_reader_thread=True)
  205. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  206. dev.open(no_reader_thread=True)
  207. #print 'alive?', a2u._device._read_thread.is_alive()
  208. while 1:
  209. line = dev.read_line(timeout=5)
  210. print line
  211. line = dev.read_line(timeout=5)
  212. print line
  213. #time.sleep(0.1)
  214. dev.close()
  215. def test_serial_grep():
  216. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840')
  217. for x in re:
  218. print x
  219. def test_double_panel_write():
  220. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  221. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  222. dev.open(no_reader_thread=True)
  223. print dev._device
  224. dev2.open(no_reader_thread=True)
  225. print dev2._device
  226. #print a2u._device.read_line()
  227. #dev.open()
  228. print 'Writing characters..'
  229. dev.write('*****')
  230. dev2.write('*****')
  231. print 'Reading..'
  232. dev_res = dev.read_line()
  233. print dev.id, dev_res
  234. dev2_res = dev2.read_line()
  235. print dev2.id, dev2_res
  236. dev.close()
  237. dev2.close()
  238. try:
  239. logging.basicConfig(level=logging.DEBUG)
  240. signal.signal(signal.SIGINT, signal_handler)
  241. #test_serial()
  242. #upload_serial()
  243. #test_usb()
  244. #test_usb_serial()
  245. #test_factory()
  246. #test_factory_watcher()
  247. #upload_usb()
  248. #upload_usb_serial()
  249. test_socket()
  250. #upload_socket()
  251. #test_no_read_thread()
  252. #test_serial_grep()
  253. #test_double_panel_write()
  254. except Exception, err:
  255. traceback.print_exc(err)