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.

489 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. msg.ignore_packet = True
  165. print msg.ignore_packet
  166. def _on_open(self, sender, args):
  167. """
  168. Internal handler for opening the device.
  169. """
  170. self.on_open(args)
  171. def _on_close(self, sender, args):
  172. """
  173. Internal handler for closing the device.
  174. """
  175. self.on_close(args)
  176. def _on_read(self, sender, args):
  177. """
  178. Internal handler for reading from the device.
  179. """
  180. msg = self._handle_message(args)
  181. if msg:
  182. self.on_message(msg)
  183. self.on_read(args)
  184. def _on_write(self, sender, args):
  185. """
  186. Internal handler for writing to the device.
  187. """
  188. self.on_write(args)
  189. class Message(object):
  190. """
  191. Represents a message from the alarm panel.
  192. """
  193. def __init__(self):
  194. """
  195. Constructor
  196. """
  197. self._ignore_packet = False
  198. self._ready = False
  199. self._armed_away = False
  200. self._armed_home = False
  201. self._backlight = False
  202. self._programming_mode = False
  203. self._beeps = -1
  204. self._bypass = False
  205. self._ac = False
  206. self._chime_mode = False
  207. self._alarm_event_occurred = False
  208. self._alarm_bell = False
  209. self._numeric = ""
  210. self._text = ""
  211. self._cursor = -1
  212. self._raw = ""
  213. @property
  214. def ignore_packet(self):
  215. """
  216. Indicates whether or not this message should be ignored.
  217. """
  218. return self._ignore_packet
  219. @ignore_packet.setter
  220. def ignore_packet(self, value):
  221. """
  222. Sets the value indicating whether or not this packet should be ignored.
  223. """
  224. self._ignore_packet = value
  225. @property
  226. def ready(self):
  227. """
  228. Indicates whether or not the panel is ready.
  229. """
  230. return self._ready
  231. @ready.setter
  232. def ready(self, value):
  233. """
  234. Sets the value indicating whether or not the panel is ready.
  235. """
  236. self._ready = value
  237. @property
  238. def armed_away(self):
  239. """
  240. Indicates whether or not the panel is armed in away mode.
  241. """
  242. return self._armed_away
  243. @armed_away.setter
  244. def armed_away(self, value):
  245. """
  246. Sets the value indicating whether or not the panel is armed in away mode.
  247. """
  248. self._armed_away = value
  249. @property
  250. def armed_home(self):
  251. """
  252. Indicates whether or not the panel is armed in home/stay mode.
  253. """
  254. return self._armed_home
  255. @armed_home.setter
  256. def armed_home(self, value):
  257. """
  258. Sets the value indicating whether or not the panel is armed in home/stay mode.
  259. """
  260. self._armed_home = value
  261. @property
  262. def backlight(self):
  263. """
  264. Indicates whether or not the panel backlight is on.
  265. """
  266. return self._backlight
  267. @backlight.setter
  268. def backlight(self, value):
  269. """
  270. Sets the value indicating whether or not the panel backlight is on.
  271. """
  272. self._backlight = value
  273. @property
  274. def programming_mode(self):
  275. """
  276. Indicates whether or not the panel is in programming mode.
  277. """
  278. return self._programming_mode
  279. @programming_mode.setter
  280. def programming_mode(self, value):
  281. """
  282. Sets the value indicating whether or not the panel is in programming mode.
  283. """
  284. self._programming_mode = value
  285. @property
  286. def beeps(self):
  287. """
  288. Returns the number of beeps associated with this message.
  289. """
  290. return self._beeps
  291. @beeps.setter
  292. def beeps(self, value):
  293. """
  294. Sets the number of beeps associated with this message.
  295. """
  296. self._beeps = value
  297. @property
  298. def bypass(self):
  299. """
  300. Indicates whether or not zones have been bypassed.
  301. """
  302. return self._bypass
  303. @bypass.setter
  304. def bypass(self, value):
  305. """
  306. Sets the value indicating whether or not zones have been bypassed.
  307. """
  308. self._bypass = value
  309. @property
  310. def ac(self):
  311. """
  312. Indicates whether or not the system is on AC power.
  313. """
  314. return self._ac
  315. @ac.setter
  316. def ac(self, value):
  317. """
  318. Sets the value indicating whether or not the system is on AC power.
  319. """
  320. self._ac = value
  321. @property
  322. def chime_mode(self):
  323. """
  324. Indicates whether or not panel chimes are enabled.
  325. """
  326. return self._chime_mode
  327. @chime_mode.setter
  328. def chime_mode(self, value):
  329. """
  330. Sets the value indicating whether or not the panel chimes are enabled.
  331. """
  332. self._chime_mode = value
  333. @property
  334. def alarm_event_occurred(self):
  335. """
  336. Indicates whether or not an alarm event has occurred.
  337. """
  338. return self._alarm_event_occurred
  339. @alarm_event_occurred.setter
  340. def alarm_event_occurred(self, value):
  341. """
  342. Sets the value indicating whether or not an alarm event has occurred.
  343. """
  344. self._alarm_event_occurred = value
  345. @property
  346. def alarm_bell(self):
  347. """
  348. Indicates whether or not an alarm is currently sounding.
  349. """
  350. return self._alarm_bell
  351. @alarm_bell.setter
  352. def alarm_bell(self, value):
  353. """
  354. Sets the value indicating whether or not an alarm is currently sounding.
  355. """
  356. self._alarm_bell = value
  357. @property
  358. def numeric(self):
  359. """
  360. Numeric indicator of associated with message. For example: If zone #3 is faulted, this value is 003.
  361. """
  362. return self._numeric
  363. @numeric.setter
  364. def numeric(self, value):
  365. """
  366. Sets the numeric indicator associated with this message.
  367. """
  368. self._numeric = value
  369. @property
  370. def text(self):
  371. """
  372. Alphanumeric text associated with this message.
  373. """
  374. return self._text
  375. @text.setter
  376. def text(self, value):
  377. """
  378. Sets the alphanumeric text associated with this message.
  379. """
  380. self._text = value
  381. @property
  382. def cursor(self):
  383. """
  384. Indicates which text position has the cursor underneath it.
  385. """
  386. return self._cursor
  387. @cursor.setter
  388. def cursor(self, value):
  389. """
  390. Sets the value indicating which text position has the cursor underneath it.
  391. """
  392. self._cursor = value
  393. @property
  394. def raw(self):
  395. """
  396. Raw representation of the message data from the panel.
  397. """
  398. return self._raw
  399. @raw.setter
  400. def raw(self, value):
  401. """
  402. Sets the raw representation of the message data from the panel.
  403. """
  404. self._raw = value