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.

790 lines
20 KiB

  1. """
  2. Provides the full AD2USB class and factory.
  3. """
  4. import time
  5. import threading
  6. import re
  7. from .event import event
  8. from . import devices
  9. from . import util
  10. class Overseer(object):
  11. """
  12. Factory for creation of AD2USB devices as well as provide4s attach/detach events."
  13. """
  14. # Factory events
  15. on_attached = event.Event('Called when an AD2USB device has been detected.')
  16. on_detached = event.Event('Called when an AD2USB device has been removed.')
  17. __devices = []
  18. @classmethod
  19. def find_all(cls):
  20. """
  21. Returns all AD2USB devices located on the system.
  22. """
  23. cls.__devices = devices.USBDevice.find_all()
  24. return cls.__devices
  25. @classmethod
  26. def devices(cls):
  27. """
  28. Returns a cached list of AD2USB devices located on the system.
  29. """
  30. return cls.__devices
  31. @classmethod
  32. def create(cls, device=None):
  33. """
  34. Factory method that returns the requested AD2USB device, or the first device.
  35. """
  36. cls.find_all()
  37. if len(cls.__devices) == 0:
  38. raise util.NoDeviceError('No AD2USB devices present.')
  39. if device is None:
  40. device = cls.__devices[0]
  41. vendor, product, sernum, ifcount, description = device
  42. device = devices.USBDevice(serial=sernum, description=description)
  43. return AD2USB(device)
  44. def __init__(self, attached_event=None, detached_event=None):
  45. """
  46. Constructor
  47. """
  48. self._detect_thread = Overseer.DetectThread(self)
  49. if attached_event:
  50. self.on_attached += attached_event
  51. if detached_event:
  52. self.on_detached += detached_event
  53. Overseer.find_all()
  54. self.start()
  55. def __del__(self):
  56. """
  57. Destructor
  58. """
  59. pass
  60. def close(self):
  61. """
  62. Clean up and shut down.
  63. """
  64. self.stop()
  65. def start(self):
  66. """
  67. Starts the detection thread, if not already running.
  68. """
  69. if not self._detect_thread.is_alive():
  70. self._detect_thread.start()
  71. def stop(self):
  72. """
  73. Stops the detection thread.
  74. """
  75. self._detect_thread.stop()
  76. def get_device(self, device=None):
  77. """
  78. Factory method that returns the requested AD2USB device, or the first device.
  79. """
  80. return Overseer.create(device)
  81. class DetectThread(threading.Thread):
  82. """
  83. Thread that handles detection of added/removed devices.
  84. """
  85. def __init__(self, overseer):
  86. """
  87. Constructor
  88. """
  89. threading.Thread.__init__(self)
  90. self._overseer = overseer
  91. self._running = False
  92. def stop(self):
  93. """
  94. Stops the thread.
  95. """
  96. self._running = False
  97. def run(self):
  98. """
  99. The actual detection process.
  100. """
  101. self._running = True
  102. last_devices = set()
  103. while self._running:
  104. try:
  105. Overseer.find_all()
  106. current_devices = set(Overseer.devices())
  107. new_devices = [d for d in current_devices if d not in last_devices]
  108. removed_devices = [d for d in last_devices if d not in current_devices]
  109. last_devices = current_devices
  110. for d in new_devices:
  111. self._overseer.on_attached(d)
  112. for d in removed_devices:
  113. self._overseer.on_detached(d)
  114. except util.CommError, err:
  115. pass
  116. time.sleep(0.25)
  117. class AD2USB(object):
  118. """
  119. High-level wrapper around AD2USB/AD2SERIAL devices.
  120. """
  121. # High-level Events
  122. on_open = event.Event('Called when the device has been opened.')
  123. on_close = event.Event('Called when the device has been closed.')
  124. on_status_changed = event.Event('Called when the panel status changes.')
  125. on_power_changed = event.Event('Called when panel power switches between AC and DC.')
  126. on_alarm = event.Event('Called when the alarm is triggered.')
  127. on_bypass = event.Event('Called when a zone is bypassed.')
  128. # Mid-level Events
  129. on_message = event.Event('Called when a message has been received from the device.')
  130. # Low-level Events
  131. on_read = event.Event('Called when a line has been read from the device.')
  132. on_write = event.Event('Called when data has been written to the device.')
  133. def __init__(self, device):
  134. """
  135. Constructor
  136. """
  137. self._power_status = None
  138. self._alarm_status = None
  139. self._bypass_status = None
  140. self._device = device
  141. self._address_mask = 0xFF80 # TEMP
  142. def __del__(self):
  143. """
  144. Destructor
  145. """
  146. pass
  147. def open(self, baudrate=None, interface=None, index=None, no_reader_thread=False):
  148. """
  149. Opens the device.
  150. """
  151. self._wire_events()
  152. self._device.open(baudrate=baudrate, interface=interface, index=index, no_reader_thread=no_reader_thread)
  153. def close(self):
  154. """
  155. Closes the device.
  156. """
  157. self._device.close()
  158. self._device = None
  159. def _wire_events(self):
  160. """
  161. Wires up the internal device events.
  162. """
  163. self._device.on_open += self._on_open
  164. self._device.on_close += self._on_close
  165. self._device.on_read += self._on_read
  166. self._device.on_write += self._on_write
  167. def _handle_message(self, data):
  168. """
  169. Parses messages from the panel.
  170. """
  171. msg = None
  172. if data[0] != '!':
  173. msg = Message(data)
  174. if self._address_mask & msg.mask > 0:
  175. self._update_internal_states(msg)
  176. else: # specialty messages
  177. header = data[0:4]
  178. if header == '!EXP' or header == '!REL':
  179. msg = ExpanderMessage(data)
  180. elif header == '!RFX':
  181. msg = RFMessage(data)
  182. return msg
  183. def _update_internal_states(self, message):
  184. if message.ac_power != self._power_status:
  185. self._power_status, old_status = message.ac_power, self._power_status
  186. if old_status is not None:
  187. self.on_power_changed(self._power_status)
  188. if message.alarm_sounding != self._alarm_status:
  189. self._alarm_status, old_status = message.alarm_sounding, self._alarm_status
  190. if old_status is not None:
  191. self.on_alarm(self._alarm_status)
  192. if message.zone_bypassed != self._bypass_status:
  193. self._bypass_status, old_status = message.zone_bypassed, self._bypass_status
  194. if old_status is not None:
  195. self.on_bypass(self._bypass_status)
  196. def _on_open(self, sender, args):
  197. """
  198. Internal handler for opening the device.
  199. """
  200. self.on_open(args)
  201. def _on_close(self, sender, args):
  202. """
  203. Internal handler for closing the device.
  204. """
  205. self.on_close(args)
  206. def _on_read(self, sender, args):
  207. """
  208. Internal handler for reading from the device.
  209. """
  210. self.on_read(args)
  211. msg = self._handle_message(args)
  212. if msg:
  213. self.on_message(msg)
  214. def _on_write(self, sender, args):
  215. """
  216. Internal handler for writing to the device.
  217. """
  218. self.on_write(args)
  219. class Message(object):
  220. """
  221. Represents a message from the alarm panel.
  222. """
  223. def __init__(self, data=None):
  224. """
  225. Constructor
  226. """
  227. self._ready = False
  228. self._armed_away = False
  229. self._armed_home = False
  230. self._backlight_on = False
  231. self._programming_mode = False
  232. self._beeps = -1
  233. self._zone_bypassed = False
  234. self._ac_power = False
  235. self._chime_on = False
  236. self._alarm_event_occurred = False
  237. self._alarm_sounding = False
  238. self._numeric_code = ""
  239. self._text = ""
  240. self._cursor_location = -1
  241. self._data = ""
  242. self._mask = ""
  243. self._bitfield = ""
  244. self._panel_data = ""
  245. self._regex = re.compile('("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*)')
  246. if data is not None:
  247. self._parse_message(data)
  248. def _parse_message(self, data):
  249. """
  250. Parse the message from the device.
  251. """
  252. m = self._regex.match(data)
  253. if m is None:
  254. raise util.InvalidMessageError('Received invalid message: {0}'.format(data))
  255. self._bitfield, self._numeric_code, self._panel_data, alpha = m.group(1, 2, 3, 4)
  256. self._mask = int(self._panel_data[3:3+8], 16)
  257. self._data = data
  258. self._ready = not self._bitfield[1:2] == "0"
  259. self._armed_away = not self._bitfield[2:3] == "0"
  260. self._armed_home = not self._bitfield[3:4] == "0"
  261. self._backlight_on = not self._bitfield[4:5] == "0"
  262. self._programming_mode = not self._bitfield[5:6] == "0"
  263. self._beeps = int(self._bitfield[6:7], 16)
  264. self._zone_bypassed = not self._bitfield[7:8] == "0"
  265. self._ac_power = not self._bitfield[8:9] == "0"
  266. self._chime_on = not self._bitfield[9:10] == "0"
  267. self._alarm_event_occurred = not self._bitfield[10:11] == "0"
  268. self._alarm_sounding = not self._bitfield[11:12] == "0"
  269. self._text = alpha.strip('"')
  270. if int(self._panel_data[19:21], 16) & 0x01 > 0:
  271. self._cursor_location = int(self._bitfield[21:23], 16) # Alpha character index that the cursor is on.
  272. def __str__(self):
  273. """
  274. String conversion operator.
  275. """
  276. return 'msg > {0:0<9} [{1}{2}{3}] -- ({4}) {5}'.format(hex(self.mask), 1 if self.ready else 0, 1 if self.armed_away else 0, 1 if self.armed_home else 0, self.numeric_code, self.text)
  277. @property
  278. def ready(self):
  279. """
  280. Indicates whether or not the panel is ready.
  281. """
  282. return self._ready
  283. @ready.setter
  284. def ready(self, value):
  285. """
  286. Sets the value indicating whether or not the panel is ready.
  287. """
  288. self._ready = value
  289. @property
  290. def armed_away(self):
  291. """
  292. Indicates whether or not the panel is armed in away mode.
  293. """
  294. return self._armed_away
  295. @armed_away.setter
  296. def armed_away(self, value):
  297. """
  298. Sets the value indicating whether or not the panel is armed in away mode.
  299. """
  300. self._armed_away = value
  301. @property
  302. def armed_home(self):
  303. """
  304. Indicates whether or not the panel is armed in home/stay mode.
  305. """
  306. return self._armed_home
  307. @armed_home.setter
  308. def armed_home(self, value):
  309. """
  310. Sets the value indicating whether or not the panel is armed in home/stay mode.
  311. """
  312. self._armed_home = value
  313. @property
  314. def backlight_on(self):
  315. """
  316. Indicates whether or not the panel backlight is on.
  317. """
  318. return self._backlight_on
  319. @backlight_on.setter
  320. def backlight_on(self, value):
  321. """
  322. Sets the value indicating whether or not the panel backlight is on.
  323. """
  324. self._backlight_on = value
  325. @property
  326. def programming_mode(self):
  327. """
  328. Indicates whether or not the panel is in programming mode.
  329. """
  330. return self._programming_mode
  331. @programming_mode.setter
  332. def programming_mode(self, value):
  333. """
  334. Sets the value indicating whether or not the panel is in programming mode.
  335. """
  336. self._programming_mode = value
  337. @property
  338. def beeps(self):
  339. """
  340. Returns the number of beeps associated with this message.
  341. """
  342. return self._beeps
  343. @beeps.setter
  344. def beeps(self, value):
  345. """
  346. Sets the number of beeps associated with this message.
  347. """
  348. self._beeps = value
  349. @property
  350. def zone_bypassed(self):
  351. """
  352. Indicates whether or not zones have been bypassed.
  353. """
  354. return self._zone_bypassed
  355. @zone_bypassed.setter
  356. def zone_bypassed(self, value):
  357. """
  358. Sets the value indicating whether or not zones have been bypassed.
  359. """
  360. self._zone_bypassed = value
  361. @property
  362. def ac_power(self):
  363. """
  364. Indicates whether or not the system is on AC power.
  365. """
  366. return self._ac_power
  367. @ac_power.setter
  368. def ac_power(self, value):
  369. """
  370. Sets the value indicating whether or not the system is on AC power.
  371. """
  372. self._ac_power = value
  373. @property
  374. def chime_on(self):
  375. """
  376. Indicates whether or not panel chimes are enabled.
  377. """
  378. return self._chime_on
  379. @chime_on.setter
  380. def chime_on(self, value):
  381. """
  382. Sets the value indicating whether or not the panel chimes are enabled.
  383. """
  384. self._chime_on = value
  385. @property
  386. def alarm_event_occurred(self):
  387. """
  388. Indicates whether or not an alarm event has occurred.
  389. """
  390. return self._alarm_event_occurred
  391. @alarm_event_occurred.setter
  392. def alarm_event_occurred(self, value):
  393. """
  394. Sets the value indicating whether or not an alarm event has occurred.
  395. """
  396. self._alarm_event_occurred = value
  397. @property
  398. def alarm_sounding(self):
  399. """
  400. Indicates whether or not an alarm is currently sounding.
  401. """
  402. return self._alarm_sounding
  403. @alarm_sounding.setter
  404. def alarm_sounding(self, value):
  405. """
  406. Sets the value indicating whether or not an alarm is currently sounding.
  407. """
  408. self._alarm_sounding = value
  409. @property
  410. def numeric_code(self):
  411. """
  412. Numeric indicator of associated with message. For example: If zone #3 is faulted, this value is 003.
  413. """
  414. return self._numeric_code
  415. @numeric_code.setter
  416. def numeric_code(self, value):
  417. """
  418. Sets the numeric indicator associated with this message.
  419. """
  420. self._numeric_code = value
  421. @property
  422. def text(self):
  423. """
  424. Alphanumeric text associated with this message.
  425. """
  426. return self._text
  427. @text.setter
  428. def text(self, value):
  429. """
  430. Sets the alphanumeric text associated with this message.
  431. """
  432. self._text = value
  433. @property
  434. def cursor_location(self):
  435. """
  436. Indicates which text position has the cursor underneath it.
  437. """
  438. return self._cursor_location
  439. @cursor_location.setter
  440. def cursor_location(self, value):
  441. """
  442. Sets the value indicating which text position has the cursor underneath it.
  443. """
  444. self._cursor_location = value
  445. @property
  446. def data(self):
  447. """
  448. Raw representation of the message from the panel.
  449. """
  450. return self._data
  451. @data.setter
  452. def data(self, value):
  453. """
  454. Sets the raw representation of the message from the panel.
  455. """
  456. self._data = value
  457. @property
  458. def mask(self):
  459. """
  460. The panel mask for which this message is intended.
  461. """
  462. return self._mask
  463. @mask.setter
  464. def mask(self, value):
  465. """
  466. Sets the panel mask for which this message is intended.
  467. """
  468. self._mask = value
  469. @property
  470. def bitfield(self):
  471. """
  472. The bit field associated with this message.
  473. """
  474. return self._bitfield
  475. @bitfield.setter
  476. def bitfield(self, value):
  477. """
  478. Sets the bit field associated with this message.
  479. """
  480. self._bitfield = value
  481. @property
  482. def panel_data(self):
  483. """
  484. The binary field associated with this message.
  485. """
  486. return self._panel_data
  487. @panel_data.setter
  488. def panel_data(self, value):
  489. """
  490. Sets the binary field associated with this message.
  491. """
  492. self._panel_data = value
  493. class ExpanderMessage(object):
  494. """
  495. Represents a message from a zone or relay expansion module.
  496. """
  497. ZONE = 0
  498. RELAY = 1
  499. def __init__(self, data=None):
  500. """
  501. Constructor
  502. """
  503. self._type = None
  504. self._address = None
  505. self._channel = None
  506. self._value = None
  507. self._raw = None
  508. if data is not None:
  509. self._parse_message(data)
  510. def __str__(self):
  511. """
  512. String conversion operator.
  513. """
  514. expander_type = 'UNKWN'
  515. if self.type == ExpanderMessage.ZONE:
  516. expander_type = 'ZONE'
  517. elif self.type == ExpanderMessage.RELAY:
  518. expander_type = 'RELAY'
  519. return 'exp > [{0: <5}] {1}/{2} -- {3}'.format(expander_type, self.address, self.channel, self.value)
  520. def _parse_message(self, data):
  521. """
  522. Parse the raw message from the device.
  523. """
  524. header, values = data.split(':')
  525. address, channel, value = values.split(',')
  526. self.raw = data
  527. self.address = address
  528. self.channel = channel
  529. self.value = value
  530. if header == '!EXP':
  531. self.type = ExpanderMessage.ZONE
  532. elif header == '!REL':
  533. self.type = ExpanderMessage.RELAY
  534. @property
  535. def address(self):
  536. """
  537. The relay address from which the message originated.
  538. """
  539. return self._address
  540. @address.setter
  541. def address(self, value):
  542. """
  543. Sets the relay address from which the message originated.
  544. """
  545. self._address = value
  546. @property
  547. def channel(self):
  548. """
  549. The zone expander channel from which the message originated.
  550. """
  551. return self._channel
  552. @channel.setter
  553. def channel(self, value):
  554. """
  555. Sets the zone expander channel from which the message originated.
  556. """
  557. self._channel = value
  558. @property
  559. def value(self):
  560. """
  561. The value associated with the message.
  562. """
  563. return self._value
  564. @value.setter
  565. def value(self, value):
  566. """
  567. Sets the value associated with the message.
  568. """
  569. self._value = value
  570. @property
  571. def raw(self):
  572. """
  573. The raw message from the expander device.
  574. """
  575. return self._raw
  576. @raw.setter
  577. def raw(self, value):
  578. """
  579. Sets the raw message from the expander device.
  580. """
  581. self._value = value
  582. @property
  583. def type(self):
  584. """
  585. The type of expander associated with this message.
  586. """
  587. return self._type
  588. @type.setter
  589. def type(self, value):
  590. """
  591. Sets the type of expander associated with this message.
  592. """
  593. self._type = value
  594. class RFMessage(object):
  595. """
  596. Represents a message from an RF receiver.
  597. """
  598. def __init__(self, data=None):
  599. """
  600. Constructor
  601. """
  602. self._raw = None
  603. self._serial_number = None
  604. self._value = None
  605. if data is not None:
  606. self._parse_message(data)
  607. def __str__(self):
  608. """
  609. String conversion operator.
  610. """
  611. return 'rf > {0}: {1}'.format(self.serial_number, self.value)
  612. def _parse_message(self, data):
  613. """
  614. Parses the raw message from the device.
  615. """
  616. self.raw = data
  617. _, values = data.split(':')
  618. self.serial_number, self.value = values.split(',')
  619. @property
  620. def serial_number(self):
  621. """
  622. The serial number for the RF receiver.
  623. """
  624. return self._serial_number
  625. @serial_number.setter
  626. def serial_number(self, value):
  627. self._serial_number = value
  628. @property
  629. def value(self):
  630. """
  631. The value of the RF message.
  632. """
  633. return self._value
  634. @value.setter
  635. def value(self, value):
  636. """
  637. Sets the value of the RF message.
  638. """
  639. self._value = value
  640. @property
  641. def raw(self):
  642. """
  643. The raw message from the RF receiver.
  644. """
  645. return self._raw
  646. @raw.setter
  647. def raw(self, value):
  648. """
  649. Sets the raw message from the RF receiver.
  650. """
  651. self._raw = value