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.

210 lines
5.9 KiB

  1. """
  2. Provides zone tracking functionality for the AD2USB device family.
  3. """
  4. import time
  5. from .event import event
  6. from . import messages
  7. class Zone(object):
  8. """
  9. Representation of a panel zone.
  10. """
  11. CLEAR = 0
  12. FAULT = 1
  13. CHECK = 2 # Wire fault
  14. STATUS = { CLEAR: 'CLEAR', FAULT: 'FAULT', CHECK: 'CHECK' }
  15. def __init__(self, zone=0, name='', status=CLEAR):
  16. self.zone = zone
  17. self.name = name
  18. self.status = status
  19. self.timestamp = time.time()
  20. def __str__(self):
  21. return 'Zone {0} {1}'.format(self.zone, self.name)
  22. def __repr__(self):
  23. return 'Zone({0}, {1}, ts {2})'.format(self.zone, Zone.STATUS[self.status], self.timestamp)
  24. class Zonetracker(object):
  25. """
  26. Handles tracking of zone and their statuses.
  27. """
  28. on_fault = event.Event('Called when the device detects a zone fault.')
  29. on_restore = event.Event('Called when the device detects that a fault is restored.')
  30. EXPIRE = 30
  31. def __init__(self):
  32. """
  33. Constructor
  34. """
  35. self._zones = {}
  36. self._zones_faulted = []
  37. self._last_zone_fault = 0
  38. def update(self, message):
  39. """
  40. Update zone statuses based on the current message.
  41. """
  42. zone = -1
  43. if isinstance(message, messages.ExpanderMessage):
  44. zone = self._expander_to_zone(int(message.address), int(message.channel))
  45. status = Zone.CLEAR
  46. if int(message.value) == 1:
  47. status = Zone.FAULT
  48. elif int(message.value) == 2:
  49. status = Zone.CHECK
  50. try:
  51. self._update_zone(zone, status=status)
  52. except IndexError:
  53. self._add_zone(zone, status=status)
  54. else:
  55. # Panel is ready, restore all zones.
  56. if message.ready:
  57. for idx, z in enumerate(self._zones_faulted):
  58. self._update_zone(z, Zone.CLEAR)
  59. self._last_zone_fault = 0
  60. # Process fault
  61. elif "FAULT" in message.text or message.check_zone:
  62. # Apparently this representation can be both base 10
  63. # or base 16, depending on where the message came
  64. # from.
  65. try:
  66. zone = int(message.numeric_code)
  67. except ValueError:
  68. zone = int(message.numeric_code, 16)
  69. # Add new zones and clear expired ones.
  70. if zone in self._zones_faulted:
  71. self._update_zone(zone)
  72. self._clear_zones(zone)
  73. else:
  74. status = Zone.FAULT
  75. if message.check_zone:
  76. status = Zone.CHECK
  77. self._add_zone(zone, status=status)
  78. self._zones_faulted.append(zone)
  79. self._zones_faulted.sort()
  80. # Save our spot for the next message.
  81. self._last_zone_fault = zone
  82. self._clear_expired_zones()
  83. def _clear_zones(self, zone):
  84. """
  85. Clear all expired zones from our status list.
  86. """
  87. cleared_zones = []
  88. found_last = found_new = at_end = False
  89. # First pass: Find our start spot.
  90. it = iter(self._zones_faulted)
  91. try:
  92. while not found_last:
  93. z = it.next()
  94. if z == self._last_zone_fault:
  95. found_last = True
  96. break
  97. except StopIteration:
  98. at_end = True
  99. # Continue until we find our end point and add zones in
  100. # between to our clear list.
  101. try:
  102. while not at_end and not found_new:
  103. z = it.next()
  104. if z == zone:
  105. found_new = True
  106. break
  107. else:
  108. cleared_zones += [z]
  109. except StopIteration:
  110. pass
  111. # Second pass: roll through the list again if we didn't find
  112. # our end point and remove everything until we do.
  113. if not found_new:
  114. it = iter(self._zones_faulted)
  115. try:
  116. while not found_new:
  117. z = it.next()
  118. if z == zone:
  119. found_new = True
  120. break
  121. else:
  122. cleared_zones += [z]
  123. except StopIteration:
  124. pass
  125. # Actually remove the zones and trigger the restores.
  126. for idx, z in enumerate(cleared_zones):
  127. self._update_zone(z, Zone.CLEAR)
  128. def _clear_expired_zones(self):
  129. zones = []
  130. for z in self._zones.keys():
  131. zones += [z]
  132. for z in zones:
  133. if self._zones[z].status != Zone.CLEAR and self._zone_expired(z):
  134. self._update_zone(z, Zone.CLEAR)
  135. def _add_zone(self, zone, name='', status=Zone.CLEAR):
  136. """
  137. Adds a zone to the internal zone list.
  138. """
  139. if not zone in self._zones:
  140. self._zones[zone] = Zone(zone=zone, name=name, status=status)
  141. if status != Zone.CLEAR:
  142. self.on_fault(zone)
  143. def _update_zone(self, zone, status=None):
  144. """
  145. Updates a zones status.
  146. """
  147. if not zone in self._zones:
  148. raise IndexError('Zone does not exist and cannot be updated: %d', zone)
  149. if status is not None:
  150. self._zones[zone].status = status
  151. self._zones[zone].timestamp = time.time()
  152. if status == Zone.CLEAR:
  153. if zone in self._zones_faulted:
  154. self._zones_faulted.remove(zone)
  155. self.on_restore(zone)
  156. def _zone_expired(self, zone):
  157. if time.time() > self._zones[zone].timestamp + Zonetracker.EXPIRE:
  158. return True
  159. return False
  160. def _expander_to_zone(self, address, channel):
  161. idx = address - 7 # Expanders start at address 7.
  162. return address + channel + (idx * 7) + 1