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.

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