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.

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