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.

190 lines
5.1 KiB

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