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.

487 lines
12 KiB

  1. """
  2. Provides the full AD2USB class and factory.
  3. """
  4. import time
  5. import threading
  6. from .event import event
  7. from . import devices
  8. from . import util
  9. class Overseer(object):
  10. """
  11. Factory for creation of AD2USB devices as well as provide4s attach/detach events."
  12. """
  13. # Factory events
  14. on_attached = event.Event('Called when an AD2USB device has been detected.')
  15. on_detached = event.Event('Called when an AD2USB device has been removed.')
  16. __devices = []
  17. @classmethod
  18. def find_all(cls):
  19. """
  20. Returns all AD2USB devices located on the system.
  21. """
  22. cls.__devices = devices.USBDevice.find_all()
  23. return cls.__devices
  24. @classmethod
  25. def devices(cls):
  26. """
  27. Returns a cached list of AD2USB devices located on the system.
  28. """
  29. return cls.__devices
  30. @classmethod
  31. def create(cls, device=None):
  32. """
  33. Factory method that returns the requested AD2USB device, or the first device.
  34. """
  35. cls.find_all()
  36. if len(cls.__devices) == 0:
  37. raise util.NoDeviceError('No AD2USB devices present.')
  38. if device is None:
  39. device = cls.__devices[0]
  40. vendor, product, sernum, ifcount, description = device
  41. device = devices.USBDevice(serial=sernum, description=description)
  42. return AD2USB(device)
  43. def __init__(self, attached_event=None, detached_event=None):
  44. """
  45. Constructor
  46. """
  47. self._detect_thread = Overseer.DetectThread(self)
  48. if attached_event:
  49. self.on_attached += attached_event
  50. if detached_event:
  51. self.on_detached += detached_event
  52. Overseer.find_all()
  53. self.start()
  54. def __del__(self):
  55. """
  56. Destructor
  57. """
  58. pass
  59. def close(self):
  60. """
  61. Clean up and shut down.
  62. """
  63. self.stop()
  64. def start(self):
  65. """
  66. Starts the detection thread, if not already running.
  67. """
  68. if not self._detect_thread.is_alive():
  69. self._detect_thread.start()
  70. def stop(self):
  71. """
  72. Stops the detection thread.
  73. """
  74. self._detect_thread.stop()
  75. def get_device(self, device=None):
  76. """
  77. Factory method that returns the requested AD2USB device, or the first device.
  78. """
  79. return Overseer.create(device)
  80. class DetectThread(threading.Thread):
  81. """
  82. Thread that handles detection of added/removed devices.
  83. """
  84. def __init__(self, overseer):
  85. """
  86. Constructor
  87. """
  88. threading.Thread.__init__(self)
  89. self._overseer = overseer
  90. self._running = False
  91. def stop(self):
  92. """
  93. Stops the thread.
  94. """
  95. self._running = False
  96. def run(self):
  97. """
  98. The actual detection process.
  99. """
  100. self._running = True
  101. last_devices = set()
  102. while self._running:
  103. try:
  104. Overseer.find_all()
  105. current_devices = set(Overseer.devices())
  106. new_devices = [d for d in current_devices if d not in last_devices]
  107. removed_devices = [d for d in last_devices if d not in current_devices]
  108. last_devices = current_devices
  109. for d in new_devices:
  110. self._overseer.on_attached(d)
  111. for d in removed_devices:
  112. self._overseer.on_detached(d)
  113. except util.CommError, err:
  114. pass
  115. time.sleep(0.25)
  116. class AD2USB(object):
  117. """
  118. High-level wrapper around AD2USB/AD2SERIAL devices.
  119. """
  120. # High-level Events
  121. on_open = event.Event('Called when the device has been opened')
  122. on_close = event.Event('Called when the device has been closed')
  123. on_message = event.Event('Called when a message has been received from the device.')
  124. # Low-level Events
  125. on_read = event.Event('Called when a line has been read from the device')
  126. on_write = event.Event('Called when data has been written to the device')
  127. def __init__(self, device):
  128. """
  129. Constructor
  130. """
  131. self._device = device
  132. def __del__(self):
  133. """
  134. Destructor
  135. """
  136. pass
  137. def open(self, baudrate=None, interface=None, index=None):
  138. """
  139. Opens the device.
  140. """
  141. self._wire_events()
  142. self._device.open(baudrate=baudrate, interface=interface, index=index)
  143. def close(self):
  144. """
  145. Closes the device.
  146. """
  147. self._device.close()
  148. self._device = None
  149. def _wire_events(self):
  150. """
  151. Wires up the internal device events.
  152. """
  153. self._device.on_open += self._on_open
  154. self._device.on_close += self._on_close
  155. self._device.on_read += self._on_read
  156. self._device.on_write += self._on_write
  157. def _handle_message(self, data):
  158. """
  159. Parses messages from the panel.
  160. """
  161. if data[0] == '!': # TEMP: Remove this.
  162. return None
  163. msg = Message()
  164. # parse and build stuff
  165. def _on_open(self, sender, args):
  166. """
  167. Internal handler for opening the device.
  168. """
  169. self.on_open(args)
  170. def _on_close(self, sender, args):
  171. """
  172. Internal handler for closing the device.
  173. """
  174. self.on_close(args)
  175. def _on_read(self, sender, args):
  176. """
  177. Internal handler for reading from the device.
  178. """
  179. msg = self._handle_message(args)
  180. if msg:
  181. self.on_message(msg)
  182. self.on_read(args)
  183. def _on_write(self, sender, args):
  184. """
  185. Internal handler for writing to the device.
  186. """
  187. self.on_write(args)
  188. class Message(object):
  189. """
  190. Represents a message from the alarm panel.
  191. """
  192. def __init__(self):
  193. """
  194. Constructor
  195. """
  196. self._ignore_packet = False
  197. self._ready = False
  198. self._armed_away = False
  199. self._armed_home = False
  200. self._backlight = False
  201. self._programming_mode = False
  202. self._beeps = -1
  203. self._bypass = False
  204. self._ac = False
  205. self._chime_mode = False
  206. self._alarm_event_occurred = False
  207. self._alarm_bell = False
  208. self._numeric = ""
  209. self._text = ""
  210. self._cursor = -1
  211. self._raw = ""
  212. @property
  213. def ignore_packet(self):
  214. """
  215. Indicates whether or not this message should be ignored.
  216. """
  217. return self._ignore_packet
  218. @ignore_packet.setter
  219. def ignore_packet(self, value):
  220. """
  221. Sets the value indicating whether or not this packet should be ignored.
  222. """
  223. self._ignore_packet = value
  224. @property
  225. def ready(self):
  226. """
  227. Indicates whether or not the panel is ready.
  228. """
  229. return self._ready
  230. @ready.setter
  231. def ready(self, value):
  232. """
  233. Sets the value indicating whether or not the panel is ready.
  234. """
  235. self._ready = value
  236. @property
  237. def armed_away(self):
  238. """
  239. Indicates whether or not the panel is armed in away mode.
  240. """
  241. return self._armed_away
  242. @armed_away.setter
  243. def armed_away(self, value):
  244. """
  245. Sets the value indicating whether or not the panel is armed in away mode.
  246. """
  247. self._armed_away = value
  248. @property
  249. def armed_home(self):
  250. """
  251. Indicates whether or not the panel is armed in home/stay mode.
  252. """
  253. return self._armed_home
  254. @armed_home.setter
  255. def armed_home(self, value):
  256. """
  257. Sets the value indicating whether or not the panel is armed in home/stay mode.
  258. """
  259. self._armed_home = value
  260. @property
  261. def backlight(self):
  262. """
  263. Indicates whether or not the panel backlight is on.
  264. """
  265. return self._backlight
  266. @backlight.setter
  267. def backlight(self, value):
  268. """
  269. Sets the value indicating whether or not the panel backlight is on.
  270. """
  271. self._backlight = value
  272. @property
  273. def programming_mode(self):
  274. """
  275. Indicates whether or not the panel is in programming mode.
  276. """
  277. return self._programming_mode
  278. @programming_mode.setter
  279. def programming_mode(self, value):
  280. """
  281. Sets the value indicating whether or not the panel is in programming mode.
  282. """
  283. self._programming_mode = value
  284. @property
  285. def beeps(self):
  286. """
  287. Returns the number of beeps associated with this message.
  288. """
  289. return self._beeps
  290. @beeps.setter
  291. def beeps(self, value):
  292. """
  293. Sets the number of beeps associated with this message.
  294. """
  295. self._beeps = value
  296. @property
  297. def bypass(self):
  298. """
  299. Indicates whether or not zones have been bypassed.
  300. """
  301. return self._bypass
  302. @bypass.setter
  303. def bypass(self, value):
  304. """
  305. Sets the value indicating whether or not zones have been bypassed.
  306. """
  307. self._bypass = value
  308. @property
  309. def ac(self):
  310. """
  311. Indicates whether or not the system is on AC power.
  312. """
  313. return self._ac
  314. @ac.setter
  315. def ac(self, value):
  316. """
  317. Sets the value indicating whether or not the system is on AC power.
  318. """
  319. self._ac = value
  320. @property
  321. def chime_mode(self):
  322. """
  323. Indicates whether or not panel chimes are enabled.
  324. """
  325. return self._chime_mode
  326. @chime_mode.setter
  327. def chime_mode(self, value):
  328. """
  329. Sets the value indicating whether or not the panel chimes are enabled.
  330. """
  331. self._chime_mode = value
  332. @property
  333. def alarm_event_occurred(self):
  334. """
  335. Indicates whether or not an alarm event has occurred.
  336. """
  337. return self._alarm_event_occurred
  338. @alarm_event_occurred.setter
  339. def alarm_event_occurred(self, value):
  340. """
  341. Sets the value indicating whether or not an alarm event has occurred.
  342. """
  343. self._alarm_event_occurred = value
  344. @property
  345. def alarm_bell(self):
  346. """
  347. Indicates whether or not an alarm is currently sounding.
  348. """
  349. return self._alarm_bell
  350. @alarm_bell.setter
  351. def alarm_bell(self, value):
  352. """
  353. Sets the value indicating whether or not an alarm is currently sounding.
  354. """
  355. self._alarm_bell = value
  356. @property
  357. def numeric(self):
  358. """
  359. Numeric indicator of associated with message. For example: If zone #3 is faulted, this value is 003.
  360. """
  361. return self._numeric
  362. @numeric.setter
  363. def numeric(self, value):
  364. """
  365. Sets the numeric indicator associated with this message.
  366. """
  367. self._numeric = value
  368. @property
  369. def text(self):
  370. """
  371. Alphanumeric text associated with this message.
  372. """
  373. return self._text
  374. @text.setter
  375. def text(self, value):
  376. """
  377. Sets the alphanumeric text associated with this message.
  378. """
  379. self._text = value
  380. @property
  381. def cursor(self):
  382. """
  383. Indicates which text position has the cursor underneath it.
  384. """
  385. return self._cursor
  386. @cursor.setter
  387. def cursor(self, value):
  388. """
  389. Sets the value indicating which text position has the cursor underneath it.
  390. """
  391. self._cursor = value
  392. @property
  393. def raw(self):
  394. """
  395. Raw representation of the message data from the panel.
  396. """
  397. return self._raw
  398. @raw.setter
  399. def raw(self, value):
  400. """
  401. Sets the raw representation of the message data from the panel.
  402. """
  403. self._raw = value