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.

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