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.

816 lines
21 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):
  148. """
  149. Opens the device.
  150. """
  151. self._wire_events()
  152. self._device.open(baudrate=baudrate, interface=interface, index=index)
  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. if msg:
  183. self.on_message(msg)
  184. def _update_internal_states(self, message):
  185. if message.ac != self._power_status:
  186. self._power_status, old_status = message.ac, self._power_status
  187. if old_status is not None:
  188. self.on_power_changed(self._power_status)
  189. if message.alarm_bell != self._alarm_status:
  190. self._alarm_status, old_status = message.alarm_bell, self._alarm_status
  191. if old_status is not None:
  192. self.on_alarm(self._alarm_status)
  193. if message.bypass != self._bypass_status:
  194. self._bypass_status, old_status = message.bypass, self._bypass_status
  195. if old_status is not None:
  196. self.on_bypass(self._bypass_status)
  197. def _on_open(self, sender, args):
  198. """
  199. Internal handler for opening the device.
  200. """
  201. self.on_open(args)
  202. def _on_close(self, sender, args):
  203. """
  204. Internal handler for closing the device.
  205. """
  206. self.on_close(args)
  207. def _on_read(self, sender, args):
  208. """
  209. Internal handler for reading from the device.
  210. """
  211. msg = self._handle_message(args)
  212. if msg:
  213. self.on_message(msg)
  214. self.on_read(args)
  215. def _on_write(self, sender, args):
  216. """
  217. Internal handler for writing to the device.
  218. """
  219. self.on_write(args)
  220. class Message(object):
  221. """
  222. Represents a message from the alarm panel.
  223. """
  224. def __init__(self, data=None):
  225. """
  226. Constructor
  227. """
  228. self._ignore_packet = False
  229. self._ready = False
  230. self._armed_away = False
  231. self._armed_home = False
  232. self._backlight = False
  233. self._programming_mode = False
  234. self._beeps = -1
  235. self._bypass = False
  236. self._ac = False
  237. self._chime_mode = False
  238. self._alarm_event_occurred = False
  239. self._alarm_bell = False
  240. self._numeric = ""
  241. self._text = ""
  242. self._cursor = -1
  243. self._raw = ""
  244. self._mask = ""
  245. self._msg_bitfields = ""
  246. self._msg_zone = ""
  247. self._msg_binary = ""
  248. self._msg_alpha = ""
  249. self._regex = re.compile('("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*),("(?:[^"]|"")*"|[^,]*)')
  250. if data is not None:
  251. self._parse_message(data)
  252. def _parse_message(self, data):
  253. """
  254. Parse the raw message from the device.
  255. """
  256. m = self._regex.match(data)
  257. if m is None:
  258. raise util.InvalidMessageError('Received invalid message: {0}'.format(data))
  259. self._msg_bitfields, self._msg_zone, self._msg_binary, self._msg_alpha = m.group(1, 2, 3, 4)
  260. self.mask = int(self._msg_binary[3:3+8], 16)
  261. self.raw = data
  262. self.ready = not self._msg_bitfields[1:2] == "0"
  263. self.armed_away = not self._msg_bitfields[2:3] == "0"
  264. self.armed_home = not self._msg_bitfields[3:4] == "0"
  265. self.backlight = not self._msg_bitfields[4:5] == "0"
  266. self.programming_mode = not self._msg_bitfields[5:6] == "0"
  267. self.beeps = int(self._msg_bitfields[6:7], 16)
  268. self.bypass = not self._msg_bitfields[7:8] == "0"
  269. self.ac = not self._msg_bitfields[8:9] == "0"
  270. self.chime_mode = not self._msg_bitfields[9:10] == "0"
  271. self.alarm_event_occurred = not self._msg_bitfields[10:11] == "0"
  272. self.alarm_bell = not self._msg_bitfields[11:12] == "0"
  273. self.numeric = self._msg_zone
  274. self.text = self._msg_alpha.strip('"')
  275. if int(self._msg_binary[19:21], 16) & 0x01 > 0:
  276. self.cursor = int(self._msg_bitfields[21:23], 16)
  277. #print "Message:\r\n" \
  278. # "\tmask: {0}\r\n" \
  279. # "\tready: {1}\r\n" \
  280. # "\tarmed_away: {2}\r\n" \
  281. # "\tarmed_home: {3}\r\n" \
  282. # "\tbacklight: {4}\r\n" \
  283. # "\tprogramming_mode: {5}\r\n" \
  284. # "\tbeeps: {6}\r\n" \
  285. # "\tbypass: {7}\r\n" \
  286. # "\tac: {8}\r\n" \
  287. # "\tchime_mode: {9}\r\n" \
  288. # "\talarm_event_occurred: {10}\r\n" \
  289. # "\talarm_bell: {11}\r\n" \
  290. # "\tcursor: {12}\r\n" \
  291. # "\tnumeric: {13}\r\n" \
  292. # "\ttext: {14}\r\n".format(
  293. # self.mask,
  294. # self.ready,
  295. # self.armed_away,
  296. # self.armed_home,
  297. # self.backlight,
  298. # self.programming_mode,
  299. # self.beeps,
  300. # self.bypass,
  301. # self.ac,
  302. # self.chime_mode,
  303. # self.alarm_event_occurred,
  304. # self.alarm_bell,
  305. # self.cursor,
  306. # self.numeric,
  307. # self.text
  308. # )
  309. def __str__(self):
  310. """
  311. String conversion operator.
  312. """
  313. 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, self.text)
  314. @property
  315. def ignore_packet(self):
  316. """
  317. Indicates whether or not this message should be ignored.
  318. """
  319. return self._ignore_packet
  320. @ignore_packet.setter
  321. def ignore_packet(self, value):
  322. """
  323. Sets the value indicating whether or not this packet should be ignored.
  324. """
  325. self._ignore_packet = value
  326. @property
  327. def ready(self):
  328. """
  329. Indicates whether or not the panel is ready.
  330. """
  331. return self._ready
  332. @ready.setter
  333. def ready(self, value):
  334. """
  335. Sets the value indicating whether or not the panel is ready.
  336. """
  337. self._ready = value
  338. @property
  339. def armed_away(self):
  340. """
  341. Indicates whether or not the panel is armed in away mode.
  342. """
  343. return self._armed_away
  344. @armed_away.setter
  345. def armed_away(self, value):
  346. """
  347. Sets the value indicating whether or not the panel is armed in away mode.
  348. """
  349. self._armed_away = value
  350. @property
  351. def armed_home(self):
  352. """
  353. Indicates whether or not the panel is armed in home/stay mode.
  354. """
  355. return self._armed_home
  356. @armed_home.setter
  357. def armed_home(self, value):
  358. """
  359. Sets the value indicating whether or not the panel is armed in home/stay mode.
  360. """
  361. self._armed_home = value
  362. @property
  363. def backlight(self):
  364. """
  365. Indicates whether or not the panel backlight is on.
  366. """
  367. return self._backlight
  368. @backlight.setter
  369. def backlight(self, value):
  370. """
  371. Sets the value indicating whether or not the panel backlight is on.
  372. """
  373. self._backlight = value
  374. @property
  375. def programming_mode(self):
  376. """
  377. Indicates whether or not the panel is in programming mode.
  378. """
  379. return self._programming_mode
  380. @programming_mode.setter
  381. def programming_mode(self, value):
  382. """
  383. Sets the value indicating whether or not the panel is in programming mode.
  384. """
  385. self._programming_mode = value
  386. @property
  387. def beeps(self):
  388. """
  389. Returns the number of beeps associated with this message.
  390. """
  391. return self._beeps
  392. @beeps.setter
  393. def beeps(self, value):
  394. """
  395. Sets the number of beeps associated with this message.
  396. """
  397. self._beeps = value
  398. @property
  399. def bypass(self):
  400. """
  401. Indicates whether or not zones have been bypassed.
  402. """
  403. return self._bypass
  404. @bypass.setter
  405. def bypass(self, value):
  406. """
  407. Sets the value indicating whether or not zones have been bypassed.
  408. """
  409. self._bypass = value
  410. @property
  411. def ac(self):
  412. """
  413. Indicates whether or not the system is on AC power.
  414. """
  415. return self._ac
  416. @ac.setter
  417. def ac(self, value):
  418. """
  419. Sets the value indicating whether or not the system is on AC power.
  420. """
  421. self._ac = value
  422. @property
  423. def chime_mode(self):
  424. """
  425. Indicates whether or not panel chimes are enabled.
  426. """
  427. return self._chime_mode
  428. @chime_mode.setter
  429. def chime_mode(self, value):
  430. """
  431. Sets the value indicating whether or not the panel chimes are enabled.
  432. """
  433. self._chime_mode = value
  434. @property
  435. def alarm_event_occurred(self):
  436. """
  437. Indicates whether or not an alarm event has occurred.
  438. """
  439. return self._alarm_event_occurred
  440. @alarm_event_occurred.setter
  441. def alarm_event_occurred(self, value):
  442. """
  443. Sets the value indicating whether or not an alarm event has occurred.
  444. """
  445. self._alarm_event_occurred = value
  446. @property
  447. def alarm_bell(self):
  448. """
  449. Indicates whether or not an alarm is currently sounding.
  450. """
  451. return self._alarm_bell
  452. @alarm_bell.setter
  453. def alarm_bell(self, value):
  454. """
  455. Sets the value indicating whether or not an alarm is currently sounding.
  456. """
  457. self._alarm_bell = value
  458. @property
  459. def numeric(self):
  460. """
  461. Numeric indicator of associated with message. For example: If zone #3 is faulted, this value is 003.
  462. """
  463. return self._numeric
  464. @numeric.setter
  465. def numeric(self, value):
  466. """
  467. Sets the numeric indicator associated with this message.
  468. """
  469. self._numeric = value
  470. @property
  471. def text(self):
  472. """
  473. Alphanumeric text associated with this message.
  474. """
  475. return self._text
  476. @text.setter
  477. def text(self, value):
  478. """
  479. Sets the alphanumeric text associated with this message.
  480. """
  481. self._text = value
  482. @property
  483. def cursor(self):
  484. """
  485. Indicates which text position has the cursor underneath it.
  486. """
  487. return self._cursor
  488. @cursor.setter
  489. def cursor(self, value):
  490. """
  491. Sets the value indicating which text position has the cursor underneath it.
  492. """
  493. self._cursor = value
  494. @property
  495. def raw(self):
  496. """
  497. Raw representation of the message data from the panel.
  498. """
  499. return self._raw
  500. @raw.setter
  501. def raw(self, value):
  502. """
  503. Sets the raw representation of the message data from the panel.
  504. """
  505. self._raw = value
  506. @property
  507. def mask(self):
  508. """
  509. The panel mask for which this message is intended.
  510. """
  511. return self._mask
  512. @mask.setter
  513. def mask(self, value):
  514. """
  515. Sets the panel mask for which this message is intended.
  516. """
  517. self._mask = value
  518. class ExpanderMessage(object):
  519. """
  520. Represents a message from a zone or relay expansion module.
  521. """
  522. ZONE = 0
  523. RELAY = 1
  524. def __init__(self, data=None):
  525. """
  526. Constructor
  527. """
  528. self._type = None
  529. self._address = None
  530. self._channel = None
  531. self._value = None
  532. self._raw = None
  533. if data is not None:
  534. self._parse_message(data)
  535. def __str__(self):
  536. """
  537. String conversion operator.
  538. """
  539. expander_type = 'UNKWN'
  540. if self.type == ExpanderMessage.ZONE:
  541. expander_type = 'ZONE'
  542. elif self.type == ExpanderMessage.RELAY:
  543. expander_type = 'RELAY'
  544. return 'exp > [{0: <5}] {1}/{2} -- {3}'.format(expander_type, self.address, self.channel, self.value)
  545. def _parse_message(self, data):
  546. """
  547. Parse the raw message from the device.
  548. """
  549. header, values = data.split(':')
  550. address, channel, value = values.split(',')
  551. self.raw = data
  552. self.address = address
  553. self.channel = channel
  554. self.value = value
  555. if header == '!EXP':
  556. self.type = ExpanderMessage.ZONE
  557. elif header == '!REL':
  558. self.type = ExpanderMessage.RELAY
  559. @property
  560. def address(self):
  561. """
  562. The relay address from which the message originated.
  563. """
  564. return self._address
  565. @address.setter
  566. def address(self, value):
  567. """
  568. Sets the relay address from which the message originated.
  569. """
  570. self._address = value
  571. @property
  572. def channel(self):
  573. """
  574. The zone expander channel from which the message originated.
  575. """
  576. return self._channel
  577. @channel.setter
  578. def channel(self, value):
  579. """
  580. Sets the zone expander channel from which the message originated.
  581. """
  582. self._channel = value
  583. @property
  584. def value(self):
  585. """
  586. The value associated with the message.
  587. """
  588. return self._value
  589. @value.setter
  590. def value(self, value):
  591. """
  592. Sets the value associated with the message.
  593. """
  594. self._value = value
  595. @property
  596. def raw(self):
  597. """
  598. The raw message from the expander device.
  599. """
  600. return self._raw
  601. @raw.setter
  602. def raw(self, value):
  603. """
  604. Sets the raw message from the expander device.
  605. """
  606. self._value = value
  607. @property
  608. def type(self):
  609. """
  610. The type of expander associated with this message.
  611. """
  612. return self._type
  613. @type.setter
  614. def type(self, value):
  615. """
  616. Sets the type of expander associated with this message.
  617. """
  618. self._type = value
  619. class RFMessage(object):
  620. """
  621. Represents a message from an RF receiver.
  622. """
  623. def __init__(self, data=None):
  624. """
  625. Constructor
  626. """
  627. self._raw = None
  628. self._serial_number = None
  629. self._value = None
  630. if data is not None:
  631. self._parse_message(data)
  632. def __str__(self):
  633. """
  634. String conversion operator.
  635. """
  636. return 'rf > {0}: {1}'.format(self.serial_number, self.value)
  637. def _parse_message(self, data):
  638. """
  639. Parses the raw message from the device.
  640. """
  641. self.raw = data
  642. _, values = data.split(':')
  643. self.serial_number, self.value = values.split(',')
  644. @property
  645. def serial_number(self):
  646. """
  647. The serial number for the RF receiver.
  648. """
  649. return self._serial_number
  650. @serial_number.setter
  651. def serial_number(self, value):
  652. self._serial_number = value
  653. @property
  654. def value(self):
  655. """
  656. The value of the RF message.
  657. """
  658. return self._value
  659. @value.setter
  660. def value(self, value):
  661. """
  662. Sets the value of the RF message.
  663. """
  664. self._value = value
  665. @property
  666. def raw(self):
  667. """
  668. The raw message from the RF receiver.
  669. """
  670. return self._raw
  671. @raw.setter
  672. def raw(self, value):
  673. """
  674. Sets the raw message from the RF receiver.
  675. """
  676. self._raw = value