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.

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