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.

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