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.

401 lines
9.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. 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. global running
  104. dev = pyad2usb.ad2usb.devices.USBDevice(interface=(0, 0))
  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_zone_fault += handle_fault
  112. a2u.on_zone_restore += handle_restore
  113. #a2u.on_power_changed += handle_power_changed
  114. #a2u.on_alarm += handle_alarm_bell
  115. #a2u.on_bypass += handle_bypass
  116. a2u.open()
  117. print dev.id
  118. while running:
  119. time.sleep(0.1)
  120. a2u.close()
  121. def test_serial():
  122. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB2')
  123. a2u = pyad2usb.ad2usb.AD2USB(dev)
  124. a2u.on_open += handle_open
  125. a2u.on_close += handle_close
  126. #a2u.on_read += handle_read
  127. #a2u.on_write += handle_write
  128. a2u.on_message += handle_message
  129. a2u.on_power_changed += handle_power_changed
  130. a2u.on_alarm += handle_alarm_bell
  131. a2u.on_bypass += handle_bypass
  132. a2u.open(no_reader_thread=False)
  133. print a2u._device._device
  134. #print a2u._device.read_line()
  135. #dev.open()
  136. print dev._id
  137. time.sleep(1)
  138. a2u.send('V')
  139. while running:
  140. time.sleep(0.1)
  141. a2u.close()
  142. #dev.close()
  143. def test_usb_serial():
  144. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB1')
  145. #a2u = pyad2usb.ad2usb.AD2USB(dev)
  146. dev.on_open += handle_open
  147. dev.on_close += handle_close
  148. dev.on_read += handle_read
  149. dev.on_write += handle_write
  150. dev.open(baudrate=115200)
  151. print dev._id
  152. while running:
  153. time.sleep(0.1)
  154. dev.close()
  155. def test_factory():
  156. a2u = pyad2usb.ad2usb.Overseer.create()
  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. def test_factory_watcher():
  166. overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
  167. a2u = overseer.get_device()
  168. a2u.on_open += handle_open
  169. a2u.on_close += handle_close
  170. a2u.on_read += handle_read
  171. a2u.on_write += handle_write
  172. a2u.open()
  173. while running:
  174. time.sleep(0.1)
  175. a2u.close()
  176. overseer.close()
  177. def test_socket():
  178. dev = pyad2usb.ad2usb.devices.SocketDevice(interface=("10.10.0.1", 10000))
  179. dev.ssl = True
  180. dev.ssl_certificate = '../certs-temp/client.pem'
  181. dev.ssl_key = '../certs-temp/client.key'
  182. dev.ssl_ca = '../certs-temp/ca.pem'
  183. a2u = pyad2usb.ad2usb.AD2USB(dev)
  184. a2u.on_open += handle_open
  185. a2u.on_close += handle_close
  186. a2u.on_read += handle_read
  187. #a2u.on_write += handle_write
  188. #
  189. a2u.on_message += handle_message
  190. #a2u.on_power_changed += handle_power_changed
  191. #a2u.on_alarm += handle_alarm_bell
  192. #a2u.on_bypass += handle_bypass
  193. #a2u.on_boot += handle_boot
  194. #a2u.on_config_received += handle_config
  195. #a2u.on_arm += handle_arm
  196. #a2u.on_disarm += handle_disarm
  197. a2u.on_zone_fault += handle_fault
  198. a2u.on_zone_restore += handle_restore
  199. a2u.on_rfx_message += handle_rfx
  200. a2u.on_relay_changed += handle_relay
  201. #
  202. #a2u.on_fire += handle_fire
  203. #a2u.on_low_battery += handle_battery
  204. #a2u.on_lrr_message += handle_lrr
  205. #a2u.on_panic += handle_panic
  206. a2u.open()
  207. #a2u.save_config()
  208. #a2u.reboot()
  209. #a2u.get_config()
  210. #a2u.address = 18
  211. #a2u.configbits = 0xff00
  212. #a2u.address_mask = 0xFFFFFFFF
  213. #a2u.emulate_zone[0] = False
  214. #a2u.emulate_relay[0] = False
  215. #a2u.emulate_lrr = False
  216. #a2u.deduplicate = False
  217. time.sleep(3)
  218. a2u.get_config()
  219. #a2u.emulate_zone[1] = False
  220. #a2u.save_config()
  221. #time.sleep(1)
  222. #a2u.fault_zone(17, True)
  223. #time.sleep(15)
  224. #a2u.clear_zone(17)
  225. #time.sleep(1)
  226. #a2u.fault_zone((2, 2), True)
  227. while running:
  228. time.sleep(0.1)
  229. a2u.close()
  230. def test_no_read_thread():
  231. #a2u = pyad2usb.ad2usb.Overseer.create()
  232. #a2u.on_open += handle_open
  233. #a2u.on_close += handle_close
  234. #a2u.on_read += handle_read
  235. #a2u.on_write += handle_write
  236. #a2u.open(no_reader_thread=True)
  237. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
  238. dev.open(no_reader_thread=True)
  239. #print 'alive?', a2u._device._read_thread.is_alive()
  240. while 1:
  241. line = dev.read_line(timeout=5)
  242. print line
  243. line = dev.read_line(timeout=5)
  244. print line
  245. #time.sleep(0.1)
  246. dev.close()
  247. def test_serial_grep():
  248. re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=0403:6001')
  249. print 'serial'
  250. for x in re:
  251. print x
  252. print 'usb'
  253. re = pyad2usb.devices.USBDevice.find_all()
  254. for x in re:
  255. print x
  256. def test_double_panel_write():
  257. dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB4')
  258. dev2 = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB5')
  259. dev.open(no_reader_thread=True)
  260. print dev._device
  261. dev2.open(no_reader_thread=True)
  262. print dev2._device
  263. #print a2u._device.read_line()
  264. #dev.open()
  265. print 'Writing characters..'
  266. dev.write('*****')
  267. dev2.write('*****')
  268. print 'Reading..'
  269. dev_res = dev.read_line()
  270. print dev.id, dev_res
  271. dev2_res = dev2.read_line()
  272. print dev2.id, dev2_res
  273. dev.close()
  274. dev2.close()
  275. try:
  276. logging.basicConfig(level=logging.DEBUG)
  277. signal.signal(signal.SIGINT, signal_handler)
  278. test_serial()
  279. #upload_serial()
  280. #test_usb()
  281. #test_usb_serial()
  282. #test_factory()
  283. #test_factory_watcher()
  284. #upload_usb()
  285. #upload_usb_serial()
  286. #test_socket()
  287. #upload_socket()
  288. #test_no_read_thread()
  289. #test_serial_grep()
  290. #test_double_panel_write()
  291. except Exception, err:
  292. traceback.print_exc(err)