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.

382 lines
8.9 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. from OpenSSL import SSL
  9. running = True
  10. def signal_handler(signal, frame):
  11. global running
  12. running = False
  13. def handle_open(sender, args):
  14. print 'O', args
  15. def handle_close(sender, args):
  16. print 'C', args
  17. def handle_read(sender, args):
  18. print '<', args
  19. def handle_write(sender, args):
  20. print '>', args
  21. def handle_attached(sender, args):
  22. print '+', args
  23. def handle_detached(sender, args):
  24. print '-', args
  25. def handle_power_changed(sender, args):
  26. print 'power changed', args
  27. def handle_alarm_bell(sender, args):
  28. print 'alarm', args
  29. def handle_bypass(sender, args):
  30. print 'bypass', args
  31. def handle_message(sender, args):
  32. print args
  33. def handle_arm(sender, args):
  34. print 'armed', args
  35. def handle_disarm(sender, args):
  36. print 'disarmed', args
  37. def handle_firmware(stage):
  38. if stage == pyad2usb.ad2usb.util.Firmware.STAGE_START:
  39. handle_firmware.wait_tick = 0
  40. handle_firmware.upload_tick = 0
  41. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_WAITING:
  42. if handle_firmware.wait_tick == 0:
  43. sys.stdout.write('Waiting for device.')
  44. handle_firmware.wait_tick += 1
  45. sys.stdout.write('.')
  46. sys.stdout.flush()
  47. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT:
  48. if handle_firmware.wait_tick > 0: print ""
  49. print "Rebooting device.."
  50. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
  51. print 'Waiting for boot loader..'
  52. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
  53. if handle_firmware.upload_tick == 0:
  54. sys.stdout.write('Uploading firmware.')
  55. handle_firmware.upload_tick += 1
  56. if handle_firmware.upload_tick % 30 == 0:
  57. sys.stdout.write('.')
  58. sys.stdout.flush()
  59. elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_DONE:
  60. print "\r\nDone!"
  61. def handle_boot(sender, args):
  62. print 'boot', args
  63. def handle_config(sender, args):
  64. print 'config', args
  65. def handle_fault(sender, args):
  66. print 'zone fault', args
  67. def handle_restore(sender, args):
  68. print 'zone restored', args
  69. def handle_battery(sender, args):
  70. print 'low battery', args
  71. def handle_fire(sender, args):
  72. print 'FIRE!', args
  73. def handle_lrr(sender, args):
  74. print 'LRR', args
  75. def handle_panic(sender, args):
  76. print 'PANIC!', args
  77. def handle_rfx(sender, args):
  78. print 'RFX', args
  79. def handle_relay(sender, args):
  80. print 'RELAY', args
  81. def upload_usb():
  82. dev = pyad2usb.ad2usb.devices.USBDevice()
  83. dev.open(no_reader_thread=True)
  84. #pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  85. dev.close()
  86. def upload_serial():
  87. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  88. dev.open(no_reader_thread=True)
  89. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  90. #pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu-V2-2a-5-beta20-C4.hex', handle_firmware)
  91. dev.close()
  92. def upload_usb_serial():
  93. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  94. dev.open(baudrate=115200)
  95. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  96. dev.close()
  97. def upload_socket():
  98. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=('localhost', 10000))
  99. dev.open()
  100. pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
  101. dev.close()
  102. def test_usb():
  103. dev = pyad2usb.ad2usb.devices.USBDevice()
  104. a2u = pyad2usb.ad2usb.AD2USB(dev)
  105. a2u.on_open += handle_open
  106. a2u.on_close += handle_close
  107. a2u.on_read += handle_read
  108. a2u.on_write += handle_write
  109. a2u.on_power_changed += handle_power_changed
  110. a2u.on_alarm += handle_alarm_bell
  111. a2u.on_bypass += handle_bypass
  112. a2u.open()
  113. print dev._id
  114. while running:
  115. time.sleep(0.1)
  116. a2u.close()
  117. def test_serial():
  118. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  119. a2u = pyad2usb.ad2usb.AD2USB(dev)
  120. a2u.on_open += handle_open
  121. a2u.on_close += handle_close
  122. #a2u.on_read += handle_read
  123. #a2u.on_write += handle_write
  124. a2u.on_message += handle_message
  125. a2u.on_power_changed += handle_power_changed
  126. a2u.on_alarm += handle_alarm_bell
  127. a2u.on_bypass += handle_bypass
  128. a2u.open(no_reader_thread=False)
  129. print a2u._device._device
  130. #print a2u._device.read_line()
  131. #dev.open()
  132. print dev._id
  133. while running:
  134. time.sleep(0.1)
  135. a2u.close()
  136. #dev.close()
  137. def test_usb_serial():
  138. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  139. a2u = pyad2usb.ad2usb.AD2USB(dev)
  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(baudrate=115200)
  145. print dev._id
  146. while running:
  147. time.sleep(0.1)
  148. a2u.close()
  149. def test_factory():
  150. a2u = pyad2usb.ad2usb.Overseer.create()
  151. a2u.on_open += handle_open
  152. a2u.on_close += handle_close
  153. a2u.on_read += handle_read
  154. a2u.on_write += handle_write
  155. a2u.open()
  156. while running:
  157. time.sleep(0.1)
  158. a2u.close()
  159. def test_factory_watcher():
  160. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  161. a2u = overseer.get_device()
  162. a2u.on_open += handle_open
  163. a2u.on_close += handle_close
  164. a2u.on_read += handle_read
  165. a2u.on_write += handle_write
  166. a2u.open()
  167. while running:
  168. time.sleep(0.1)
  169. a2u.close()
  170. overseer.close()
  171. def test_socket():
  172. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("10.10.0.1", 10000), use_ssl=True, ssl_certificate='tmp/certs/client1.pem', ssl_key='tmp/certs/client1.key', ssl_ca='tmp/certs/ca.pem')
  173. a2u = pyad2usb.ad2usb.AD2USB(dev)
  174. a2u.on_open += handle_open
  175. a2u.on_close += handle_close
  176. a2u.on_read += handle_read
  177. #a2u.on_write += handle_write
  178. #
  179. #a2u.on_message += handle_message
  180. #a2u.on_power_changed += handle_power_changed
  181. #a2u.on_alarm += handle_alarm_bell
  182. #a2u.on_bypass += handle_bypass
  183. #a2u.on_boot += handle_boot
  184. #a2u.on_config_received += handle_config
  185. #a2u.on_arm += handle_arm
  186. #a2u.on_disarm += handle_disarm
  187. a2u.on_zone_fault += handle_fault
  188. a2u.on_zone_restore += handle_restore
  189. a2u.on_rfx_message += handle_rfx
  190. a2u.on_relay_changed += handle_relay
  191. #
  192. #a2u.on_fire += handle_fire
  193. #a2u.on_low_battery += handle_battery
  194. #a2u.on_lrr_message += handle_lrr
  195. #a2u.on_panic += handle_panic
  196. a2u.open()
  197. #a2u.save_config()
  198. #a2u.reboot()
  199. #a2u.get_config()
  200. #a2u.address = 18
  201. #a2u.configbits = 0xff00
  202. #a2u.address_mask = 0xFFFFFFFF
  203. #a2u.emulate_zone[0] = False
  204. #a2u.emulate_relay[0] = False
  205. #a2u.emulate_lrr = False
  206. #a2u.deduplicate = False
  207. time.sleep(3)
  208. a2u.get_config()
  209. #a2u.emulate_zone[1] = False
  210. #a2u.save_config()
  211. #time.sleep(1)
  212. #a2u.fault_zone(17, True)
  213. #time.sleep(15)
  214. #a2u.clear_zone(17)
  215. #time.sleep(1)
  216. #a2u.fault_zone((2, 2), True)
  217. while running:
  218. time.sleep(0.1)
  219. a2u.close()
  220. def test_no_read_thread():
  221. #a2u = pyad2usb.ad2usb.Overseer.create()
  222. #a2u.on_open += handle_open
  223. #a2u.on_close += handle_close
  224. #a2u.on_read += handle_read
  225. #a2u.on_write += handle_write
  226. #a2u.open(no_reader_thread=True)
  227. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  228. dev.open(no_reader_thread=True)
  229. #print 'alive?', a2u._device._read_thread.is_alive()
  230. while 1:
  231. line = dev.read_line(timeout=5)
  232. print line
  233. line = dev.read_line(timeout=5)
  234. print line
  235. #time.sleep(0.1)
  236. dev.close()
  237. def test_serial_grep():
  238. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840')
  239. for x in re:
  240. print x
  241. def test_double_panel_write():
  242. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  243. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  244. dev.open(no_reader_thread=True)
  245. print dev._device
  246. dev2.open(no_reader_thread=True)
  247. print dev2._device
  248. #print a2u._device.read_line()
  249. #dev.open()
  250. print 'Writing characters..'
  251. dev.write('*****')
  252. dev2.write('*****')
  253. print 'Reading..'
  254. dev_res = dev.read_line()
  255. print dev.id, dev_res
  256. dev2_res = dev2.read_line()
  257. print dev2.id, dev2_res
  258. dev.close()
  259. dev2.close()
  260. try:
  261. logging.basicConfig(level=logging.DEBUG)
  262. signal.signal(signal.SIGINT, signal_handler)
  263. #test_serial()
  264. #upload_serial()
  265. #test_usb()
  266. #test_usb_serial()
  267. #test_factory()
  268. #test_factory_watcher()
  269. #upload_usb()
  270. #upload_usb_serial()
  271. test_socket()
  272. #upload_socket()
  273. #test_no_read_thread()
  274. #test_serial_grep()
  275. #test_double_panel_write()
  276. except Exception, err:
  277. traceback.print_exc(err)