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.

368 lines
33 KiB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>pyad2usb.zonetracking &mdash; pyad2usb documentation</title>
  7. <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../../',
  12. VERSION: '',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../../_static/doctools.js"></script>
  21. <link rel="top" title="pyad2usb documentation" href="../../index.html" />
  22. <link rel="up" title="Module code" href="../index.html" />
  23. </head>
  24. <body>
  25. <div class="related">
  26. <h3>Navigation</h3>
  27. <ul>
  28. <li class="right" style="margin-right: 10px">
  29. <a href="../../genindex.html" title="General Index"
  30. accesskey="I">index</a></li>
  31. <li class="right" >
  32. <a href="../../py-modindex.html" title="Python Module Index"
  33. >modules</a> |</li>
  34. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  35. <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li>
  36. </ul>
  37. </div>
  38. <div class="document">
  39. <div class="documentwrapper">
  40. <div class="bodywrapper">
  41. <div class="body">
  42. <h1>Source code for pyad2usb.zonetracking</h1><div class="highlight"><pre>
  43. <span class="sd">&quot;&quot;&quot;</span>
  44. <span class="sd">Provides zone tracking functionality for the AD2USB device family.</span>
  45. <span class="sd">&quot;&quot;&quot;</span>
  46. <span class="kn">import</span> <span class="nn">time</span>
  47. <span class="kn">from</span> <span class="nn">.event</span> <span class="kn">import</span> <span class="n">event</span>
  48. <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">messages</span>
  49. <div class="viewcode-block" id="Zone"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.zonetracking.Zone">[docs]</a><span class="k">class</span> <span class="nc">Zone</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  50. <span class="sd">&quot;&quot;&quot;</span>
  51. <span class="sd"> Representation of a panel zone.</span>
  52. <span class="sd"> &quot;&quot;&quot;</span>
  53. <span class="n">CLEAR</span> <span class="o">=</span> <span class="mi">0</span>
  54. <span class="sd">&quot;&quot;&quot;Status indicating that the zone is cleared.&quot;&quot;&quot;</span>
  55. <span class="n">FAULT</span> <span class="o">=</span> <span class="mi">1</span>
  56. <span class="sd">&quot;&quot;&quot;Status indicating that the zone is faulted.&quot;&quot;&quot;</span>
  57. <span class="n">CHECK</span> <span class="o">=</span> <span class="mi">2</span> <span class="c"># Wire fault</span>
  58. <span class="sd">&quot;&quot;&quot;Status indicating that there is a wiring issue with the zone.&quot;&quot;&quot;</span>
  59. <span class="n">STATUS</span> <span class="o">=</span> <span class="p">{</span> <span class="n">CLEAR</span><span class="p">:</span> <span class="s">&#39;CLEAR&#39;</span><span class="p">,</span> <span class="n">FAULT</span><span class="p">:</span> <span class="s">&#39;FAULT&#39;</span><span class="p">,</span> <span class="n">CHECK</span><span class="p">:</span> <span class="s">&#39;CHECK&#39;</span> <span class="p">}</span>
  60. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">zone</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">CLEAR</span><span class="p">):</span>
  61. <span class="sd">&quot;&quot;&quot;</span>
  62. <span class="sd"> Constructor</span>
  63. <span class="sd"> :param zone: The zone number.</span>
  64. <span class="sd"> :type zone: int</span>
  65. <span class="sd"> :param name: Human readable zone name.</span>
  66. <span class="sd"> :type name: str</span>
  67. <span class="sd"> :param status: Initial zone state.</span>
  68. <span class="sd"> :type status: int</span>
  69. <span class="sd"> &quot;&quot;&quot;</span>
  70. <span class="bp">self</span><span class="o">.</span><span class="n">zone</span> <span class="o">=</span> <span class="n">zone</span>
  71. <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
  72. <span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="n">status</span>
  73. <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
  74. <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  75. <span class="sd">&quot;&quot;&quot;</span>
  76. <span class="sd"> String conversion operator.</span>
  77. <span class="sd"> &quot;&quot;&quot;</span>
  78. <span class="k">return</span> <span class="s">&#39;Zone {0} {1}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">zone</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  79. <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  80. <span class="sd">&quot;&quot;&quot;</span>
  81. <span class="sd"> Human readable representation operator.</span>
  82. <span class="sd"> &quot;&quot;&quot;</span>
  83. <span class="k">return</span> <span class="s">&#39;Zone({0}, {1}, ts {2})&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">zone</span><span class="p">,</span> <span class="n">Zone</span><span class="o">.</span><span class="n">STATUS</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">status</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span><span class="p">)</span>
  84. </div>
  85. <div class="viewcode-block" id="Zonetracker"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.zonetracking.Zonetracker">[docs]</a><span class="k">class</span> <span class="nc">Zonetracker</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  86. <span class="sd">&quot;&quot;&quot;</span>
  87. <span class="sd"> Handles tracking of zone and their statuses.</span>
  88. <span class="sd"> &quot;&quot;&quot;</span>
  89. <span class="n">on_fault</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device detects a zone fault.&#39;</span><span class="p">)</span>
  90. <span class="n">on_restore</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device detects that a fault is restored.&#39;</span><span class="p">)</span>
  91. <span class="n">EXPIRE</span> <span class="o">=</span> <span class="mi">30</span>
  92. <span class="sd">&quot;&quot;&quot;Zone expiration timeout.&quot;&quot;&quot;</span>
  93. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  94. <span class="sd">&quot;&quot;&quot;</span>
  95. <span class="sd"> Constructor</span>
  96. <span class="sd"> &quot;&quot;&quot;</span>
  97. <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span> <span class="o">=</span> <span class="p">{}</span>
  98. <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span> <span class="o">=</span> <span class="p">[]</span>
  99. <span class="bp">self</span><span class="o">.</span><span class="n">_last_zone_fault</span> <span class="o">=</span> <span class="mi">0</span>
  100. <div class="viewcode-block" id="Zonetracker.update"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.zonetracking.Zonetracker.update">[docs]</a> <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
  101. <span class="sd">&quot;&quot;&quot;</span>
  102. <span class="sd"> Update zone statuses based on the current message.</span>
  103. <span class="sd"> :param message: Message to use to update the zone tracking.</span>
  104. <span class="sd"> :type message: Message or ExpanderMessage</span>
  105. <span class="sd"> &quot;&quot;&quot;</span>
  106. <span class="n">zone</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
  107. <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">ExpanderMessage</span><span class="p">):</span>
  108. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">messages</span><span class="o">.</span><span class="n">ExpanderMessage</span><span class="o">.</span><span class="n">ZONE</span><span class="p">:</span>
  109. <span class="n">zone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_expander_to_zone</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">address</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">channel</span><span class="p">))</span>
  110. <span class="n">status</span> <span class="o">=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span>
  111. <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
  112. <span class="n">status</span> <span class="o">=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">FAULT</span>
  113. <span class="k">elif</span> <span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
  114. <span class="n">status</span> <span class="o">=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CHECK</span>
  115. <span class="k">try</span><span class="p">:</span>
  116. <span class="bp">self</span><span class="o">.</span><span class="n">_update_zone</span><span class="p">(</span><span class="n">zone</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">)</span>
  117. <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
  118. <span class="bp">self</span><span class="o">.</span><span class="n">_add_zone</span><span class="p">(</span><span class="n">zone</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">)</span>
  119. <span class="k">else</span><span class="p">:</span>
  120. <span class="c"># Panel is ready, restore all zones.</span>
  121. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">ready</span><span class="p">:</span>
  122. <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="p">):</span>
  123. <span class="bp">self</span><span class="o">.</span><span class="n">_update_zone</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">)</span>
  124. <span class="bp">self</span><span class="o">.</span><span class="n">_last_zone_fault</span> <span class="o">=</span> <span class="mi">0</span>
  125. <span class="c"># Process fault</span>
  126. <span class="k">elif</span> <span class="s">&quot;FAULT&quot;</span> <span class="ow">in</span> <span class="n">message</span><span class="o">.</span><span class="n">text</span> <span class="ow">or</span> <span class="n">message</span><span class="o">.</span><span class="n">check_zone</span><span class="p">:</span>
  127. <span class="c"># Apparently this representation can be both base 10</span>
  128. <span class="c"># or base 16, depending on where the message came</span>
  129. <span class="c"># from.</span>
  130. <span class="k">try</span><span class="p">:</span>
  131. <span class="n">zone</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">numeric_code</span><span class="p">)</span>
  132. <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
  133. <span class="n">zone</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">numeric_code</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>
  134. <span class="c"># Add new zones and clear expired ones.</span>
  135. <span class="k">if</span> <span class="n">zone</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="p">:</span>
  136. <span class="bp">self</span><span class="o">.</span><span class="n">_update_zone</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  137. <span class="bp">self</span><span class="o">.</span><span class="n">_clear_zones</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  138. <span class="k">else</span><span class="p">:</span>
  139. <span class="n">status</span> <span class="o">=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">FAULT</span>
  140. <span class="k">if</span> <span class="n">message</span><span class="o">.</span><span class="n">check_zone</span><span class="p">:</span>
  141. <span class="n">status</span> <span class="o">=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CHECK</span>
  142. <span class="bp">self</span><span class="o">.</span><span class="n">_add_zone</span><span class="p">(</span><span class="n">zone</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">)</span>
  143. <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  144. <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
  145. <span class="c"># Save our spot for the next message.</span>
  146. <span class="bp">self</span><span class="o">.</span><span class="n">_last_zone_fault</span> <span class="o">=</span> <span class="n">zone</span>
  147. <span class="bp">self</span><span class="o">.</span><span class="n">_clear_expired_zones</span><span class="p">()</span>
  148. </div>
  149. <span class="k">def</span> <span class="nf">_clear_zones</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">zone</span><span class="p">):</span>
  150. <span class="sd">&quot;&quot;&quot;</span>
  151. <span class="sd"> Clear all expired zones from our status list.</span>
  152. <span class="sd"> :param zone: current zone being processed.</span>
  153. <span class="sd"> :type zone: int</span>
  154. <span class="sd"> &quot;&quot;&quot;</span>
  155. <span class="n">cleared_zones</span> <span class="o">=</span> <span class="p">[]</span>
  156. <span class="n">found_last</span> <span class="o">=</span> <span class="n">found_new</span> <span class="o">=</span> <span class="n">at_end</span> <span class="o">=</span> <span class="bp">False</span>
  157. <span class="c"># First pass: Find our start spot.</span>
  158. <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="p">)</span>
  159. <span class="k">try</span><span class="p">:</span>
  160. <span class="k">while</span> <span class="ow">not</span> <span class="n">found_last</span><span class="p">:</span>
  161. <span class="n">z</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  162. <span class="k">if</span> <span class="n">z</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_last_zone_fault</span><span class="p">:</span>
  163. <span class="n">found_last</span> <span class="o">=</span> <span class="bp">True</span>
  164. <span class="k">break</span>
  165. <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
  166. <span class="n">at_end</span> <span class="o">=</span> <span class="bp">True</span>
  167. <span class="c"># Continue until we find our end point and add zones in</span>
  168. <span class="c"># between to our clear list.</span>
  169. <span class="k">try</span><span class="p">:</span>
  170. <span class="k">while</span> <span class="ow">not</span> <span class="n">at_end</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found_new</span><span class="p">:</span>
  171. <span class="n">z</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  172. <span class="k">if</span> <span class="n">z</span> <span class="o">==</span> <span class="n">zone</span><span class="p">:</span>
  173. <span class="n">found_new</span> <span class="o">=</span> <span class="bp">True</span>
  174. <span class="k">break</span>
  175. <span class="k">else</span><span class="p">:</span>
  176. <span class="n">cleared_zones</span> <span class="o">+=</span> <span class="p">[</span><span class="n">z</span><span class="p">]</span>
  177. <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
  178. <span class="k">pass</span>
  179. <span class="c"># Second pass: roll through the list again if we didn&#39;t find</span>
  180. <span class="c"># our end point and remove everything until we do.</span>
  181. <span class="k">if</span> <span class="ow">not</span> <span class="n">found_new</span><span class="p">:</span>
  182. <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="p">)</span>
  183. <span class="k">try</span><span class="p">:</span>
  184. <span class="k">while</span> <span class="ow">not</span> <span class="n">found_new</span><span class="p">:</span>
  185. <span class="n">z</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  186. <span class="k">if</span> <span class="n">z</span> <span class="o">==</span> <span class="n">zone</span><span class="p">:</span>
  187. <span class="n">found_new</span> <span class="o">=</span> <span class="bp">True</span>
  188. <span class="k">break</span>
  189. <span class="k">else</span><span class="p">:</span>
  190. <span class="n">cleared_zones</span> <span class="o">+=</span> <span class="p">[</span><span class="n">z</span><span class="p">]</span>
  191. <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
  192. <span class="k">pass</span>
  193. <span class="c"># Actually remove the zones and trigger the restores.</span>
  194. <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">cleared_zones</span><span class="p">):</span>
  195. <span class="bp">self</span><span class="o">.</span><span class="n">_update_zone</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">)</span>
  196. <span class="k">def</span> <span class="nf">_clear_expired_zones</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  197. <span class="sd">&quot;&quot;&quot;</span>
  198. <span class="sd"> Update zone status for all expired zones.</span>
  199. <span class="sd"> &quot;&quot;&quot;</span>
  200. <span class="n">zones</span> <span class="o">=</span> <span class="p">[]</span>
  201. <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
  202. <span class="n">zones</span> <span class="o">+=</span> <span class="p">[</span><span class="n">z</span><span class="p">]</span>
  203. <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="n">zones</span><span class="p">:</span>
  204. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">[</span><span class="n">z</span><span class="p">]</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zone_expired</span><span class="p">(</span><span class="n">z</span><span class="p">):</span>
  205. <span class="bp">self</span><span class="o">.</span><span class="n">_update_zone</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">)</span>
  206. <span class="k">def</span> <span class="nf">_add_zone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">zone</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">):</span>
  207. <span class="sd">&quot;&quot;&quot;</span>
  208. <span class="sd"> Adds a zone to the internal zone list.</span>
  209. <span class="sd"> :param zone: The zone number.</span>
  210. <span class="sd"> :type zone: int</span>
  211. <span class="sd"> :param name: Human readable zone name.</span>
  212. <span class="sd"> :type name: str</span>
  213. <span class="sd"> :param status: The zone status.</span>
  214. <span class="sd"> :type status: int</span>
  215. <span class="sd"> &quot;&quot;&quot;</span>
  216. <span class="k">if</span> <span class="ow">not</span> <span class="n">zone</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">:</span>
  217. <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span> <span class="o">=</span> <span class="n">Zone</span><span class="p">(</span><span class="n">zone</span><span class="o">=</span><span class="n">zone</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">)</span>
  218. <span class="k">if</span> <span class="n">status</span> <span class="o">!=</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">:</span>
  219. <span class="bp">self</span><span class="o">.</span><span class="n">on_fault</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  220. <span class="k">def</span> <span class="nf">_update_zone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">zone</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  221. <span class="sd">&quot;&quot;&quot;</span>
  222. <span class="sd"> Updates a zones status.</span>
  223. <span class="sd"> :param zone: The zone number.</span>
  224. <span class="sd"> :type zone: int</span>
  225. <span class="sd"> :param status: The zone status.</span>
  226. <span class="sd"> :type status: int</span>
  227. <span class="sd"> :raises: IndexError</span>
  228. <span class="sd"> &quot;&quot;&quot;</span>
  229. <span class="k">if</span> <span class="ow">not</span> <span class="n">zone</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">:</span>
  230. <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">&#39;Zone does not exist and cannot be updated: </span><span class="si">%d</span><span class="s">&#39;</span><span class="p">,</span> <span class="n">zone</span><span class="p">)</span>
  231. <span class="k">if</span> <span class="n">status</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  232. <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="n">status</span>
  233. <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span><span class="o">.</span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
  234. <span class="k">if</span> <span class="n">status</span> <span class="o">==</span> <span class="n">Zone</span><span class="o">.</span><span class="n">CLEAR</span><span class="p">:</span>
  235. <span class="k">if</span> <span class="n">zone</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="p">:</span>
  236. <span class="bp">self</span><span class="o">.</span><span class="n">_zones_faulted</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  237. <span class="bp">self</span><span class="o">.</span><span class="n">on_restore</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span>
  238. <span class="k">def</span> <span class="nf">_zone_expired</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">zone</span><span class="p">):</span>
  239. <span class="sd">&quot;&quot;&quot;</span>
  240. <span class="sd"> Determine if a zone is expired or not.</span>
  241. <span class="sd"> :param zone: The zone number.</span>
  242. <span class="sd"> :type zone: int</span>
  243. <span class="sd"> :returns: Whether or not the zone is expired.</span>
  244. <span class="sd"> &quot;&quot;&quot;</span>
  245. <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_zones</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span><span class="o">.</span><span class="n">timestamp</span> <span class="o">+</span> <span class="n">Zonetracker</span><span class="o">.</span><span class="n">EXPIRE</span><span class="p">:</span>
  246. <span class="k">return</span> <span class="bp">True</span>
  247. <span class="k">return</span> <span class="bp">False</span>
  248. <span class="k">def</span> <span class="nf">_expander_to_zone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">address</span><span class="p">,</span> <span class="n">channel</span><span class="p">):</span>
  249. <span class="sd">&quot;&quot;&quot;</span>
  250. <span class="sd"> Convert an address and channel into a zone number.</span>
  251. <span class="sd"> :param address: The expander address</span>
  252. <span class="sd"> :type address: int</span>
  253. <span class="sd"> :param channel: The channel</span>
  254. <span class="sd"> :type channel: int</span>
  255. <span class="sd"> :returns: The zone number associated with an address and channel.</span>
  256. <span class="sd"> &quot;&quot;&quot;</span>
  257. <span class="c"># TODO: This is going to need to be reworked to support the larger</span>
  258. <span class="c"># panels without fixed addressing on the expanders.</span>
  259. <span class="n">idx</span> <span class="o">=</span> <span class="n">address</span> <span class="o">-</span> <span class="mi">7</span> <span class="c"># Expanders start at address 7.</span>
  260. <span class="k">return</span> <span class="n">address</span> <span class="o">+</span> <span class="n">channel</span> <span class="o">+</span> <span class="p">(</span><span class="n">idx</span> <span class="o">*</span> <span class="mi">7</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span></div>
  261. </pre></div>
  262. </div>
  263. </div>
  264. </div>
  265. <div class="sphinxsidebar">
  266. <div class="sphinxsidebarwrapper">
  267. <div id="searchbox" style="display: none">
  268. <h3>Quick search</h3>
  269. <form class="search" action="../../search.html" method="get">
  270. <input type="text" name="q" />
  271. <input type="submit" value="Go" />
  272. <input type="hidden" name="check_keywords" value="yes" />
  273. <input type="hidden" name="area" value="default" />
  274. </form>
  275. <p class="searchtip" style="font-size: 90%">
  276. Enter search terms or a module, class or function name.
  277. </p>
  278. </div>
  279. <script type="text/javascript">$('#searchbox').show(0);</script>
  280. </div>
  281. </div>
  282. <div class="clearer"></div>
  283. </div>
  284. <div class="related">
  285. <h3>Navigation</h3>
  286. <ul>
  287. <li class="right" style="margin-right: 10px">
  288. <a href="../../genindex.html" title="General Index"
  289. >index</a></li>
  290. <li class="right" >
  291. <a href="../../py-modindex.html" title="Python Module Index"
  292. >modules</a> |</li>
  293. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  294. <li><a href="../index.html" >Module code</a> &raquo;</li>
  295. </ul>
  296. </div>
  297. <div class="footer">
  298. &copy; Copyright 2013, Author.
  299. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
  300. </div>
  301. </body>
  302. </html>