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.

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